JFrame window size too big for screen?

Viewed 1392

Okay so i've tried finding an answer for this pretty much all day but can't seem to find any. :/

Using JFrame, i've been trying to set a window to 1920x1080 resolution but when i try doing this it seems to use my whole screen as a window, kinda like my screen resolution is 1920x1080 but it's 3840x2160, why is this? When i run another application with a 1920x1080 resolution window it seems to not be the same size. (it's smaller)

Current code:

JFrame frame = new JFrame("Test");
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
frame.setSize(width, height);
System.out.println(width+" "+height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);

The output from this is a window covering my screen tho a little off at some parts and then in the console it says 3840 2160. If i change the frame.setSize(width, height); from the current to frame.setSize(1920, 1080); it still makes the window the same size. I think the maximum is a little below 1920x1080 for some reason. :/ Any ideas here?

EDIT: It seems using frame.setSize(Toolkit.getDefaultToolkit().getScreenSize()); makes the window size 1536x864 and not 3840x2160, setting it any higher than 1536x864 makes no difference, it'll say the size is bigger but i can't see any difference, it covers the screen anyway.

3 Answers

If you want the frame to fill the desktop then use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

This will make the frame the appropriate size so it doesn't cover the task bar.

If you want the frame to cover the screen including the taskbar then you can use:

frame.setSize( Toolkit.getDefaultToolkit().getScreenSize() );
frame.setVisible( true );

Try using the following instead of frame.setSize(...):

frame.setPreferredSize(new Dimension(1920, 1080));

Thats because your windows scaling is not set to 100%.

Related