This is the third of six tutorials that will show you how to create graphical user interfaces using java.
Prerequisites
This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you do know the Java basics you should be familiar with my previous tutorials on GUI's. To read my tutorials please refer to my INDEX
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. Although buttons look good, if they don’t do anything they have no meaning.
Solution
Since this tutorial assumes you know how to create a window and add a button to the window we will start off with that code.
The first thing we need to do is import another package and implement the ActionListerner class within that package to handle events. The package is java.awt.event (awt meaning abstract windowing toolkit). This looks like:Code:package cctuts; import javax.swing.*; public class InterfaceThree extends JFrame{ public InterfaceThree(){ setSize(400,400); JButton startButton = new JButton("Start"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args){ new InterfaceThree(); } }
By implementing the ActionListener interface, our class is required to use the actionPerformed method.Code:package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceThree extends JFrame implements ActionListener{ public InterfaceThree(){ setSize(400,400); JButton startButton = new JButton("Start"); add(startButton); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args){ new InterfaceThree(); } }
Now you are wondering, How does the program know if the button is pushed? Well at the moment it doesn’t. To do so, we must add an action listener to the startButton object. Like this:Code:package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceThree extends JFrame implements ActionListener{ public InterfaceThree(){ setSize(400,400); JButton startButton = new JButton("Start"); add(startButton); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent a) { } public static void main(String[] args){ new InterfaceThree(); } }
Now anything in the actionPerformed method will be executed every time that button is pressed. So add something in there and your done! Here is my final code to demonstrate the idea:Code:package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceThree extends JFrame implements ActionListener{ public InterfaceThree(){ setSize(400,400); JButton startButton = new JButton("Start"); startButton.addActionListener(this); add(startButton); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent a) { } public static void main(String[] args){ new InterfaceThree(); } }
Which will out put this:Code:package cctuts; import java.awt.event.*; import javax.swing.*; public class InterfaceThree extends JFrame implements ActionListener{ public InterfaceThree(){ setSize(400,400); JButton startButton = new JButton("Start"); startButton.addActionListener(this); add(startButton); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent a) { for(int i = 0; true; i++){ System.out.println(i); } } public static void main(String[] args){ new InterfaceThree(); } }
![]()
Last edited by John; 01-11-2007 at 01:10 PM.
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks