简体   繁体   English

为什么不能在两个内部类上实现两个ActionListener?

[英]Why can't I implement two ActionListeners on two inner classes?

I'm doing the double actionlistener exercise detailed in Head First Java, and learn about inner classes, but for some reason my code is not compiling. 我正在执行Head First Java中详细介绍的double actionlistener练习,并了解内部类,但是由于某些原因我的代码未编译。 I'm getting argument invalid errors when I try to call the addActionListener method for JButton. 当我尝试为JButton调用addActionListener方法时,出现参数无效错误。

TwoButtons.java: TwoButtons.java:

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

public class TwoButtons {
    JFrame frame;
    JLabel label;

    public static void main(String[] args){
        TwoButtons gui = new TwoButtons();
        gui.go();
    }

    public void go(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change Label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change circle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a label");
        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.EAST, labelButton);
        frame.getContentPane().add(BorderLayout.WEST, label);

        frame.setSize(300,300);
        frame.setVisible(true);

    }

    class LabelListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            label.setText("OUch!");
        }
    }//close inner class

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

}

MyDrawPanel.java: (no errors) MyDrawPanel.java :(无错误)

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


public class MyDrawPanel extends JPanel{
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        int red = (int)(Math.random()*255);
        int green = (int)(Math.random()*255);
        int blue = (int)(Math.random() * 255);
        Color startColor = new Color(red,green,blue);

        red = (int)(Math.random()*255);
        green = (int)(Math.random()*255);
        blue = (int)(Math.random() * 255);
        Color endColor = new Color(red, green, blue);

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

    }

}

You need to import ActionEvent and ActionListener : 您需要导入ActionEventActionListener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

They reside under java.awt.event.* and not java.awt.* . 它们位于java.awt.event.* ,而不位于java.awt.*

If you use an IDE like Eclipse, you can easily import all needed classes with a simple action. 如果使用像Eclipse这样的IDE,则可以通过简单的操作轻松导入所有需要的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM