通过热键执行相应操作时为按钮设置动画

发布于 2025-01-04 10:48:19 字数 371 浏览 4 评论 0原文

我有与按钮和热键相结合的操作。我想在通过热键触发按钮的相应操作时对按钮进行动画处理(类似于鼠标单击期间显示的动画)。这可能吗?

我正在做如下:

        btnAdd.setAction(addDataAction);
        panelAdd.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                .put(KeyStroke.getKeyStroke("ctrl ENTER"), addDataAction);
        panelAdd.getActionMap().put(addDataAction, addDataAction);

I have actions coupled with buttons as well as hotkeys. I want to animate(animation similar to the one exhibited during mouse click) the button when its respective action is triggered thru hotkeys. Is that possible?

I am doing as follows:

        btnAdd.setAction(addDataAction);
        panelAdd.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                .put(KeyStroke.getKeyStroke("ctrl ENTER"), addDataAction);
        panelAdd.getActionMap().put(addDataAction, addDataAction);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2025-01-11 10:48:19

这对我来说很有效(不优雅但有效)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Test extends JFrame {

    public static void main(String[] args) {
        Test t = new Test();
        final JButton button = new JButton();
        AbstractAction action = new AbstractAction("Hello World!") {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.getModel().setArmed(true);
                button.getModel().setPressed(true);
                Timer t = new Timer(200, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button.getModel().setArmed(false);
                        button.getModel().setPressed(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                // Do action stuff
            }
        };
        button.setAction(action);

        JPanel panel = new JPanel();
        panel.add(button);

        panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ctrl ENTER"), action);
        panel.getActionMap().put(action, action);

        t.add(panel);

        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.pack();
        t.setVisible(true);
    }
}

来自 ButtonModel 的 Java API 文档

将鼠标按在按钮顶部可以使模型既武装又按下。只要鼠标保持按下状态,模型就会保持按下状态,即使鼠标移到按钮之外也是如此。相反,模型仅在鼠标在按钮范围内保持按下状态时才处于武装状态(鼠标可以移入或移出按钮,但模型仅在按钮内花费的时间部分处于武装状态)。当模型装备时释放鼠标时,将触发按钮并触发 ActionEvent - 意味着在先前按下该按钮(且尚未释放)后在按钮上方释放鼠标。释放鼠标后,模型将变为无武装且未按下。

This does the trick for me (not elegant but it works)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Test extends JFrame {

    public static void main(String[] args) {
        Test t = new Test();
        final JButton button = new JButton();
        AbstractAction action = new AbstractAction("Hello World!") {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.getModel().setArmed(true);
                button.getModel().setPressed(true);
                Timer t = new Timer(200, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button.getModel().setArmed(false);
                        button.getModel().setPressed(false);
                    }
                });
                t.setRepeats(false);
                t.start();
                // Do action stuff
            }
        };
        button.setAction(action);

        JPanel panel = new JPanel();
        panel.add(button);

        panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ctrl ENTER"), action);
        panel.getActionMap().put(action, action);

        t.add(panel);

        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.pack();
        t.setVisible(true);
    }
}

From the Java API docs for ButtonModel:

Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed - meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released). Upon mouse release, the model becomes unarmed and unpressed.

深海里的那抹蓝 2025-01-11 10:48:19

请参阅 AbstractButton.setPressedIcon(Icon)(以及接受 Icon 的相关方法)。我们的想法是将非动画图标设置为标准图标,并设置动画 GIF 作为替代图标。

See AbstractButton.setPressedIcon(Icon) (and related methods that accept an Icon). The idea would be to set a non-animated icon as the standard icon, and an animated GIF as the alternate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文