How can we disable the maximize button of a JFrame in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A JFrame is a class from javax. swing package and it can extend java.awt.frame class. It is a top-level window with a border and a title bar. A JFrame class has many methods that can be used to customize it.

After setting the size of a JFrame we can still change the size by putting the cursor at the corners and dragging it or if we press resize option next to close at the top right corner, it will maximize to the size of a full screen. This happens because resize is set to true by default for JFrame class. We can make it as false as setResizable(false), now it can appear according to the dimensions we have given in the code and it cannot be resizable.

Example

import java.awt.*;
import javax.swing.*;
public class JFrameDemo extends JFrame {
   String title;
   public JFrameDemo(String title) {
      super(title);
      add(new JLabel("JFrame with maximize button disabled"), BorderLayout.CENTER);
      setSize(350, 275);
      setLocationRelativeTo(null);
      setResizable(false); // maximize button disable
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JFrameDemo("JFrame Demo");
   }
}

Output

raja
Published on 09-Jul-2019 17:50:57
Advertisements