Lab 6: Introduction to Graphical User Interfaces
CS 141: Introduction to Programming , Fall 2015


This lab is due Monday, Oct 19, by the end of the lab,

In this lab, you will learn two ways to create a graphical user interface (GUI): By writing the code directly (Part 2) and by using the Netbeans GUI Builder (Part 1). Feel free to do these in whichever order you prefer. After completing Parts 1 and 2, you will create a GUI for your Make Change program.

The Assignment

The goal of this lab is for you to create a GUI for a simplified version of your Make Change program as displayed here. 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.

However, before attempting this assignment, you need to learn about how to make GUIs. To do this, work through Parts 1 and 2 below. 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 to 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.

Deliverables

To receive full credit, you need to:

  1. Make sure that your Java code:

  2. Demonstrate your program to the instructor or lab assistant during the lab on the due date. You will be asked to display your commented/formatted code and to run the code on a few inputs.

  3. Submit your code as attachments to Lab 6 in Wise.



Part 1: Using the Netbeans GUI Builder:
Temperature Conversion

The two videos below show how to create a temperature conversion program using the Netbeans GUI Builder:

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 below where you create an interface by hand. However, you may prefer to do this part first.

Part 2: Creating a GUI by Hand

Creating a Window

Adding Components

Adding Actions

Complete Code (not modularized)

The complete final code for the above is here:


import java.awt.Color;
import java.awt.event.ActionEvent;
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 myButton1;
    private JButton myButton2;
    private JTextField myTextField;

    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("Here is message 1");
        add(messageLabel1, java.awt.BorderLayout.NORTH);
        messageLabel1 = new JLabel("Here is message 2");
        add(messageLabel1, java.awt.BorderLayout.SOUTH);

        myButton1 = new JButton("My green red.");
        myButton1.setBackground(Color.red);
        add(myButton1, java.awt.BorderLayout.EAST);

        myButton2 = new JButton("My green button.");
        myButton2.setBackground(Color.green);
        add(myButton2, java.awt.BorderLayout.CENTER);
        myButton2.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()));
                myButton2.setBackground(color);
            }
        });

        myTextField = new JTextField("This is a text field");
        add(myTextField, java.awt.BorderLayout.WEST);
        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 myButton1;
    private JButton myButton2;
    private JTextField myTextField;

    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("Here is message 1");
        add(messageLabel1, java.awt.BorderLayout.NORTH);
        messageLabel1 = new JLabel("Here is message 2");
        add(messageLabel1, java.awt.BorderLayout.SOUTH);

        myButton1 = new JButton("My green red.");
        myButton1.setBackground(Color.red);
        add(myButton1, java.awt.BorderLayout.EAST);

        myButton2 = new JButton("My green button.");
        myButton2.setBackground(Color.green);
        add(myButton2, java.awt.BorderLayout.CENTER);
        myButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myButton2ActionPerformed(evt);
            }
        });

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

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

} 

Component Interaction