Lab 5: Introduction to Graphical User Interfaces
CS 141: Introduction to Programming , Fall 2017


This lab will not be collected, but should be completed by lab on Monday, Oct 9

In this lab, you will learn two ways to create a graphical user interface (GUI): By writing the code directly (Part 1) and by using the Netbeans GUI Builder (Part 2).

For more practice, after completing Parts 1 and 2, you can try to create a GUI for your Make Change program.

Even though this lab will not be collected, it is important to complete because you will need to know how to create GUIs for future labs.

Reading in your Text



Part 1: Creating a GUI by Hand

Writing the GUI code directly is a lot more work than using the Netbeans GUI Builder as is done in Part 2. However, in order to understand what the GUI builder is doing, you need to build the code yourself at least initially.

Creating a Window

Adding Components

Adding Action Event Listeners

Complete Code (not modularized)

The complete final code for the above is here:


import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyJFrame extends JFrame {

    private JLabel messageLabel1;
    private JLabel messageLabel2;
    private JButton myButtonRed;
    private JTextField myTextField;
    private JButton myButtonGreen;

    public MyJFrame(String title) {
        super(title);
        setSize(400, 300);   // sets the size of the window     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Closing the window will stop the program.
        messageLabel1 = new JLabel("This is a message 1");
        add(messageLabel1, java.awt.BorderLayout.PAGE_START);
        messageLabel2 = new JLabel("This is a message 2");
        add(messageLabel2, java.awt.BorderLayout.PAGE_END);

        myButtonRed = new JButton("My Red Button");
        myButtonRed.setBackground(Color.red);
        add(myButtonRed, java.awt.BorderLayout.LINE_END);

        myButtonGreen = new JButton("Click Here!");
        myButtonGreen.setBackground(Color.green);
        add(myButtonGreen, java.awt.BorderLayout.CENTER);
        myButtonGreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Color color = new Color((int) (256 * Math.random()), (int) (256 * Math.random()), (int) (256 * Math.random()));
                myButtonGreen.setBackground(color);
            }
        });

        myTextField = new JTextField("This is a text field");
        add(myTextField, java.awt.BorderLayout.LINE_START);

        setVisible(true);
    }
}

Complete Code (modularized)

The Netbeans GUI Builder organizes its code into separate methods (just like you did for your Expert system). Let's rearrange the above code to better match what you will see in the GUI Builder. First, we create a private method called initComponents where all of the components are initialized. This method is called from the JFrame constructor. Next, we move the contents of myButton2's actionPerformed method into a separate private method called myButton2ActionPerformed. The result is closer to (but not exactly equal to) what you will see in the GUI Builder. Carefully study how things were moved around. No new functionality has been added, rather instructions have just been moved around. New method calls are in red. The new method headers are in green.



import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyJFrame extends JFrame {

    private JLabel messageLabel1;
    private JLabel messageLabel2;
    private JButton myButtonRed;
    private JTextField myTextField;
    private JButton myButtonGreen;

    public MyJFrame(String title) {
        super(title);
        setSize(400, 300);   // sets the size of the window   
        initComponents();
        setVisible(true);
    }

    private void initComponents() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Closing the window will stop the program.
        messageLabel1 = new JLabel("This is a message 1");
        add(messageLabel1, java.awt.BorderLayout.PAGE_START);
        messageLabel2 = new JLabel("This is a message 2");
        add(messageLabel2, java.awt.BorderLayout.PAGE_END);

        myButtonRed = new JButton("My Red Button");
        myButtonRed.setBackground(Color.red);
        add(myButtonRed, java.awt.BorderLayout.LINE_END);

        myButtonGreen = new JButton("Click Here!");
        myButtonGreen.setBackground(Color.green);
        add(myButtonGreen, java.awt.BorderLayout.CENTER);
        myButtonGreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myButtonGreenActionPerformed(evt);
            }
        });

        myTextField = new JTextField("This is a text field");
        add(myTextField, java.awt.BorderLayout.LINE_START);

    }

    private void myButtonGreenActionPerformed(java.awt.event.ActionEvent evt) {
        Color color = new Color((int) (256 * Math.random()), (int) (256 * Math.random()), (int) (256 * Math.random()));
        myButtonGreen.setBackground(color);
    }

}

Component Interaction



Part 2: Using the Netbeans GUI Builder:
Temperature Conversion

The two videos below show how to create a temperature conversion program using the Netbeans GUI Builder. These are in quicktime format (*.mov) so you should be able to right-click on the link and Save Link As. Once you have downloaded the file, open it up using the Quicktime Player.

It is suggested that you follow along in Netbeans as you watch the video. The video makes reference to some of the concepts discussed in the sections above where you created an interface by hand.

Suggested Assignment (not to hand in)

The goal of this part of the lab is to give you practice applying the above concepts by creating a GUI for your Make Change program. The window might look like the picture below. The user enters an amount, presses the compute button, and the program calculates and diplays the number of dollars and coins cooresponding the amount entered.

Before attempting this assignment, be sure you have worked through Parts 1 and 2 above. Once you are familiar with creating a GUI you can begin work on your Make Change program.
Make Change Program - you will need:
8 labels, 1 textfield, and 1 button. Add an action listener for the button. The calculations can be done in the ActionPerformed method for the button. Please remember to rename your components when using the GUI Builder.