如何停止空格键触发按钮的点击效果? [英] How to stop spacebar from triggering click effect on a button?

查看:489
本文介绍了如何停止空格键触发按钮的点击效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个jframe并添加了一个按钮,单击该按钮后,它要求u按下任何按钮,该按钮也会显示在该按钮上.
(其显示应如下所示->单击我"->按任意按钮"->空格键")
我的第一个问题是,我不想通过按空格键从单击我"变为按任意按钮".
我的第二个问题是,当我在按任意键"处并且按空格键时,释放时,它返回到按任意键",而不是停留在空格键"上.
这是我的代码.

I have created a jframe and i have added a button which, after it get clicked, it asks u to press any button, which also displays on the button.
(its displays should go like this -> "Click Me" -> "Press Any Button" -> "Space Bar")
My problem no.1 is that, i dont want to go from "Click Me" to "Press Any Button" by pressing the spacebar.
And my problem no.2 is that, when i am at "Press Any Button" and i press spacebar, on release, it goes back to "Press Any Button" instead of staying at "Space Bar".
Here is my code.

    public class Test {

/**
 * @param args the command line arguments
 */

static class starton implements ActionListener {
    private JButton button;
    private JFrame frame;

    public starton(JButton button, JFrame frame) {
        this.button = button;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
        button.setText("Press A Button");
        button.setSize(button.getPreferredSize());
        button.addKeyListener(
        new KeyListener() {
            @Override 
            public void keyPressed(KeyEvent e){
                String text = null;

                    char a = e.getKeyChar();
                    text = ""+a+"";
                    if (a == ' '){
                        text = "Space Bar";
                    }

                button.setText(""+text+"");
                button.setSize(button.getPreferredSize());
                button.removeKeyListener(this);
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent ke) {
            }
        });
    }
}

public static void main(String[] args) throws IOException {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    int frame1w = 600;
    int frame1h = 400;

    JFrame frame1 = new JFrame("Foo");
    frame1.setSize(frame1w, frame1h);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);
    frame1.setContentPane(contentPane);

    JButton button1 = new JButton("Click Me");
        button1.setSize(button1.getPreferredSize());
        button1.addActionListener(new starton(button1, frame1));
            // add more code here
        contentPane.add(button1);
        frame1.setVisible(true);
}

}

推荐答案

JButton安装一系列键绑定以控制用户输入

The JButton installs a series of key bindings to control user input

您可以使用...来检查这些内容.

You can inspect what these are using something like...

JButton btn = new JButton("Test");
InputMap im = btn.getInputMap();
for (KeyStroke ik : im.allKeys()) {
    System.out.println(ik + " = " + im.get(ik));
}

在我的系统上,它会打印

On my system, it prints

pressed SPACE = pressed
released SPACE = released

这告诉我 Space 键绑定到操作键pressedreleased

This tells me that the Space key is bound to the action keys pressed and released

要禁用这些键,您需要提供自己的绑定...

In order to disable these keys, you need to provide your own binding...

Action blankAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
    }
};

ActionMap am = btn.getActionMap();
am.put("pressed", blankAction);
am.put("released", blankAction);

这只是将pressedreleased绑定替换为不执行任何操作的空Action.

This just replaces the pressed and released bindings with a empty Action which does nothing.

现在,要提一个警告-键绑定在不同平台/外观上可能有所不同;您还应该注意,用户通常会对某些控件可以做什么和不能做什么进行预先定义,更改它们会影响他们对程序的反应

Now, a word of warning - key bindings can be different on different platforms/look and feels; You should also beware that users often have a pre-defined expectation of what certain controls can and can't do, changing them will affect how they react to you program

关于第二个问题,button仍使用与您最初注册的相同的ActionListener,因此每当您按下 Space 时,它都会再次触发ActionListener,并添加一个新的KeyListener这将使您的问题更加复杂

As to your second problem, the button is still using the same ActionListener you originally registered, so whenever you press Space, it's triggering the ActionListener again, and adding a new KeyListener which is going to compound your problems

您要么想为两个按钮使用单独的ActionListener,要么想在第一次被触发时从按钮中删除ActionListener-我想第二遍,它更容易理解代码

You either want to use a separate ActionListener for both buttons or you want to remove the ActionListener from the button when it's first triggered - I'd go for the second, it's easier to understand the code

不,我是说,如果我禁用空格键的按",当我在按任意按钮"时是否可以按它?

no, i mean, if i disable the "press" for spacebar, would i be able to press it when i am at "Press Any Button"?

最简单的解决方案是使用两个不同的按钮,一个带有ActionListener来设置另一个按钮,另一个按钮上带有一个KeyListener.

The simplest solution would be to use two different buttons, one with an ActionListener which set up the other button, which had a KeyListener attached to it.

如果由于某种原因您确实"不想这样做,那么您需要在按钮首次触发时从按钮中删除ActionListener

If, for some reason you "really" don't want to do that, then you need to remove the ActionListener from the button when it's first triggered

static class starton implements ActionListener {

    //...

    public void actionPerformed(ActionEvent e) {
        button.setText("Press A Button");
        button.setSize(button.getPreferredSize());
        button.addKeyListener(...);
        button.removeActionListener(this);
    }
}

这篇关于如何停止空格键触发按钮的点击效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆