简体   繁体   中英

swing clicking two buttons

In my program there are two buttons and you have to click both of them in order for a system print out to happen. i am having trouble trying to achieve this though.

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

I am getting errors in the line:

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

saying

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

I am only a beginner in this so it would be great if someone could help or tell me another way to do this.

Exception is very clear. isEnabled() hasn't got parameters so you should use it in this way buttons[0].isEnabled() .

isEnabled does not require an argument.

Do this:

if( buttons[0].isEnabled() )

Here is your answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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