Dynamically change jButton icon

后端 未结 5 1229
Happy的楠姐
Happy的楠姐 2021-01-19 22:14

I have a program that detects when certain machines are online and creates a button with a green \"online\" icon to show this. I want to add the functionality to check perio

5条回答
  •  佛祖请我去吃肉
    2021-01-19 22:38

    you can add an action listener on the button, then in its called function change the icon - here is an example :

    public yourDialogSwing() {
    
            Yourbutton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onButtonPressed();
                }
            });
    }
    
    private void onButtonPressed() {
           YourButton.setIcon(YourButton.getPressedIcon());
    }
    

提交回复
热议问题