Forums Register Login

ActionListener not recognized

+Pie Number of slices to send: Send
Hello Ranchers,
i would like to program a frame with 2 buttons, so i go for inner classes that implement ActionListener.
Even i've imported the classes, the compiler doesn't want to recognize it.
Here's the code, below that the compiler output:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ZweiButtons {
JFrame frame;// erzeugen der framesreferenz
JLabel label;

public static void main (String[] args) {
ZweiButtons gui = new ZweiButtons();
gui.los();
} // end of main

public void los () {
frame = new JFrame();// creating frameobject
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// closes program after closing object

JButton labelButton = new JButton("�ndere Label!!!");// creating button
labelButton.addActionListener(new LabelListener());// calling registration

JButton colourButton = new JButton("�ndere Kreis");// creating button
colourButton.addActionListener(new ColourListener());// calling registration

label = new JLabel("Ich bin ein Label");
MeinZeichenPanel zeichenPanel = new MeinZeichenPanel();// creating panel

frame.getContentPane().add(BorderLayout.SOUTH,colourButton); // put the button in the frame
frame.getContentPane().add(BorderLayout.CENTER,zeichenPanel); // put the panel in the frame
frame.getContentPane().add(BorderLayout.EAST,labelButton); // put the panel in the frame
frame.getContentPane().add(BorderLayout.WEST,label); // put the panel in the frame
frame.setSize(420,300);// set framesize
frame.setVisible(true);// make it appear
} // end of los

class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
label.setText("Autsch");
}
}

class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}

class MeinZeichenPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int rot = (int) (Math.random()*255);
int gr�n = (int) (Math.random()*255);
int blau = (int) (Math.random()*255);
Color startColor = new Color(rot, gr�n, blau);

rot = (int) (Math.random()*255);
gr�n = (int) (Math.random()*255);
blau = (int) (Math.random()*255);
Color endColor = new Color(rot, gr�n, blau);

GradientPaint gradient = new GradientPaint (70,70,startColor, 150,150, endColor);
g2d.setPaint(gradient);

// g.fillRect(0,0,this.getWidth(),this.getHeight());

// g.setColor(randomColor);


g.fillOval(70,70,100,100);
} // end of paintComponent
} // end of class MeinZeichenPanel
} // end of class ZweiButtons

Here's the compiler output:

ZweiButtons.java:35: cannot find symbol
symbol : class ActionListener
location: class ZweiButtons
class LabelListener implements ActionListener {
^
ZweiButtons.java:36: cannot find symbol
symbol : class ActionEvent
location: class ZweiButtons.LabelListener
public void actionPerformed(ActionEvent event) {
^
ZweiButtons.java:41: cannot find symbol
symbol : class ActionListener
location: class ZweiButtons
class ColorListener implements ActionListener {
^
ZweiButtons.java:42: cannot find symbol
symbol : class ActionEvent
location: class ZweiButtons.ColorListener
public void actionPerformed(ActionEvent event) {
^
ZweiButtons.java:19: addActionListener(java.awt.event.ActionListener) in javax.s
wing.AbstractButton cannot be applied to (ZweiButtons.LabelListener)
labelButton.addActionListener(new LabelListener()); // calli
ng registration
^
ZweiButtons.java:22: cannot find symbol
symbol : class ColourListener
location: class ZweiButtons
colourButton.addActionListener(new ColourListener()); // calli
ng registration
^
6 errors

Thanks a lot, Patrick
+Pie Number of slices to send: Send
You forgot to import java.awt.event.*
+Pie Number of slices to send: Send
Thanks Bud, concluded
Patrick
You can't have everything. Where would you put it?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1118 times.
Similar Threads
I'm not able to compile the below prog...can you pleas tell me what is the error in using while loop
Head First Java Question - GUI program TwoButtons
why does this code fail?
TwoButtons App From "HeadFirst Java" Book
Beginners question (Head First - TwoButtons)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:46:28.