开发者

Close window on pressing of certain key

I just started using java and am not very good, so if you could help me that would be awesome! I'm making a Swing applica开发者_运维知识库tion and want to close it only when a certain key is pressed. Is there a way to do this?


Going against the other X answers here, I'm going to recommend that you not use a KeyListener but rather use key bindings. This is a higher level abstraction, and helps you avoid focus issues that come with use of KeyListeners. You can find out more about key bindings in the Swing tutorials here: How to use Key Bindings


When you want a program to react immediately once a key is pressed, you use keyboard events and the KeyListener interface. Unlike the ActionListener or ItemListener interfaces, the KeyListener interface must implement three methods:

  • void keyPressed(KeyEvent) -- A method called the moment a key is pressed

  • void keyReleased(KeyEvent) -- A method called the moment a key is released

  • void keyTyped(KeyEvent) -- A method called after a key has been pressed and released

Although all of these methods must be present in your code, you don't have to have any statements inside of them.

Call the getKeyChar() method to find out which key was pressed. As the method implies, this is returned as a char value. However, this method only works for letter keys, number keys, and punctuation keys.

To monitor any key on the keyboard, use the getKeyCode() method. This is returned as an int value. You can follow that up with a getKeyText() method, with the int value as the argument. This will return the actual name of the key (i.e. Home, F2, etc.).

You want a window to close only when a certain key is pressed. Below is an example of how you would go about doing that:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class example extends JFrame implements KeyListener {
    JLabel closeLabel = new JLabel("Press the \"x\" key to close me!");

    public example() {
        super("Close me!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        closeLabel.addKeyListener(this);
        closeLabel.setFocusable(true);
        add(closeLabel);
        pack();
        setVisible(true);
    }

    public void keyTyped(KeyEvent input) {
        char key = input.getKeyChar();
        if (key == 'x') System.exit(0);
    }

    public void keyPressed(KeyEvent txt) {
        //do nothing
    }

    public void keyReleased(KeyEvent txt) {
        //do nothing
    }
}


You have to take a look at the KeyListener interface, and i suggest you read this kind of article that explain clearly the swing way of doing things, here


Add a KeyListener and check if your key was pressed. Here's a tutorial on how to do that. http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html


you must register an handler to handle keypressed on the swing component you want to react. On the handler, close the window.

This is the observer pattern http://www.javaworld.com/javaworld/javaqa/2001-05/04-qa-0525-observer.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜