【问题标题】:swing clicking two buttons摆动点击两个按钮
【发布时间】:2013-03-20 07:29:23
【问题描述】:

在我的程序中有两个按钮,您必须同时单击它们才能进行系统打印。不过,我在尝试实现这一目标时遇到了麻烦。

button[0].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            button[0].setEnabled( false );
            if( button[1].isEnabled( false) );
                System.out.println("you clicked both buttons");
        }
    });
    button[1].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            button[1].setBackground(Color.YELLOW);
            button[1].setEnabled( false );
            if( buttons[0].isEnabled( false) );
            System.out.println("you clicked both buttons");
        }
    });

我在行中遇到错误:

if( buttons[0].isEnabled( false) );

The method isEnabled() in the type Component is not applicable for the arguments (boolean)

我只是这方面的初学者,所以如果有人可以帮助或告诉我另一种方法,那就太好了。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) “在我的程序中有两个按钮,您必须同时单击它们才能进行系统打印” 为什么? 或者更确切地说,为什么没有一个 Print 按钮?
  • 在我的最终程序中,它不会创建系统打印,它会打开一个弹出框。但目前这就是我所得到的。顺便说一句,这是一个要求有两个按钮并且您必须同时单击它们的评估。
  • “顺便说一句,这是一个要求有两个按钮的评估”我觉得这很难相信。你能复制/粘贴你认为它说的部分吗?我怀疑它们的含义不同。

标签: java swing button compiler-errors listeners


【解决方案1】:

异常非常清楚。 isEnabled()没有参数所以你应该这样使用它buttons[0].isEnabled()

【讨论】:

    【解决方案2】:

    isEnabled 不需要参数。

    这样做:

    if( buttons[0].isEnabled() )
    

    【讨论】:

      【解决方案3】:

      这是你的答案:

      button1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
      
              button1.setEnabled(false);
              if (!button1.isEnabled() && !button2.isEnabled()) {
                  System.out.println("you clicked both buttons");
              }
          }
      });
      
      button2.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
      
              button2.setBackground(Color.YELLOW);
              button2.setEnabled(false);
              if (!button2.isEnabled() && !button1.isEnabled()) {
                  System.out.println("you clicked both buttons");
              }
          }
      });
      

      【讨论】:

      • 您的代码中有几个问题,首先不要在 if 语句后放置分号,第二个 isEnabled() 方法不接受返回 true 或 false 的参数,使用 !倒置。
      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 2012-05-07
      • 2011-09-08
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多