How to set minimum size limit for a JFrame in Java



Use the setMinimumSize() method to set the minimum size limit for a JFrame −

JFrame frame = new JFrame(); frame.setMinimumSize(new Dimension(500, 300));

The following is an example to set minimum size limit for a JFrame −

Example

import java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JButton button = new JButton("Close!");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setContentPane(button);       button.addActionListener(e -> {          frame.dispose();       });       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setMinimumSize(new Dimension(500, 300));       frame.getContentPane().setBackground(Color.ORANGE);       frame.pack();       frame.setVisible(true);    } }

Output

Updated on: 2019-07-30T22:30:26+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements