【问题标题】:Event handling done by an anonymous inner class由匿名内部类完成的事件处理
【发布时间】:2016-09-14 13:14:42
【问题描述】:

该程序由简单地向面板添加按钮组成,当单击按钮时,屏幕应变为指定按钮的颜色。基本上我需要更改蓝色按钮做完全相同的事情,但是使用匿名内部类,我觉得我在正确的轨道上,但我得到了一长串错误。我查看了许多匿名内部类的示例,并且我相信我正在正确编写代码,但是有很多错误与编译器无法找到符号有关,我并不完全理解。任何帮助将不胜感激,因为我已经为此工作了 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);
   }
}

【问题讨论】:

  • 一个类 Blue 和一个内部类 Blue?

标签: java anonymous-inner-class


【解决方案1】:

不使用listenervariable,而是创建一个(新创建的)匿名内部类的实例。这样做是这样的:

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

【讨论】:

  • 或者在 Java 8 中,您可以只使用 lambda,例如 blueButton.addActionListener( event -> { /*do stuff */ } );
  • 该问题要求使用“匿名内部类”的解决方案。
  • 是的,我不能对那个特定的按钮使用 lambda 表达式。对于蓝色按钮,我需要做一个匿名内部类。 @Orin2005
  • @bruno 我在你写的时候做了那个声明,但它一直把我引到程序中的最后一个大括号,好像当时有一个错误,但我不明白为什么最后一个程序的大括号会带来一个错误。我检查了所有的大括号已经有一对匹配的。
  • @StephanieCorrea,响应中的大括号 sn-p 匹配。也许您的类文件中的其他地方有一些额外的大括号...
猜你喜欢
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多