有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从另一个类执行的java获取操作

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

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

    button1.addActionListener(new MyButtonActionListener());

其中new MyButtonActionListener()包含我要调用的已执行操作。问题是当我点击按钮时什么也没发生。 我只是随便玩玩,不知道这是否能做到。谢谢

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) 个答案

  1. # 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