【问题标题】:How to tell if an actionevent has been performed for an if statement如何判断是否为 if 语句执行了 actionevent
【发布时间】:2013-10-19 07:40:56
【问题描述】:

我需要知道是否执行了特定的动作事件,因为在 if 语句中我将执行另一个动作,但这取决于已经执行了哪些动作事件。

xButton9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent x9) {
                xButton9.setVisible(false);
                oButton9.setVisible(false);
                nine.repaint();
                nine.add(xlabel);
        }
} );

oButton9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent o9) {
                xButton9.setVisible(false);
                oButton9.setVisible(false);
                nine.repaint();
                nine.add(olabel);
        }      
} );  

if (ActionEvent 09 has been performed) {
    do this stuff
}

【问题讨论】:

    标签: java if-statement awt actionevent


    【解决方案1】:

    您需要在您的 actionPerformed() 方法中处理它,同时您仍然拥有对 o9 的引用。

    oButton9.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent o9) {
        xButton9.setVisible(false);
        oButton9.setVisible(false);
        nine.repaint();
        nine.add(olabel);
    
        // whatever else happens when oButton9 is clicked
        }      
    } );  
    

    【讨论】:

    • 但是点击 oButton9 时会发生什么取决于之前是否点击过其他按钮,是否有办法编写一个 if 语句来检查 oButton9 的可见性,因为设置了可见性在 actionevent 中为 false?
    • 我明白了——您必须自己跟踪班级中的按钮状态(即已按下哪些按钮)。
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多