This is a discussion on Java:Tutorial - Make Your Button Work within the Java Programming forums, part of the Software Development category; Prerequisites This tutorial is moderately difficult. If you know the Java basics please refer to my previous tutorials. If you ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. 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(); } } 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 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(); } } 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 void actionPerformed(ActionEvent a) { } 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"); startButton.addActionListener(this); 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) { for(int i = 0; true; i++){ System.out.println(i); } } public static void main(String[] args){ new InterfaceThree(); } } |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - Making multiple objects work differently | pranky | Java Programming | 2 | 11-25-2008 02:57 AM |
| Can I make a button on my page work as the browser's Back button in javascript? | kingmaker | HTML, CSS and Javascript Coding Techniques | 1 | 07-26-2007 01:27 AM |
| Java:Tutorial - A better looking GUI | pranky | Java Programming | 2 | 03-08-2007 12:34 PM |
| Java:Tutorial - The Variable | pranky | Java Programming | 0 | 02-24-2007 12:59 AM |
| Java:Tutorial - Tic-Tac-Toe | pranky | Java Programming | 6 | 02-23-2007 09:48 AM |