[英]How to create a rollover button in swing
So I have two buttons, 我有两个按钮
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);
How would I create it so that when I move my mouse over button A it switches to button B? 如何创建它,以便将鼠标移到按钮A上时切换到按钮B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way. 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
How would I create it so that when I move my mouse over button A it switches to button B? 如何创建它,以便将鼠标移到按钮A上时切换到按钮B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way. 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
Note: The setRolloverIcon method can't be used for this. 注意:setRolloverIcon方法不能用于此目的。
this idea, switching with two JButton s isn't ..., nor to use MouseListener , because all mouse and key events are implemented in JButtons APi and correctly 这个想法,用两个JButton进行切换不是...,也不是使用MouseListener ,因为所有鼠标和键事件都在JButtons APi中实现,并且正确
use JButton.setRolloverIcon with event from ButtonModel , output should be two Swing Action s instead of two JButton
s added on runtime, and with ActionListener
s 将JButton.setRolloverIcon与ButtonModel中的事件一起使用 ,输出应为两个Swing Action,而不是在运行时添加的两个JButton
以及ActionListener
。
eg 例如
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
}
});
EDIT 编辑
again back 再回来
How would I create it so that when I move my mouse over button A it switches to button B? 如何创建它,以便将鼠标移到按钮A上时切换到按钮B? All I can think of doing is using a mouse listener but I'm wondering if there would be any better way. 我能想到的就是使用鼠标侦听器,但我想知道是否还有更好的方法。
conclusion then first JButton (button A)
isn't, never will be accesible on users side from Mouse
or KeyEvent
s, because both those event will witch that (immediatelly) to roll_over
, 结论是,第一个JButton (button A)
不是,永远不会在Mouse
或KeyEvent
的用户端使用,因为这两个事件都将(立即)转换为roll_over
,
then there is only one way, call programatically JButton.doClick()
from KeyBinding
s, but still is there question when, how and for why reason ... complicating simple things 那么只有一种方法,可以从KeyBinding
s中以编程方式调用JButton.doClick()
,但是仍然存在问题,何时,如何以及为什么原因……使简单的事情变得复杂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.