The Basics of Making a GUI in Java
Get Started with Making Graphical User Interfaces in the Java Programming Language
Let's start with the basics: creating a window. I'll go over the following code line by line (sorry about the lack of code formatting; AssociatedContent removes space at the beginning of a line):
import javax.swing.*;
public class GUIFun
{
public static void main(String[] args) { new GUIFun(); }
public GUIFun()
{
JFrame f = new JFrame();
f.setSize(600, 600);
f.setTitle("My Window!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
The first line creates a new JFrame object. A JFrame is a Container that represents a window. On the next line, we set the size of our window, in pixels, and after that tell the window what to say in the bar on the top. The next line tells the window that if someone closes it, end the program. If you don't have this line, the program will continue to run after the window has been closed.
If you want the program to continue running, there are other options. One is JFrame.HIDE_ON_CLOSE, which closes the window but doesn't end the program, JFrame.DISPOSE_ON_CLOSE, which closes the window and disposes it so that any memory taken up by the frame is freed, and JFrame.DO_NOTHING_ON_CLOSE, which tells the window to stay open even if the user tries to close it.
After you finish setting all these properties, we can show the window with the setVisible(true) command. Run this code, and you will see a blank window. Let's make our window more interesting by adding a button:
import javax.swing.*;
public class GUIFun
{
public static void main(String[] args) { new GUIFun(); }
public GUIFun()
{
JFrame f = new JFrame();
f.setSize(600, 600);
f.setTitle("My Window!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton helloButton = new JButton("Hello");
f.add(helloButton);
f.setVisible(true);
}
}
Compile and run this code. What do you see? You should see a giant button that fills up the entire window, which does nothing when clicked. This is because a JFrame can only hold one Component/Container at a time, so it is stretched to fill the entire JFrame. The JButton does nothing because we haven't yet told the button to do anything when clicked. We can fix the first problem by introducing another Container: the JPanel. A JPanel is an "invisible" Container, because unlike the JFrame, you cannot actually see the JPanel itself, but you can see all of the Components it contains.
import javax.swing.*;
public class GUIFun
{
public static void main(String[] args) { new GUIFun(); }
public GUIFun()
{
JFrame f = new JFrame();
f.setSize(600, 600);
f.setTitle("My Window!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton helloButton = new JButton("Hello");
JButton goodbyeButton = new JButton("Goodbye");
JPanel panel = new JPanel();
panel.add(helloButton);
panel.add(goodbyeButton);
f.add(panel);
f.setVisible(true);
}
}
You might have noticed that I slipped in another JButton. That's there to show you that the JPanel can hold as many Components that you like.
Now, let's make those buttons actually do something. We can accomplish this with an "ActionListener". An ActionListener is an interface that includes only one method: actionPerformed(ActionEvent event). When you create a class that implements ActionListener, you can add it to a Component, and when that Component is interacted with, the Component calls the actionPerformed method of all the ActionListeners that have been added to it. If you have an ActionListener that handles several different Components, you can give each Component an "action command", which can be used to distinguish between the Components. Here is the final version of our class:
import java.awt.event.*;
import javax.swing.*;
public class GUIFun implements ActionListener
{
private JLabel messageLabel;
private JFrame f;
public static void main(String[] args) { new GUIFun(); }
public GUIFun()
{
f = new JFrame();
f.setSize(600, 600);
f.setTitle("My Window!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
messageLabel = new JLabel("Hello");
JButton helloButton = new JButton("Hello");
helloButton.addActionListener(this);
helloButton.setActionCommand("hello");
JButton goodbyeButton = new JButton("Goodbye");
goodbyeButton.addActionListener(this);
goodbyeButton.setActionCommand("goodbye");
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(this);
quitButton.setActionCommand("quit");
panel.add(messageLabel);
panel.add(helloButton);
panel.add(goodbyeButton);
panel.add(quitButton);
f.add(panel);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("hello"))
{
messageLabel.setText("Hello");
f.repaint();
}
else if (actionCommand.equals("goodbye"))
{
messageLabel.setText("Goodbye");
f.repaint();
}
else
{
System.exit(0);
}
}
}
Published by Nicholas Wuensch
- Computer Programming - Then and NowDescribes the history of computer programming language. The job of the computer programmer, and future career prospects.
- The Joy of Making a CandleCandle making is a hobby for some people. For others it is a business. The important thing is to be safe and have fun.
- C Programming HOW-TOThis is a mini guide for anyone who wants to remember some aspects of C programming.
- The History of Swing MusicA History and Analysis of Swing Music
- Wooden Swing Sets Versus Metal Swing SetsThere are a wide variety of different swing style styles available for purchase. There are many parents and children who are overwhelmed with all of the choices.
- A Short Introduction to Java Programming Language
- Evolution of Graphical User Interface
- Flex 2: Software Development Kit for Rich Internet Applications Aimed for Flash Pl...
- Make Windows Look like a Mac
- A Review of Alice: A Java Learning Program and Its Creator Randy Pausch
- Features of Object Oriented Programming
- Advantages of Object-Oriented Programming
- A JFrame is a Container that represents a window, and can only contain one Component/Container.
- A JPanel is a Container that can hold multiple Components.
- Add ActionListeners to your Components for interactivity.



