[英]Changing Jbutton's Icon Through mouse listener
I'm doing a board game project and I'm representing cells by Jbuttons. 我正在做一个棋盘游戏项目,我正在用Jbuttons代表细胞。 I made mouseLitener to all the buttons.
我为所有按钮制作了mouseLitener。 My question is how to change the icon of the Jbutton when it is clicked ?
我的问题是如何在单击时更改Jbutton的图标?
I'm doing a board game project and I'm representing cells by Jbuttons.
我正在做一个棋盘游戏项目,我正在用Jbuttons代表细胞。
use JToggleButton for game based on buttons array and mouse events, rather than JButton
使用JToggleButton进行基于按钮数组和鼠标事件的游戏,而不是
JButton
use ButtonModel instead of any XxxListener 使用ButtonModel而不是任何XxxListener
JButton
and JToggleButton
has implemented these methods in the API directly JButton
和JToggleButton
直接在API中实现了这些方法
. 。
setIcon(Icon i);
setRolloverIcon(Icon i);
setPressedIcon(Icon i);
setDisabledIcon(Icon i);
yourButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
yourButton.setIcon(new ImageIcon("yourImage"));
}
});
ActionListener is called when you click on the JButton. 单击JButton时会调用ActionListener。 This way is used most frequently.
这种方式最常用。
作为替代,也可以考虑setText()
使用Unicode字形,示出在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.