0

这是一个示例代码,我尝试了大多数 JButton 设置,但无法弄清楚。

import java.awt.event.*;

import javax.swing.*;

public class FailedMnemonic extends JFrame implements Runnable{

    /*
     * 
     * F4 to call button action
     * ESC to dispose Dialog
     * 
     * */

    public FailedMnemonic() {
        setSize(200, 100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        Panel p = new Panel(this);
        p.setBounds(0, 0, 200, 60);
        add(p);
    };

    public static void main(String args[]){
        FailedMnemonic f = new FailedMnemonic();
        f.setVisible(true);
    }

    @Override
    public void run() {

    }

    public class Panel extends JPanel{

        final JFrame f;
        Action a = new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                /*Here should be called the pressed animation from the button, dont know how
                 * maybe i should add the button as parameter on the dialog class so when is dispose the button returns to its original state*/
                Dialog d = new Dialog(f, "...", true);
                d.setSize(500, 200);
                d.setVisible(true);
            }
        };

        JButton b = new JButton();

        public Panel(JFrame f){
            this.f = f;
            setLayout(null);
            b.setBounds(0, 0, 150, 50);
            b.setAction(a);
            a.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_F4);
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "meh");
            getActionMap().put("meh", a);
            b.setText("CLICK ME");
            add(b);
        }

        public class Dialog extends JDialog{

            public Dialog(JFrame OWNER, String title, boolean modal){
                super(OWNER, title, modal);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                addEscapeListener(this);
            }

            public void addEscapeListener(final JDialog dialog) {
                ActionListener escListener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dialog.setVisible(false);
                    }};
                dialog.getRootPane().registerKeyboardAction(escListener,
                        KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                        JComponent.WHEN_IN_FOCUSED_WINDOW);

            }

        }

    }

}

导入没有进入块代码

4

1 回答 1

2

不要将您的课程称为“框架”和“对话”。有具有该名称的 AWT 组件,因此会让人感到困惑。使用更多描述名称(即使它是快速演示代码)。

使用助记符调用 JButton 按下动画

在 Windows 上,我在使用 Alt+F4 时会看到动画,这是助记符。当我使用 F4 时,我没有得到动画,这是键绑定。

这是有道理的,因为使用Key Bindings您只需将 a 映射KeyStrokeAction. 它不知道 Action 属于一个按钮。

如果您想查看按钮动画,那么我建议您需要:

  1. 向按钮添加一个普通的 ActionListener 以显示您的对话框
  2. 为键绑定创建一个操作。然后此 Action 将调用button.doClick().

请注意,您可能还想查看Escape Key 和 Dialog以获得更完整的操作。此操作将支持使用退出键关闭组合框下拉菜单。

于 2016-05-16T19:49:53.197 回答