2016-09-14 55 views
0

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

class'Blue' with a inner class'Blue'? – SomeJavaGuy

回答

1

而不是使用listener变量,创建和新创建的匿名内部类的实例。这是如何完成的:

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

或者在Java 8中,您可以使用lambda,例如'blueButton.addActionListener(event - > {/ * do stuff * /});' – Orin

+0

问题要求使用“匿名内部类”的解决方案。 – bruno

+0

是的,我不能使用该特定按钮的lambda表达式。对于蓝色按钮,我需要做一个匿名的内部类。 @ Orin2005 – Jay