Why does the JFrame window size in Java not determine the window size?

I was just messing around trying to make a game right now, but I also had this problem. When I specify a specific window size (for example, 1024 x 768), the created window is slightly larger than what I specified. Very annoying. Is there a reason for this? How to fix this so that the created window is actually the size I want, and not just a little bigger? Until now, I always went back and manually resized several pixels at a time until I got the result that I need, but it is out of date. If there was even a formula that I could use, it would tell me how many pixels I would need to add / subtract from my variable, which would be excellent!

PS I do not know if my OS can be a factor in this, but I use W7X64.

private int windowWidth = 1024; private int windowHeight = 768; public SomeWindow() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(windowWidth, windowHeight); this.setResizable(false); this.setLocation(0,0); this.setVisible(true); } 
+4
source share
2 answers

I want the overall frame of Windows to create the exact size that I specify

I do not understand your problem. Submit your SSCCE , which shows the problem.

If I run code like:

 frame.setResizable(false); frame.setSize(1024, 768); frame.setVisible(true); System.out.println(frame.getSize()); 

It displays java.awt.Dimension [width = 1024, height = 768], is that not what you expect?

If there was even a formula, I could use that will tell me how many pixels I needed to add / subtract from my mine a variable that would be excellent!

Perhaps you mean the space occupied by the header and borders?

 import java.awt.*; import javax.swing.*; public class FrameInfo { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle bounds = env.getMaximumWindowBounds(); System.out.println("Screen Bounds: " + bounds ); GraphicsDevice screen = env.getDefaultScreenDevice(); GraphicsConfiguration config = screen.getDefaultConfiguration(); System.out.println("Screen Size : " + config.getBounds()); JFrame frame = new JFrame("Frame Info"); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setSize(200, 200); System.out.println("Frame Insets : " + frame.getInsets() ); frame.setVisible( true ); System.out.println("Frame Size : " + frame.getSize() ); System.out.println("Frame Insets : " + frame.getInsets() ); System.out.println("Content Size : " + frame.getContentPane().getSize() ); } } 
+5
source

When you say that the size of the resulting window is not set, are you talking about this window with its decorations?

Indeed, the size of the window is determined without special window design.

Try to add

 this.setUndecorated(true); 

before

 this.setVisible(true); 
0
source

Source: https://habr.com/ru/post/1304123/


All Articles