【问题标题】:Fetching actionPerformed from another class从另一个类获取 actionPerformed
【发布时间】:2015-11-18 14:06:32
【问题描述】:

我有一个实现 actionPerfomed 并为按钮提供特定功能的类。在同一个类中,我有一个构造函数,当调用它时,我希望它“获取”存在于另一个类中的 actionPerformed,并为我的按钮提供不同的功能。

我的构造函数中的代码如下所示:

    button1.addActionListener(new MyButtonActionListener());

其中new MyButtonActionListener() 包含我要调用的actionPerformed。问题是当我单击按钮时没有任何反应。 我只是在玩,不知道这是否可以做到。谢谢!

public class ButtonFrameActionListener extends JFrame implements ActionListener {

    private MyButton button1 = new MyButton();
    private MyButton button2 = new MyButton();
    private MyButton[] buttons;

    public ButtonFrameActionListener(){
        super("A button window");
        setPreferredSize(new Dimension(400,300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel myPanel = new JPanel();

        myPanel.add(button1);
        myPanel.add(button2);

        button1.addActionListener(new MyButtonActionListener());
        button2.addActionListener(new MyButtonActionListener());

        buttons = new MyButton[2];
        buttons[0] = button1;
        buttons[1] = button2;

        this.add(myPanel);
        this.pack();
        this.setVisible(true);
    }

public void actionPerformed(ActionEvent e){
    for(int i = 0; i<buttons.length; i++){
        if(e.getSource() != buttons[i]){
            buttons[i].toggleState();
        }
    }
}

//*************************************************** *******************************//

public class MyButtonActionListener extends MyButton implements ActionListener {

    public MyButtonActionListener(Color col1, Color col2, String text1, String text2){
        super(col1, col2, text1, text2);
        /**
         * listens for an action to be performed from actionPerformed.
         * does not come into play before the button is clicked.
         */
        this.addActionListener(this);
    }

    public MyButtonActionListener(){
        this(Color.red, Color.yellow, "Press me", "Do it again!");
    }

    public void actionPerformed(ActionEvent e){
        this.toggleState();
    }
}

//*************************************************** ***********//

public class MyButton extends JButton {
private Color col1;
private Color col2;
private String text1;
private String text2;
Boolean pressed = false;

public MyButton(Color col1, Color col2, String text1, String text2){
    this.col1 = col1;
    this.col2 = col2;
    this.text1 = text1;
    this.text2 = text2;
    this.setText(text1); // states the text for the initial state
    this.setBackground(col1); // states the color for the initial state
}

public MyButton(){
    this(Color.red, Color.yellow, "Press me", "Do it again!");
}

/**
 * decide the state of the button
 */
public void toggleState(){
    pressed = !pressed; // pressed equals true
    if(!pressed){ // if pressed equals false
        this.setText(text1);
        this.setBackground(col1);
    } else {
        this.setText(text2);
        this.setBackground(col2);
    }
}

}

【问题讨论】:

  • 什么都没有发生意味着你可能没有在你的 actionPerformed 方法中做任何事情,你能分享一下你是如何实现它的吗?
  • @BOND 我现在会更新 :)
  • @DoubleOseven MyButton是什么,为什么MyButtonActionListener需要扩展MyButton?根据 Hovercraft 的建议,请更新您的帖子。

标签: java swing


【解决方案1】:

MyButtonActionListener 扩展 MyButton 是没有意义的。因此this.toggleState(); 不会在添加了侦听器的按钮上调用toggleState(),而是直接在MyButtonActionListener 实例上调用(因为它是MyButton)。改变

public void actionPerformed(ActionEvent e){
    this.toggleState();
}

public void actionPerformed(ActionEvent e) {
    ((MyButton) e.getSource()).toggleState();
}

它会起作用的。 (最好不要在类中扩展MyButton

【讨论】:

  • 谢谢。我扩展了 MyButton ActionListener 以避免再次编写(重复)MyButton 中的一些代码。可以避免吗?
  • @DoubleOseven 您不需要在MyButtonActionListener 类中重复/扩展MyButton 类的代码。它只是一个监听器,只需要 actionPerformed(ActionEvent e) 方法就可以正常工作。
  • @DoubleOseven:这是滥用继承。继承不能那样工作(正如你所发现的那样),只能用于父子关系。 ActionListener 不是一种特殊类型的按钮,因此不应扩展它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 2020-08-05
相关资源
最近更新 更多