Java Graphical User Interfaces (GUIs)

A simple example. The main class:


import javax.swing.*;
import java.awt.Color;
import java.awt.GridLayout;

public class SimpleGUI {

	public static void main(String[] args) {
		// We need a top-level container, such as
		// JFrame
		JFrame frame = new JFrame();
		frame.setSize(300, 300);
		// When you click the red cross in the right corner,
		// it exits:
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Counter clickCount = new Counter(); // the counter contains the internal 
											// state: the number of clicks
				
		JLabel instructions = new JLabel();
		instructions.setText("   Click this button here:");
		instructions.setOpaque(true); // need this for a label so that the color shows
		instructions.setBackground(Color.white); // the default is greyish 
		 
		
		JLabel clickCountLabel = new JLabel("   No clicks yet");
		clickCountLabel.setOpaque(true);
		clickCountLabel.setBackground(Color.yellow);
		
		// Create a button
		JButton button = new JButton("Click me!");
		button.setBackground(Color.yellow);
		button.setForeground(Color.red);
		// pass to the listener all the needed references:
		button.addActionListener(new ClickListener(clickCountLabel, clickCount));
		button.setSize(100, 100);
		
		// Create a button
		JButton resetButton = new JButton("Reset");
		// pass to the listener all the needed references:
		resetButton.addActionListener(new ResetListener(clickCountLabel, 
										clickCount));
		resetButton.setSize(100, 100);
		resetButton.setBackground(Color.white);
		
		// components are added to a panel, in this 
		// case the contentPane of the JPanel
		JPanel panel = (JPanel) frame.getContentPane();
		
		// there are many layouts to chose from. 
		// Grid is the best for tables and other grids
		GridLayout grid = new GridLayout(2, 2);
		// setting the layout for the panel:
		panel.setLayout(grid);
		
		// adding all the components to the next available space 
		// according to the layout:
		panel.add(instructions); 
		panel.add(button); 
		panel.add(clickCountLabel);
		panel.add(resetButton);

		//frame.pack(); // is used if you want to make the frame just
						// small enough to contain the components 
		// this is important: 
		frame.setVisible(true);		
	}

}

The class that handles the internal state:



public class Counter {
	private int count = 0;
	
	public void incrementCounter() {
		count++;
	}
	
	public int getCount() {
		return count;
	}

	public void resetCount() {
		count = 0;
	}
}

The listeners for the two buttons:



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;


public class ClickListener implements ActionListener {
	private JLabel theResult; 
	private Counter theCounter;
	
	public ClickListener(JLabel theResultLabel, Counter clickCount) {
		theResult = theResultLabel;
		theCounter = clickCount;
	}
	
	public void actionPerformed(ActionEvent event) {
		theCounter.incrementCounter();
		theResult.setText("   " + theCounter.getCount() + " clicks.");
	}
}


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;


public class ResetListener implements ActionListener {
	private JLabel theResult; 
	private Counter theCounter;

	public ResetListener(JLabel theResultLabel, Counter clickCount) {
		theResult = theResultLabel;
		theCounter = clickCount;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		theCounter.resetCount();
		theResult.setText("   Counter reset");
	}

}

CSci 2101 course web site.