How to set the maximized bounds for a Frame in Java?


To set maximized bounds, use the setMaximizedBounds() method. Here, we will create a frame, which when maximized will form a shape −

JFrame frame = new JFrame("Login!");

Above, we have created a frame and now we will use the Rectangle class to specify an area of coordinates −

Rectangle bounds = new Rectangle(50, 10, 100, 200);
Now, set maximized bounds −
frame.setMaximizedBounds(bounds);

The following is an example to set the maximized bounds for a frame −

Example

package my;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Login!");
      JLabel label1, label2, label3;
      frame.setLayout(new GridLayout(2, 2));
      label1 = new JLabel("DeptId", SwingConstants.CENTER);
      label2 = new JLabel("SSN", SwingConstants.CENTER);
      label3 = new JLabel("Password", SwingConstants.CENTER);
      JTextField deptid = new JTextField(20);
      JTextField ssn = new JTextField(20);
      JPasswordField passwd = new JPasswordField();
      passwd.setEchoChar('*');
      frame.add(label1);
      frame.add(label2);
      frame.add(label3);
      frame.add(deptid);
      frame.add(ssn);
      frame.add(passwd);
      Rectangle bounds = new Rectangle(50, 10, 100, 200);
      frame.setMaximizedBounds(bounds);
      frame.setSize(550, 200);
      frame.setVisible(true);
   }
}

Output

Now, maximize it and you can see the rectangle bounds set above −

Updated on: 30-Jul-2019

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements