简体   繁体   English

仅在jbutton上启用鼠标事件-禁用jbutton的键盘事件

[英]Enable only mouse event over a jbutton - Disable keyboard event for jbutton

I have a problem. 我有个问题。 I created a game. 我创造了一个游戏。 When I open it I must press ENTER to start the game (just enter). 当我打开它时,我必须按Enter键才能开始游戏(只需输入)。 Now I upgraded the game with one button named "EXIT GAME". 现在,我用一个名为“退出游戏”的按钮升级了游戏。 I don't know why my enter key doesn't work anymore because of this button. 我不知道为什么我的Enter键由于这个按钮而不再起作用。 If i remove it then Ican press enter again and play the game. 如果我将其删除,则可以再次按Enter键并玩游戏。

I must set only click pressed event to that button or something like this? 我必须将“仅单击事件”设置为该按钮或类似的东西? Please help me. 请帮我。

public class LeftPanel extends JPanel implements ActionListener {
    JButton ExitGame;

    public LeftPanel(Tetris tetris) {
        this.tetris = tetris;
        setPreferredSize(new Dimension(400, 480));
        setBackground(Color.getHSBColor(17f, 0.87f, 0.52f));
        add(new JButton("Exit Game"));
        {
            ExitGame.addActionListener(this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

Problem 1 - The JButton is the only focusable component within your UI. 问题1- JButton是UI中唯一可聚焦的组件。 Therefore, when you start you program, it gains default focucs. 因此,当您开始编程时,它将获得默认焦点。 While it has default focus. 虽然具有默认焦点。 It will consume the Enter key strokes. 它将消耗Enter键。

Problem 2 - JPanel is not focusable, meaning it can never receive key board focus. 问题2- JPanel无法聚焦,这意味着它永远无法获得键盘聚焦。 From your description, I would assume you are using a KeyListener , which leads to 根据您的描述,我假设您使用的是KeyListener ,这会导致

Problem 3 - Using KeyListener ... KeyListener will only respond to key events when the component it is registered to is focusable and has focus. 问题3-使用KeyListener ... KeyListener仅在其注册到的组件可聚焦并具有焦点时才响应键事件。 You can over come this by using Key Bindings . 您可以使用Key Bindings克服这一点。

...Solutions... ...解决方案...

  • Use JLabel instead of JButton . 使用JLabel而不是JButton This will require you to register a MouseListener to the label in order to recieve notification of the mouse clicks, but it won't respond to key events... 这将需要您在标签上注册一个MouseListener ,以接收有关鼠标单击的通知,但它不会响应按键事件。
  • Better still, add a "Start" button as well... 更好的是,还要添加一个“开始”按钮...

You can try: 你可以试试:

    public class LeftPanel extends JPanel implements ActionListener {


    public LeftPanel(Tetris tetris) {
        this.tetris = tetris;
        setPreferredSize(new Dimension(400, 480));
        setBackground(Color.getHSBColor(17f, 0.87f, 0.52f));
        JButton ExitGame = new JButton("Exit Game");
        ExitGame.addActionListener(this);
        ExitGame.setActionCommand("Exit");
        add(ExitGame );

    }

    public void actionPerformed(ActionEvent e) {
        if("Exit".equals(e.getActionCommand())
            System.exit(0);
    }
}

This line looks like a syntax error: 这行看起来像语法错误:

add(new JButton("Exit Game"));
    {
        ExitGame.addActionListener(this);
    }

I think it should be something like this: 我认为应该是这样的:

ExitGame= new JButton("Exit");
this.add(ExitGame);
ExitGame.addActionListener(this);

I haven't tested this, but I think with some tweaking you should be able to get it to do what you want it to. 我没有测试过,但是我认为通过一些调整,您应该能够使它完成您想要的操作。 I hope that works! 希望能成功!

-Frank -坦率

public void actionPerformed(ActionEvent e) {
    if("Exit".equals(e.getActionCommand())

System.exit(0); System.exit(0); } }

As ActionlListener can be triggered by both Mouse and Keyboard, but now user only want to response mouse event, so change the action listener to mouselistener. 由于ActionlListener既可以由鼠标也可以由键盘触发,但是现在用户只想响应鼠标事件,因此将动作侦听器更改为mouselistener。 Tested and passed. 经过测试并通过。

public class LeftPanel extends JPanel implements ActionListener {
    JButton ExitGame;

    public LeftPanel(Tetris tetris) {
        this.tetris = tetris;
        setPreferredSize(new Dimension(400, 480));
        setBackground(Color.getHSBColor(17f, 0.87f, 0.52f));
        ExitGame= new JButton("Exit Game")
        add(ExitGame);
        ExitGame.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e) {
               System.exit(0);  
           } 
        });
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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