Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 15: Using java.awt and javax.swing to Create GUI Applications

Up to this point, the Java programs have accepted input from and written output to the Eclipse Console window. Java provides a number or robust libraries for creating Graphical User Interfaces (GUIs). These include the java.awt.*, javax.swing.*, javafx.*, org.eclipse.swt.* libraries.

Java GUI Basic Concepts

Before you can begin creating a Java GUI application you need to understand a few basic GUI concepts, such as frames, components (fields, labels, etc.), panels, layout managers, and coordinates:

Frames
The window (GUI) where your program’s components are held is called the Frame.
Components
Components consist of: text fields, radio buttons, labels, and menus. Components are all contained within the frame. Every Java application runs within a window and every window needs a frame where various components can "live".
Panels
The panel is the content pane is where all of your programs components are placed. You can think of the content pane as a background and the various components of your program are placed onto this background.
Layout Managers
The layout manager aligns components within content pane into a specific style depending on what layout you choose (BorderLayout, BoxLayout, CardLayout, FlowLayout, GridBagLayout, GridLayout, GroupLayout, SpringLayout, etc.). Certain applications are well suited for using a layout manager since they can make component placement much easier, but they can also create additional complications not typically experienced when you set your layout manager to null and assign exact coordinates for every component.
Coordinates
Using coordinates is important when you are not using a layout manager. Coordinates are used to specify where components are placed on the content pane. Coordinates are described just like x-y graphs from high school math class (X, Y). The (0, 0) coordinate is the top left corner of the pane. You can specify the exact position of components in your program by setting these coordinates.

Figure 15-1: Java GUI Terminology

The java.awt and java.swing Libraries

To create a Java GUI application using the java.awt and java.swing libraries you first need to import the packages you need from each of them. Then you can add a layout (in this example BorderLayout) and components. Finally, you need to add the panes to the frame and make the frame using frame.setVisable(true). This creates the pop-up GUI application.

package edu.buffalostate.cis425.sp18.guide.your-last-name;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyFirstFrame {

  public static void main(String[] args) {
    JFrame frame = new JFrame(); // Create the window frame
    frame.getContentPane().setBackground(Color.white);
    frame.getContentPane().setForeground(Color.white);
    frame.setForeground(Color.white);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("My First Frame");

    JPanel bluePanel = new JPanel();
    JPanel redPanel = new JPanel();

    JLabel label = new JLabel(" <-- pick your side --> ");

    frame.getContentPane().add(label, BorderLayout.CENTER);

    bluePanel.setBackground(Color.blue);
    redPanel.setBackground(Color.red);

    frame.getContentPane().add(bluePanel, BorderLayout.LINE_START);
    frame.getContentPane().add(redPanel, BorderLayout.LINE_END);

    JButton blueButton = new JButton("PICK BLUE TEAM");
    JButton redButton = new JButton("PICK RED TEAM");

    bluePanel.add(blueButton);
    redPanel.add(redButton);

    frame.pack();
    frame.setVisible(true);
  }
}

Figure 15-2: MyFirstFrame.java Using BorderLayout Code

Here is a list of Java background and foreground colors.

MyFirstFrame

Figure 15-3: MyFirstFrame.class Output
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2018 Jim Gerland