java - 由匿名内部类完成的事件处理

标签 java anonymous-inner-class

该程序只需向面板添加按钮,单击按钮时屏幕应变成指定按钮的颜色。基本上我需要更改蓝色按钮做完全相同的事情,但是使用匿名内部类,我觉得我有点走在正确的轨道上,但我收到了一长串错误。我看过很多匿名内部类的例子,我相信我编写的代码是正确的,但是有很多错误与编译器无法找到符号有关,我不完全理解。任何帮助将不胜感激,因为我已经为此工作了 2 天,希望在本周之前修复它。这是我的代码:(很多东西都被注释掉了,这样我就可以一次专注于一个按钮)

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

这是我尝试添加匿名内部类的地方:

class Blue
{
  public void start()
  {
     ActionListener listener = new Blue();
  }
  public class Blue implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
            Object source = evt.getSource();
            Color color = getBackground();
            color = Color.blue;
            setBackground(color);
            repaint();
    }
  }
}

class ButtonPanel extends JPanel //implements ActionListener
{
   private JButton yellowButton;
   private JButton blueButton;
   private JButton redButton;
   private JButton greenButton;

public ButtonPanel()
{
    //yellowButton = new JButton("Yellow");
    //redButton = new JButton("Red");
    blueButton = new JButton("Blue");
    //greenButton = new JButton("Green");

    //add(yellowButton);
    //add(redButton);
    add(blueButton);
    //add(greenButton);

    //yellowButton.addActionListener(this);

    blueButton.addActionListener(listener);
    //greenButton.addActionListener(this);

    /*class Red
    {
        public void red()
        {
            ActionListener listener = new ActionListener();
            redButton.addActionListener(listener);
        }
        class turnRed implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                setBackground(Color.red);
                repaint();
            }
        }
    }*/
}


/*public void actionPerformed(ActionEvent evt)
{
    Object source = evt.getSource();
    Color color = getBackground();
    if (source == yellowButton) color = Color.yellow;
    else if (source == blueButton) color = Color.blue;
    else if (source == redButton) color = Color.red;
    else if (source == greenButton) color = Color.green;
    setBackground(color);
    repaint();
}
}


class ButtonFrame extends JFrame
{
  public ButtonFrame()
  {
    setTitle("ButtonTest");
    setSize(300, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new ButtonPanel());
  }
}

public class ButtonTest
{
   public static void main(String[] args)
   {
      JFrame frame = new ButtonFrame();
      frame.setVisible(true);
   }
}

最佳答案

不要使用listener变量,而是创建(新创建的)匿名内部类的实例。它是这样完成的:

blueButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        Object source = evt.getSource();
        Color color = getBackground();
        color = Color.blue;
        setBackground(color);
        repaint();
    }
});

关于java - 由匿名内部类完成的事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39491445/

相关文章:

java - Gradle JaCoCo 插件 - 类和方法名称在报告中不可点击

Java - 从 lambda 中更改最终变量的值

java selenium 使用 xpath 浏览网页

java - android/graphics/bitmapfactory 上的 NoClassDefFoundError

java - 在定义之前引用匿名内部类

d - 为什么我为匿名类设置 "need opCmp"?

java - gradle 在 Java 中构建匿名内部类在哪里?

java - 将匿名类转换为 lambda 后代码表现不同

java - setAnimationStyle() 的显式动画,我的选择是什么?

java - Android java 屏幕作为按钮