How to Maximize and Minimize Jframe Window
In this tutorial, we will learn how to maximize and minimize JFrame window. We will also see how to do this with button clicks.
To maximize a JFrame window, you can use the setExtendedState
method and pass in the JFrame.MAXIMIZED_BOTH
constant as the argument.
Maximize Jframe
For example:
JFrame frame = new JFrame(); // maximize the frame frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // show the frame frame.setVisible(true);
You can also use the setUndecorated
method and pass in true
as the argument to remove the frame’s border and title bar, and then use the setBounds
method to set the frame’s size to the size of the screen. For example:
JFrame frame = new JFrame(); // remove the frame's border and title bar frame.setUndecorated(true); // get the screen dimensions Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // set the frame's size to the screen size frame.setBounds(0, 0, screenSize.width, screenSize.height); // show the frame frame.setVisible(true);
Minimize JFrame Window
To minimize a JFrame window, you can use the setExtendedState
method and pass in the JFrame.ICONIFIED
constant as the argument. For example:
JFrame frame = new JFrame(); // minimize the frame frame.setExtendedState(JFrame.ICONIFIED);
Note that this will minimize the frame, but it will not hide it. To hide the frame completely, you can use the setVisible
method and pass in false
as the argument. For example:
JFrame frame = new JFrame(); // minimize and hide the frame frame.setExtendedState(JFrame.ICONIFIED); frame.setVisible(false);
Alternatively, you can use the setState
method and pass in the Frame.ICONIFIED
constant as the argument to minimize the frame. For example:
JFrame frame = new JFrame(); // minimize the frame frame.setState(Frame.ICONIFIED);
Maximize and Minimize JFrame Window with button click:
To maximize and minimize a JFrame window with a button click, you can create a JButton
and add an action listener to it that toggles the frame’s extended state between JFrame.MAXIMIZED_BOTH
and JFrame.NORMAL
. For example:
JFrame frame = new JFrame(); JButton button = new JButton("Toggle Maximize"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (frame.getExtendedState() == JFrame.MAXIMIZED_BOTH) { frame.setExtendedState(JFrame.NORMAL); } else { frame.setExtendedState(JFrame.MAXIMIZED_BOTH); } } }); frame.add(button, BorderLayout.NORTH); frame.setVisible(true);
In order to minimize and restore the frame with a button click, you can use the same approach, but toggle the extended state between JFrame.ICONIFIED
and JFrame.NORMAL
. For example:
JFrame frame = new JFrame(); JButton button = new JButton("Toggle Minimize"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (frame.getExtendedState() == JFrame.ICONIFIED) { frame.setExtendedState(JFrame.NORMAL); } else { frame.setExtendedState(JFrame.ICONIFIED); } } }); frame.add(button, BorderLayout.NORTH); frame.setVisible(true);
Hide the JFrame window completely
Note that this will minimize the frame, but it will not hide it. To hide the frame completely, you can use the setVisible
method and pass in false
as the argument in the action listener. For example:
JFrame frame = new JFrame(); JButton button = new JButton("Toggle Minimize"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (frame.isVisible()) { frame.setExtendedState(JFrame.ICONIFIED); frame.setVisible(false); } else { frame.setExtendedState(JFrame.NORMAL); frame.setVisible(true); } } }); frame.add(button, BorderLayout.NORTH); frame.setVisible(true);
Leave a Reply