从另一个类获取actionPerformed

时间:2015-11-18 14:06:32

标签: java swing

我有一个实现actionPerfomed的类,并为按钮提供了某些功能。在同一个类中,我有一个构造函数,在调用时我想要它来获取" fetch" 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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

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

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

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

它会起作用。 (最好也不要在课堂上MyButton延伸