[英]How to create a rollover button in swing
我有两个按钮
JButton playB = new JButton(new ImageIcon("res/playA.png"));
playB.setBorder(BorderFactory.createEmptyBorder());
playB.setContentAreaFilled(false);
JButton playA = new JButton(new ImageIcon("res/playA.png"));
playA.setBorder(BorderFactory.createEmptyBorder());
playA.setContentAreaFilled(false);
如何创建它,以便将鼠标移到按钮A上时切换到按钮B? 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
如何创建它,以便将鼠标移到按钮A上时切换到按钮B? 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
注意:setRolloverIcon方法不能用于此目的。
这个想法,用两个JButton进行切换不是...,也不是使用MouseListener ,因为所有鼠标和键事件都在JButtons APi中实现,并且正确
将JButton.setRolloverIcon与ButtonModel中的事件一起使用 ,输出应为两个Swing Action,而不是在运行时添加的两个JButton
以及ActionListener
。
例如
JButton.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isRollover()) {
//doSomething
} else if (model.isPressed()) {
//doSomething
} // etc
}
});
编辑
再回来
如何创建它,以便将鼠标移到按钮A上时切换到按钮B? 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
结论是,第一个JButton (button A)
不是,永远不会在Mouse
或KeyEvent
的用户端使用,因为这两个事件都将(立即)转换为roll_over
,
那么只有一种方法,可以从KeyBinding
s中以编程方式调用JButton.doClick()
,但是仍然存在问题,何时,如何以及为什么原因……使简单的事情变得复杂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.