Java Swing was created for large-scale enterprise development of Java applications with a graphical user interface. The GUI components add graphics and interactivity to a Java program.
The graphical components used for Java Swing are contained in predefined classes in the javax.swing package. The classes in your application will utilize inheritance to customize the skeleton of the Java Swing components. These classes include JButton, JTextField, JTextArea, etc to create your GUI.
JFrame
The heart of a Java Swing application is the JFrame. All the components for the GUI go onto the JFrame and the JFrame is the window that appears on your screen. It can be as simple as a window with no controls or as robust as your Chrome window with options to minimize, close and maximize, set a title and open in a default size.
The JFrame will ‘hold’ all the other objects that we need to make the GUI. Remember those old science fair display boards you made in middle school? You can think of a JFrame to a science fair display board where you would add pictures and titles to explain your science fair project.
As you work through all these components to create a Java Swing program, we will be creating a Madlib program. The end project will look like this:
To create your first JFrame, create a new Java class called StartProgram.java with a main method. Add the following lines of code:
public static void main(String[ ] args) {
JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setTitle("My First JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Line 2 creates a new instance of the JFrame object. Note that your IDE will import the javax.swing.JFrame; package into your program.
Line 3 sets the size of the frame. Without this, the frame will look a little smaller than we would prefer.
Line 4 sets the title. This is what appears on the line with the close, minimize, maximize buttons.
Line 5 makes the program quit as soon as you close the window. If you don’t have this, you will find several instances of your program running in the task manager (Windows) or in your Dock or Force Quit Applications menu (Mac) for each time that you launch your program to test.
Line 6 finally makes it visible. If you forget this, you’ll never see it appear on your screen.
Right now, this frame is boring. Let’s add a button.
Add the following code before frame.setVisible(true); statement.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
frame.add(button);
frame.setVisible(true);
Run your application and it should look like a giant button has taken over your JFrame. The button does not do anything besides look cool at this time. It just takes over the whole JFrame. It can be controlled later. What if we wanted to add a second button?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
frame.add(button);
JButton button1 = new JButton("No! Click me! I’m better!");
frame.add(button1);
frame.setVisible(true);
Where did the other button go?
It’s there, but it’s under the second button. You are now experiencing one of the limitations with a JFrame. In its basic state, it can only display one component at a time. This, of course, can be modified by setting sizes and/or using a layout manager, but there is another container that easily supports more components added to it and formats them by flowing from one component to another.