This is the sixth tutorial that will show you how to create graphical user interfaces using java.
Prerequisites
You should know how to create a window in Java.
LINK GOES HERE
You should know how to add a button to the window in Java.
LINK GOES HERE
The Idea
In order for your program to be attractive, the user must be able to easily navigate through your program. By creating a GUI the user is presented with all the features of the program in a clear and coherent manner. Adding buttons to your interface allow for user interaction which is the reason for a GUI. In pervious tutorials, our two buttons did the same thing, but real applications have different buttons that perform different functions.
Solution
Lets start with a shell we’ve formulated in previous tutorials:
Code:
package cctuts;
import java.awt.event.*;
import javax.swing.*;
public class InterfaceSix implements ActionListener{
JFrame interfaceFrame;
JButton startButton, stopButton;
public InterfaceSix(){
JFrame.setDefaultLookAndFeelDecorated(true);
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
interfaceFrame.add(stopButton);
interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
interfaceFrame.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
}
public static void main(String[] args){
new InterfaceSix();
}
}
Now just to recap what we have here: a class that makes a visible window 200px by 70px. The window looks pretty because we set the default look and feel. The window contains two button objects which are positioned using a grid layout. Each button as an action listener, so when the button is pressed the actionPerformed method is invoked. If anything I just said doesn’t make sense, you should read my previous tutorials. In the actionPerformed method we need to determine which button is pressed so we can have our program act accordingly. We do this by using the a.getSource() method, which tells us which button is being pressed. We can then perform simple logical operations to tell our program what to do.
For example if the startButton is pressed, a.getSource() == startButton. We then can say something like:
Code:
if(a.getSource() == startButton){
System.out.println(“The start button was pushed…”);
Incorporated into a full class it would look something like this:
Code:
package cctuts;
import java.awt.event.*;
import javax.swing.*;
public class InterfaceSix implements ActionListener{
JFrame interfaceFrame;
JButton startButton, stopButton;
public InterfaceSix(){
JFrame.setDefaultLookAndFeelDecorated(true);
interfaceFrame = new JFrame("First GUI");
interfaceFrame.setSize(200,70);
interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
startButton = new JButton("Start");
startButton.addActionListener(this);
interfaceFrame.add(startButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
interfaceFrame.add(stopButton);
interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
interfaceFrame.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
if(a.getSource() == startButton){
System.out.println("Start button was pressed...");
}
if(a.getSource() == stopButton){
System.out.println("Stop button was pressed...");
}
}
public static void main(String[] args){
new InterfaceSix();
}
}