繁体   English   中英

摇摆点击两个按钮

[英]swing clicking two buttons

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

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)

我只是一个初学者,所以如果有人可以帮助或告诉我另一种方式,那将是很棒的。

异常非常明显。 isEnabled()没有参数,因此您应该以这种方式使用它buttons[0].isEnabled()

isEnabled不需要参数。

做这个:

if( buttons[0].isEnabled() )

这是您的答案:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM