简体   繁体   中英

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). 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. If i remove it then Ican press enter again and play the game.

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. Therefore, when you start you program, it gains default focucs. While it has default focus. It will consume the Enter key strokes.

Problem 2 - JPanel is not focusable, meaning it can never receive key board focus. From your description, I would assume you are using a KeyListener , which leads to

Problem 3 - Using KeyListener ... KeyListener will only respond to key events when the component it is registered to is focusable and has focus. You can over come this by using Key Bindings .

...Solutions...

  • Use JLabel instead of 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...
  • 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); }

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. 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);  
           } 
        });
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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