简体   繁体   English

如何在秋千中创建翻转按钮

[英]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方法不能用于此目的。

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)不是,永远不会在MouseKeyEvent的用户端使用,因为这两个事件都将(立即)转换为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM