• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener not recognized  RSS feed

 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to import java.awt.event.*
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bud, concluded
Patrick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!