Changing Jbutton's Icon Through mouse listener

后端 未结 3 821
南旧
南旧 2020-12-18 10:05

I\'m doing a board game project and I\'m representing cells by Jbuttons. I made mouseLitener to all the buttons. My question is how to change the icon of the Jbutton when it

3条回答
  •  鱼传尺愫
    2020-12-18 10:43

    yourButton.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent e) {
            yourButton.setIcon(new ImageIcon("yourImage"));
        }
    });
    

    ActionListener is called when you click on the JButton. This way is used most frequently.

提交回复
热议问题