Of course there are always different ways of doing things.
SwingUtilities.invokeLater(new JFrameExample()) īut that can be a little hard for a new Java developer to deal with, so I left it as shown above.
To make the code smaller I could reduce the main method to look like this: Note that the proper way to display a JFrame is by using the SwingUtilities invokeLater method to make sure this "job" is placed on the Event Dispatch Thread (EDT). The Auto Size option automatically adjusts the size of the desktop session display to fit the client computer screen. See Section 3.1, Display Tab for more details about the available settings. Those are pretty much the minimal lines of code you need to construct and display a JFrame that (a) has the size you intended and (b) the user can close by selecting a quit/exit operation. You set the display size of the desktop session using the settings on the Display tab. JFrame frame = new JFrame("My JFrame Example") įtDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) įtPreferredSize(new Dimension(400, 200)) schedule this for the event dispatch thread (edt) We have to make sure the window is visible because it starts initially as invisible. Now, we just need to make sure the window shows up. This will set the window to 800圆00 pixels. JFrameExample example = new JFrameExample() The Java JFrame has a method called setSize which allows you to say how many pixels wide and how many pixels long the window should be. Public class JFrameExample implements Runnable
This example shows how to construct a JFrame, and make sure it's properly displayed using the SwingUtilities invokeLater method: To that end, here is the source code for a simple "JFrame example" demo class. Here are two examples that show the correct technique.
As well the the reported behavior, the window reacts strangely when attempting to move it to the second monitor using keybinds. Max Kamps added a comment - 17/Jun/17 1:22 PM This is still an issue in version 1.12 on GNOME 3. This morning when I saw some Java JFrame code on a mailing list, it made me think that I needed to put a simple JFrame example out here, something that would show how to properly construct and display a JFrame without getting into a discussion of anything else. The only way to regain mouse control is to use alt-tab and un-fullscreen Minecraft.