JFrame - Part 2
So far you have created a simple JFrame. What you have done so far is legitimate, well created code but there are a few more lines to add into the previous program to make it perfect.
The reason they were omitted from the first example is that all of these lines are semi-optional. There's a good reason for putting each in, but if you have a better reason to leave them out, do so.
Here's the new version of the first example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| import javax.swing.*;
public class FrameExample_Extended{
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] Hello World [=]");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
|
There are three main lines here that have been edited from the previous code. Let's go through each in turn.
8
| JFrame.setDefaultLookAndFeelDecorated(true);
|
This line turns on the default 'Look and Feel' of Java.
Compare the two styles using these pictures.


The first picture is the first JFrame example running on WindowsXP. As you can see, this looks very like the operating system style. We say this looks 'native' to the operating system.
The second picture is the first JFrame example with the 'Look and Feel' set to true. This GUI looks different from the native version previously, but will look like that no matter what operating system we run the program on.
This has both it's advantages and disadvantages, but normally you would set the look and feel to true when developing Swing applications. This is so we get a consistent GUI, with consistent widgets with consistent sizes and positions no matter what operating system we run the program on.
14
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
The second line entered to the code is there to make sure that on pressing the close button, the program exits. Other behaviours can be used to catch the program before it actually exits. For example, you may simply want to hide the screen instead of closing the whole application.
frame.pack() is used to size the JFrame automatically to the size of the widgets within the page. We set this manually last time using frame.setSize(400,100) to create a window of 400px by 100px. You will see though, if we run this program above us, the size of the window is tiny.

This is due to their being no widgets to display in the JFrame, so the JFrame is sized as small as possible without getting rid of any of the buttons on the bar.
frame.pack() is used when LayoutManagers are put in place, as a minimum size is used for many of the widgets (we do this using .setMinimumSize()). Until you know how to use a LayoutManager, stick to manually setting the size of the frame using .setSize().
The teaching segment of JFrames is complete. From this tutorial, you should know how to create a JFrame within Java and edit many of the simple attributes that control what is shown on the screen.
You should also know some of the theory behind Swing GUIs, such as the 'Look and Feel' and being 'Thread-Safe'.

Take a quick test on JFrames.
The next tutorial looks at placing JPanels on our new JFrame.