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

前端 未结 6 1528
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:47

    Seems you need to use JToggleButton :

    JToggleButton tb = new JToggleButton("push me");
    tb.addActionListener(new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            JToggleButton btn =  (JToggleButton) e.getSource();
            btn.setText(btn.isSelected() ? "pushed" : "push me");
        }
    });
    

提交回复
热议问题