How can I check that JButton is pressed? If the isEnable() is not work?

前端 未结 6 1527
Happy的楠姐
Happy的楠姐 2020-11-30 14:22

How can I check that JButton is pressed? I know that there is a method that its name is \"isEnabled\"

So I try to write a code to test.

  1. this code have
6条回答
  •  抹茶落季
    2020-11-30 14:57

    JButton#isEnabled changes the user interactivity of a component, that is, whether a user is able to interact with it (press it) or not.

    When a JButton is pressed, it fires a actionPerformed event.

    You are receiving Add button is pressed when you press the confirm button because the add button is enabled. As stated, it has nothing to do with the pressed start of the button.

    Based on you code, if you tried to check the "pressed" start of the add button within the confirm button's ActionListener it would always be false, as the button will only be in the pressed state while the add button's ActionListeners are being called.

    Based on all this information, I would suggest you might want to consider using a JCheckBox which you can then use JCheckBox#isSelected to determine if it has being checked or not.

    Take a closer look at How to Use Buttons for more details

提交回复
热议问题