使用ActionListener访问前父类

时间:2012-01-01 20:14:43

标签: java swing parent actionlistener

我把它作为家庭练习,但我不知道如何解决它...(只是访问部分有点难以弄清楚)

所以,我有两个课程:" Top"和#34; Main"。 Top类看起来像这样:(简短说明:Top class'变量" pane"将在Main类中用作顶部面板,Main扩展JFrame并具有BorderLayout)

public class Top extends JPanel implements ActionListener {

//public JPanel pane;
public JButton red, green, blue, white, black;

public Top() {
    //pane = new JPanel(); //Useless, as Top is already a JPanel
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setBackground(new Color(0xc9c9c9)); //A gray background

    red = new JButton("Red");
    red.setBackground(Color.red);
    red.addActionListener(this);
    this.add(red);

    green = new JButton("Green");
    green.setBackground(Color.green);
    green.addActionListener(this);
    this.add(green);

    blue = new JButton("Blue");
    blue.setBackground(Color.blue);
    blue.addActionListener(this);
    this.add(green);

    white = new JButton("White");
    white.setBackground(Color.white);
    white.addActionListener(this);
    this.add(white);

    black = new JButton("Black");
    black.setForeground(Color.white);
    black.setBackground(Color.black);
    black.addActionListener(this);
    this.add(black);
    }

public void actionPerformed(ActionEvent e) {
    Main main = (Main)e.getSource();
    if (e.getSource() == this.red)
        main.setCENTER(1);
    if(e.getSource() == this.green)
        main.setCENTER(2);
    if(e.getSource() == this.blue)
        main.setCENTER(3);
    if(e.getSource() == this.white)
        main.setCENTER(4);
    if(e.getSource() == this.black)
        main.setCENTER(5);
    }
}

Top类正在Main中使用,每次按下每个按钮时,主类的CENTER区域背景会发生变化。因此我编写了一个小函数setCENTER,它改变了背景。它的代码看起来像这样(代码在Main类中):

    public void setCENTER(int var){
    switch(var){
        case 1: pane.setBackground(Color.red);
        break;
        case 2: pane.setBackground(Color.green);
        break;
        case 3: pane.setBackground(Color.blue);
        break;
        case 4: pane.setBackground(Color.white);
        break;
        case 5: pane.setBackground(Color.black);
        break;
    }
}

到目前为止,内容显示正确(主窗口中的顶部面板和主窗口本身),但每次我按下顶部面板中的5个按钮之一尝试更改背景时,我会收到错误并且第一个错误指向Top类中actionPerformed-method的这一行:

        Main main = (Main)e.getSource();

我尝试用" Main main = new Main();"替换它。它起作用了,背景发生了变化,但每次按下它都会打开一个新窗口(显然不是它应该如何表现)。

编辑:我忘记了Top已经是一个JPanel了,所以在课堂上使用另一个JPanel是没有意义的。 (编辑来源)

3 个答案:

答案 0 :(得分:2)

事件的来源是按钮。要查找按钮所属的面板,您可以获得按钮的父级:

JButton button = (JButton)e.getSource();
JPanel parent = button.getParent();

要获取主面板,您可能还需要在父面板上调用getParent()。

答案 1 :(得分:1)

操作的来源是JButton,而不是Main,因此当您尝试投射时会出现异常。

您可以考虑将Main实例传递给Top课程,而不是

答案 2 :(得分:0)

在一般情况下,您需要按照层次结构链接,调用getParent(),直到找到所需的父级。如果你正在寻找一个特定的类,通过一些泛型魔术你可以写一个实用程序来做到这一点:

   public static <T extends Container> T findParent(Component comp, Class<T> clazz)  {
      if (comp == null)
         return null;
      if (clazz.isInstance(comp))
         return (clazz.cast(comp));
      else
         return findParent(comp.getParent(), clazz);     
   }

所以,如果你想找到FooPanel,它是触发事件的Button的ancester,你会说

FooPanel fooPanel = findParent((Component)event.getSource(), FooPanel.class);
fooPanel.doSomething...