简体   繁体   中英

How to use actionPerformed() to check to see if a button has been clicked?

public void actionPerformed(ActionEvent e) {
    if (text1.equals(e.isSelected())) {
        System.out.println("test");
    }
    else {
        System.out.println("error");
        }
    }
}

text1 is my JButton for some reason, isSelected is not working, anyone know why? any help is appreciated!

thanks in advance,

-kameron

isSelected is undefined for ActionEvent . If the ActionListener is the only one registered with the JButton , then the code will be simply:

text1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {

       System.out.println("test");
    }
});

This question raises more questions.

By it's nature, JButton does not support the property isSelected . isSelected is used by toggle style buttons, like JRadioButton , JCheckBox and JToggleButton .

By the very nature of your question, if the JButton is clicked, it will be "selected"

public void actionPerformed(ActionEvent e) {
    System.out.println("text1 has being selected");
}

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