Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFrame.setExtendedState doesn't actually maximise

public static void main(String args[]){
    JFrame frame = new JFrame();
    frame.setExtendedState(JFrame.MAXIMISED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

I've used this code to maximise a JFrame, but instead of actually maximising the frame, it just sets the window size to that of the screen, without actually changing the state, so clicking the maximize button doesn't actually downscale it again.

Am I using the wrong command or something?

like image 420
Shaun Wild Avatar asked May 13 '26 22:05

Shaun Wild


2 Answers

You have an error in frame.setExtendedState(JFrame.MAXIMISED_BOTH);

You should write frame.setExtendedState(JFrame.MAXIMIZED_BOTH); instead

like image 149
Jesus Flores Avatar answered May 15 '26 11:05

Jesus Flores


This worked for me:

We need to combine the setSize () and setExtendedState together JFrame frame=new JFrame();

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // aligns itself with windows task bar
// set maximum screen   
frame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
like image 28
Ajith Moni Avatar answered May 15 '26 13:05

Ajith Moni