Example Java Code For Building a Simple GUI Application

java script
Degui Adil / EyeEm / Getty Images

 A GUI -- Graphical User Interface -- of an application built using Java is made up of layers of containers. The first layer is the window used to move the application around the screen of your computer. It is a top-level container that gives all other containers and graphical components a place to work in. For a desktop application, this top-level container is usually made using the JFrame class.

01
of 02

Background

How many layers a GUI has depends on your design. You can place graphical components such as text boxes, labels, and buttons directly into the JFrame, or they can be grouped in other containers depending on how complex the application GUI needs to be. 

This sample code below shows how to build an application out of a JFrame, two JPanels and a JButton, which determines the visibility of the components held in the two JPanels. Follow along with what is happening in the code by reading the implementation comments, indicated by two slashes at the beginning of each comment line.

This code goes with the Coding a Simple Graphical User Interface - Part I step-by-step guide. It shows how to build an application out of a JFrame, two JPanels and JButton. The button determines the visibility of the components held within the two JPanels.

02
of 02

Java Code

Business team at computer
Comstock/Stockbyte/Getty Images

Compare this Java code with program listing generated from the Coding a Simple Graphical User Interface - Part II which uses the NetBeans GUI Builder to create the same GUI application.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiApp1 {
//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {
new GuiApp1();
}
public GuiApp1()
{
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Example GUI");
guiFrame.setSize(300,250);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//Options for the JComboBox
String[] fruitOptions = {"Apple", "Apricot", "Banana"
,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"};
//Options for the JList
String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage"
, "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
, "Pepper", "Radish", "Shallot", "Spinach", "Swede"
, "Turnip"};
//The first JPanel contains a JLabel and JCombobox
final JPanel comboPanel = new JPanel();
JLabel comboLbl = new JLabel("Fruits:");
JComboBox fruits = new JComboBox(fruitOptions);
comboPanel.add(comboLbl);
comboPanel.add(fruits);
//Create the second JPanel. Add a JLabel and JList and
//make use the JPanel is not visible.
final JPanel listPanel = new JPanel();
listPanel.setVisible(false);
JLabel listLbl = new JLabel("Vegetables:");
JList vegs = new JList(vegOptions);
vegs.setLayoutOrientation(JList.HORIZONTAL_WRAP);
listPanel.add(listLbl);
listPanel.add(vegs);
JButton vegFruitBut = new JButton( "Fruit or Veg");
//The ActionListener class is used to handle the
//event that happens when the user clicks the button.
//As there is not a lot that needs to happen we can
//define an anonymous inner class to make the code simpler.
vegFruitBut.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
//When the fruit of veg button is pressed
//the setVisible value of the listPanel and
//comboPanel is switched from true to
//value or vice versa.
listPanel.setVisible(!listPanel.isVisible());
comboPanel.setVisible(!comboPanel.isVisible());
}
});
//The JFrame uses the BorderLayout layout manager.
//Put the two JPanels and JButton in different areas.
guiFrame.add(comboPanel, BorderLayout.NORTH);
guiFrame.add(listPanel, BorderLayout.CENTER);
guiFrame.add(vegFruitBut,BorderLayout.SOUTH);
//make sure the JFrame is visible
guiFrame.setVisible(true);
}
}
Format
mla apa chicago
Your Citation
Leahy, Paul. "Example Java Code For Building a Simple GUI Application." ThoughtCo, Feb. 16, 2021, thoughtco.com/example-java-code-for-building-a-simple-gui-application-2034066. Leahy, Paul. (2021, February 16). Example Java Code For Building a Simple GUI Application. Retrieved from https://www.thoughtco.com/example-java-code-for-building-a-simple-gui-application-2034066 Leahy, Paul. "Example Java Code For Building a Simple GUI Application." ThoughtCo. https://www.thoughtco.com/example-java-code-for-building-a-simple-gui-application-2034066 (accessed March 28, 2024).