Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 第二次按下GUI按钮_Java_User Interface_Button_Uibutton_Actionlistener - Fatal编程技术网

Java 第二次按下GUI按钮

Java 第二次按下GUI按钮,java,user-interface,button,uibutton,actionlistener,Java,User Interface,Button,Uibutton,Actionlistener,在计算并显示用户掷骰子后,我尝试在GUI中再次按下按钮。到目前为止,我提出的唯一解决方案是放置一个.setSource(false);我的按钮的actionListener中的语句。我应该把我的代码放在哪里(我假设可能有一个更有效的语句来重置我的按钮) 可能利用类全局布尔变量作为切换标志: public class MyClass { boolean toggle = false; /* other relative class code */

在计算并显示用户掷骰子后,我尝试在GUI中再次按下按钮。到目前为止,我提出的唯一解决方案是放置一个.setSource(false);我的按钮的actionListener中的语句。我应该把我的代码放在哪里(我假设可能有一个更有效的语句来重置我的按钮)


可能利用类全局布尔变量作为切换标志:

public class MyClass {

    boolean toggle = false;
    /*
      other relative class code
    */


    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        // If button is selected the first time.
        if (e.getSource() == button && !toggle){
            toggle = true;
            /* my output when button is pressed the first time.
            .
            .
            . */

            e.setSource(false);
        }
        // If button is selected the second time.
        if (e.getSource() == button && toggle){
            toggle = false;
            /* my output when button is pressed the second time.
            .
            .
            . */
            e.setSource(false);
        }
    }); 

您想重置按钮GUI,因为每次单击按钮时都需要该按钮来更改按钮内的文本显示?为什么要重置它?@user3437460 post-edited。我希望允许在我的程序中再按一次按钮,这将在单击按钮后替换相应的输出。@DevilsHnd^在上面的评论中回答
public class MyClass {

    boolean toggle = false;
    /*
      other relative class code
    */


    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        // If button is selected the first time.
        if (e.getSource() == button && !toggle){
            toggle = true;
            /* my output when button is pressed the first time.
            .
            .
            . */

            e.setSource(false);
        }
        // If button is selected the second time.
        if (e.getSource() == button && toggle){
            toggle = false;
            /* my output when button is pressed the second time.
            .
            .
            . */
            e.setSource(false);
        }
    });