简体   繁体   English

ActionListener-跟踪是否在actionPerformed方法内部调用actionPerformed?

[英]ActionListener - Tracing if actionPerformed is called inside actionPerformed method?

Say I have a 'Select' JButton with SelectPieceListener. 说我有一个带有SelectPieceListener的“选择” JButton。 I want to check if the action(clicking Select Button) is performed again or not. 我想检查是否再次执行该动作(单击“选择按钮”)。 However, actionPerformed is void, so I cannot use it as boolean expression. 但是,actionPerformed是无效的,因此我不能将其用作布尔表达式。

Is there any way to do that? 有什么办法吗? Or is it better to use another JButton to handle this? 还是使用另一个JButton处理这个更好?

Thank you. 谢谢。

public class SelectPieceListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(c == _st.checkMoveImpossible()) {
            System.out.println("No possible move for "+nextPlayer+": click select to skip turn, " +
                    "or click right to move " + t + " pieces.");

            if(**the action is performed**){_board.setcmi(1);}
            _board.update();
        }

Do you want to know if the select button has been clicked once already (1) ? 您是否想知道是否已经单击一次选择按钮(1)? Or do you want to make sure the actionPerformed method isn't called recursively (2)? 还是要确保不会递归调用actionPerformed方法(2)?

(1) Simply add a boolean field to SelectPieceListener called 'selectPressed' (or whatever), and set it to true when actionPerformed is called. (1)只需在SelectPieceListener中添加一个名为“ selectPressed”(或其他)的布尔字段,然后在调用actionPerformed时将其设置为true。

(2) No worries, you don't seem to call it manually. (2)不用担心,您似乎没有手动调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM