Hello guys. How do you create an ActionListener. I am confused on how to do it.
Here is my code: -
/**
-
* Written By: Edward Sanger
-
* Coded in: Java
-
* IDE Used: IntelliJ IDEA
-
* Program Name: Calculator
-
* Program Description: This program will create a GUI window with 4 buttons for 4 mathematical operators and 2 text
-
* fields for the numbers to add, subtract, multiply, and divide.
-
* Date: May 26, 2008
-
* Time: 2:34:52 PM
-
*/
-
-
//import declarations
-
import java.awt.*;
-
import javax.swing.*;
-
-
public class Calculator implements ActionListener {
-
public static void main(String[] args) {
-
//create a new jframe
-
JFrame frame = new JFrame();
-
frame.setTitle("Calculator");
-
//create a container
-
Container contentPane = frame.getContentPane();
-
-
//create the frame objects
-
JTextField num1 = new JTextField("Number 1");
-
JTextField num2 = new JTextField("Number 2");
-
-
JButton add = new JButton("Add");
-
JButton subtract = new JButton("Subtract");
-
JButton multiply = new JButton("Multiply");
-
JButton divide = new JButton("Subtract");
-
-
JLabel result = new JLabel("Result");
-
-
//add the objects
-
contentPane.add(num1);
-
contentPane.add(num2);
-
-
contentPane.add(add);
-
contentPane.add(subtract);
-
contentPane.add(multiply);
-
contentPane.add(divide);
-
-
contentPane.add(result);
-
-
//create the layout
-
FlowLayout layout = new FlowLayout();
-
contentPane.setLayout(layout);
-
-
//create an action listener
-
-
-
//frame settings
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
}
-
4 1982
Did you look in the new tutorial? http://java.sun.com/docs/books/tutor...nlistener.html
2 questions.
1. What goes in the parentheses in this line of code - b.addActionListener(this);
2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial: - public void actionPerformed(ActionEvent e) {
2 questions.
1. What goes in the parentheses in this line of code - b.addActionListener(this);
2. In my code I have four buttons. How do I create a seperate event for each one. It just says in the tutorial: - public void actionPerformed(ActionEvent e) {
1. To find out how to use a method, look it up in the API: void addActionListener(ActionListener listener)
So you pass addActionListener a reference to an ActionListener.
2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo: - import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
class Listener1 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener1");
-
}
-
}
-
-
class Listener2 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener2");
-
}
-
}
-
-
class Listener3 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener3");
-
}
-
}
-
-
class Listener4 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener4");
-
}
-
}
-
-
public class FourListenersExample {
-
public static void main(String[] args) {
-
EventQueue.invokeLater(new Runnable(){
-
public void run() {
-
launch();
-
}
-
});
-
}
-
-
static void launch() {
-
JPanel cp = new JPanel();
-
configure(cp, "button 1", new Listener1());
-
configure(cp, "button 2", new Listener2());
-
configure(cp, "button 3", new Listener3());
-
configure(cp, "button 4", new Listener4());
-
-
JFrame f = new JFrame("FourListenersExample");
-
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
f.setContentPane(cp);
-
f.pack();
-
f.setLocationRelativeTo(null);
-
f.setVisible(true);
-
}
-
-
static void configure(JPanel cp, String text, ActionListener al) {
-
JButton btn = new JButton(text);
-
btn.addActionListener(al);
-
cp.add(btn);
-
}
-
}
1. To find out how to use a method, look it up in the API: void addActionListener(ActionListener listener)
So you pass addActionListener a reference to an ActionListener.
2. Separate events? The buttons create the events. I think you mean "separate event listeners". You create separate listeners by creating separate listeners. I think you are confused about something, because that question is like asking "how can I define four classes" or "how can I eat four apples". You go ahead and do it. Here's a quick demo: - import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
class Listener1 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener1");
-
}
-
}
-
-
class Listener2 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener2");
-
}
-
}
-
-
class Listener3 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener3");
-
}
-
}
-
-
class Listener4 implements ActionListener {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println("Listener4");
-
}
-
}
-
-
public class FourListenersExample {
-
public static void main(String[] args) {
-
EventQueue.invokeLater(new Runnable(){
-
public void run() {
-
launch();
-
}
-
});
-
}
-
-
static void launch() {
-
JPanel cp = new JPanel();
-
configure(cp, "button 1", new Listener1());
-
configure(cp, "button 2", new Listener2());
-
configure(cp, "button 3", new Listener3());
-
configure(cp, "button 4", new Listener4());
-
-
JFrame f = new JFrame("FourListenersExample");
-
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
f.setContentPane(cp);
-
f.pack();
-
f.setLocationRelativeTo(null);
-
f.setVisible(true);
-
}
-
-
static void configure(JPanel cp, String text, ActionListener al) {
-
JButton btn = new JButton(text);
-
btn.addActionListener(al);
-
cp.add(btn);
-
}
-
}
I am getting errors in my code for the methods. It says Identifier expected.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hal Vaughan |
last post by:
I have a "wizard" type program with a series of panels that are placed in a
JPanel. When the "Next >" button is pressed, I put in a new panel (in
other words, the ActionListener for "Next >"...
|
by: Patrick Vanhoof |
last post by:
Hi,
I have a small program to test the ActionListener, but it gives errors.
Here's part of the code:
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent...
|
by: linux newbie |
last post by:
is there any java expert could tell me what the problem below and
explain the code to me, your explanation is greatly appreciated :)
static Class class$java$awt$event$ActionListener;
...
|
by: cnixuser |
last post by:
Hello, I am currently attempting to implement a simple actionlistener for a button in a JFrame that was created via "drag and drop" with the Netbeans 5.0 IDE, the code that I am using to implement...
|
by: Phil Latio |
last post by:
Quite a while back I used Java Swing (for Uni project) which allowed the
developer to a create form and on the "Submit" button I recall adding
something called an ActionListener. I was thinking of...
|
by: shailajaanand |
last post by:
can we add Actionlistener to textarea?
|
by: stmfc |
last post by:
KeyListener listens keyboard strokes,
MouseListener listens mouse events, etc.
My question is about ActionListener.
what kind of events does ActionListener listen?
its name does not give...
|
by: carlos123 |
last post by:
if (bathroom.isSelected()){
bathroom is a jcheckbox, why doesnt that work.
|
by: Stubert |
last post by:
Hi there just a quick question, if I have multiple JMenuItems do I need to create an inner class ActionListener for each and every JMenuItem I create? Or is there a more efficient way of doing it?
...
|
by: Kid Programmer |
last post by:
Hey guys. Once again I am making a calculator like I do so much. I ran into an error with this program though. I just couldn't figure out how to make the operators work. I want this calculator to...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |