如何在 java、eclipse 中启用/禁用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21209532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:21:29  来源:igfitidea点击:

How to enable / disable buttons in java, eclipse?

javaeclipseswingbuttonawt

提问by John Smith

How can I disable buttons with the press of one button and when the task that the button which has been pressed was assigned to has been done then I want all the buttons to be enabled. for example;

如何通过按下一个按钮来禁用按钮,并且当按下的按钮分配给的任务完成时,我希望启用所有按钮。例如;

UpButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        DownButton.setEnabled(false);
        LeftButton.setEnabled(false);
        RightButton.setEnabled(false);
        System.out.printline("Up Button");

    }
});
DownButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        UpButton.setEnabled(false);
        LeftButton.setEnabled(false);
        RightButton.setEnabled(false);
        System.out.printline("Down Button");


    }
});
LeftButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        DownButton.setEnabled(false);
        UpButton.setEnabled(false);
        RightButton.setEnabled(false);
        System.out.printline("Left Button");

    }
});
RightButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        DownButton.setEnabled(false);
        LeftButton.setEnabled(false);
        UpButton.setEnabled(false);
        System.out.printline("Right Button");

    }
});

I have tried to do it but it does not work. What I want is, for example, my Upbutton is printing out ("Up button"), so when a user presses this button I want all the other buttons to be disabled until it has finished doing the command that it was suppose to do, in this case print out a text but later on I will be adding things like adding up two user inputs e.g. I will have a button that will ask the user to type number 1 and then number 2 and then the computer will add them up and during this process I want all the buttons to be disabled expect the one that has been clicked on, until the user has given all numbers and the computer has given the output.

我试图这样做,但它不起作用。例如,我想要的是,我的向上按钮正在打印(“向上按钮”),因此当用户按下此按钮时,我希望禁用所有其他按钮,直到它完成它应该执行的命令,在这种情况下,打印出一个文本,但稍后我将添加诸如将两个用户输入相加之类的内容,例如,我将有一个按钮,要求用户输入数字 1,然后输入数字 2,然后计算机会将它们相加并在此期间这个过程我希望所有按钮都被禁用,除了被点击的按钮,直到用户给出所有数字并且计算机给出输出。

I hope I have explained myself properly, if not please let me know and I will try my best to give more information. Thanks in advance.

我希望我已经正确地解释了自己,如果没有,请告诉我,我会尽力提供更多信息。提前致谢。

回答by Rafael Carvalhido

That seems right. Maybe you want them invisible instead of disabled...

这似乎是对的。也许您希望它们不可见而不是禁用...

In that case:

在这种情况下:

LeftButton.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) {

DownButton.setVisible(false);
UpButton.setVisible(false);
RightButton.setVisible(false);
System.out.printline("Left Button");

Here′s an example of something that works:

这是一个有效的例子:

private void jButtonChangeChampActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    jComboBoxPlayer.setEnabled(true);
    jComboBoxChampion.setEnabled(true);
    jButtonSelecionar.setVisible(true);
    jButtonMagias.setVisible(false);
}

回答by Rafael Carvalhido

What if you try this:

如果你试试这个怎么办:

UpButton.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e) {

    UpButtonActionPerformed(evt);

        }
    });

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    DownButton.setEnabled(false);
    LeftButton.setEnabled(false);
    RightButton.setEnabled(false);
    System.out.printline("Up Button");
}

It works here...

它在这里工作......

回答by user 451

If you want to Set the Button Disabled , You can use the

如果您想将按钮设置为禁用,您可以使用

btnExample.setDisable(true); // This will disable the Button

btnExample.setDisable(true); // 这将禁用按钮

btnBreak.setVisible(false); // This will make the button Disappear visually only.

btnBreak.setVisible(false); // 这将使按钮仅在视觉上消失。

Hope this help guys.

希望这对伙计们有所帮助。

回答by The_Cute_Hedgehog

How about using Commandobjects? Each command object might have execute()and canExecute()methods at least. Then;

如何使用Command对象?每个命令对象可能至少有execute()canExecute()方法。然后;

btnA.setEnabled( cmdA.canExecute() );
btnA.addActionListener( event-> { 
    cmdA.execute();
    btnB.setEnabled( cmdA.canExecute() );
});

回答by anacron

You have to first disable the other buttons, finish your task and then enable the buttons again.

您必须首先禁用其他按钮,完成您的任务,然后再次启用这些按钮。

For example:

例如:

UpButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        // first disable all other buttons 
        DownButton.setEnabled(false);
        LeftButton.setEnabled(false);
        RightButton.setEnabled(false);

        System.out.println("Up Button");
        // do rest of your tasks here. 

        // now enable them again
        DownButton.setEnabled(true);
        LeftButton.setEnabled(true);
        RightButton.setEnabled(true);

    }

});

Similarly, setup the Listeners for the other buttons.

同样,为其他按钮设置监听器。



Hope this helps!

希望这可以帮助!