How to set minimum size limit for a JFrame in Java

Java 8Object Oriented ProgrammingProgramming

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

raja
Published on 08-May-2019 16:21:50
Advertisements