【问题标题】:How to enable / disable buttons in java, eclipse?如何在 java、eclipse 中启用/禁用按钮?
【发布时间】:2014-02-08 04:17:01
【问题描述】:

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

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");

    }
});

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

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

【问题讨论】:

  • 只有一个问题,请问这个问题是关于......
  • 在 dot 之后无法使用代码标签(永远不要举手,也许有人会使用 html 格式这样做)
  • 使用 JRadioButtons 添加到 ButtonGroup,使用 JToggleButtons 可以轻松实现
  • 您需要了解SwingWorker API。您可以禁用按钮并启动工作人员。完成后,它可以重新启用按钮。
  • 请学习常见的Java naming conventions(特别是用于名称的大小写)用于类、方法和属性名称并一致地使用它们。

标签: java eclipse swing button awt


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    在这种情况下:

    LeftButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
    
    DownButton.setVisible(false);
    UpButton.setVisible(false);
    RightButton.setVisible(false);
    System.out.printline("Left Button");
    

    这是一个有效的例子:

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

    【讨论】:

    • 嗨,Rafael 都用我的方法 DownButton.setEnabled(false);和你的方法 DownButton.setVisible(false);它说按钮无法解决。如果按下一个按钮,我希望按钮可见但禁用。
    • 是否声明了按钮(私有 javax.swing.JButton UpButton;)?您是否导入了 javax.swing.JButton?应该是吧?
    【解决方案3】:

    如果你试试这个:

    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");
    }
    

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

    【讨论】:

      【解决方案4】:

      如果你想设置按钮禁用,你可以使用

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

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

      希望对大家有所帮助。

      【讨论】:

        【解决方案5】:

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

        例如:

        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);
        
            }
        
        });
        

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


        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-05
          • 2021-05-18
          • 1970-01-01
          • 2017-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多