Java Jframe正在关闭所有键盘输入

时间:2014-08-26 16:47:09

标签: java swing jframe keylistener keyevent

Jframe正在关闭键盘的任何输入,只需要在用户输入转义键时关闭它。如果存在已知问题,则无法找到类似的示例请提供链接。

package jframe_no_decoration;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;


public class Jframe_no_decoration {

    public static void main(String args[]) {

        int FrameWidth = 400;
        int FrameHeight = 350;


        JFrame frame = new JFrame ();

        frame.setResizable(false);
        frame.setBounds(0, 0, FrameWidth, FrameHeight);
        frame.setLocationRelativeTo(null);
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().setBackground(Color.black);

        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {

                if( e.getKeyCode() == KeyEvent.VK_ESCAPE);
                System.exit(0);
            }

        });

    }



}

Jframe正在关闭键盘的任何输入,只需要在用户输入转义键时关闭它。如果存在已知问题,则无法找到类似的示例请提供链接。

5 个答案:

答案 0 :(得分:1)

删除;在你的if语句之后,因为它结束了语句。

if( e.getKeyCode() == KeyEvent.VK_ESCAPE); //if the key code is VK_ESCAPE, do nothing
System.exit(0); //in any case, exit the application

答案 1 :(得分:1)

  

“你能告诉我一个例子,我将把密钥绑定放在KeyEvent上吗?”

其他人已经发现了明显的问题,但没有解决你最终将面临的另一个问题。这个问题是焦点问题。这是键绑定比KeyListener更受欢迎的主要原因之一。您可以通过阅读How to Use Key Bindings

获取更多信息

这是基于您的程序的最简单的示例(请注意,它看起来与使用KeyListener完全不同 - 因此您需要查看教程以了解所有内容的含义)。

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

public class SimpleKeyBindDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame("Demo");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new JLabel("Type Esc to exit"));

                // get the contentPane of the frame
                JPanel panel = (JPanel)frame.getContentPane();
                // bind the Escape key to the contentPane
                addKeyBindToComponent(panel, "ESCAPE", "random");

                frame.setSize(400,  400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private static void addKeyBindToComponent(
            JComponent component, String key, String identifier) {

        InputMap imap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        imap.put(KeyStroke.getKeyStroke(key), identifier);
        ActionMap amap = component.getActionMap();
        amap.put(identifier, new AbstractAction(){
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }
}

另请注意SwingUtilities.invokeLater。这就是你应该如何在Event Dispatch Thread上初始化你的swing应用程序。请点击Initial Threads

了解更多信息

答案 2 :(得分:0)

你正在做(注意...... ESCAPE后的分号):

if( e.getKeyCode() == KeyEvent.VK_ESCAPE);
System.exit(0);

如果你这样做,那就是一样:

if( e.getKeyCode() == KeyEvent.VK_ESCAPE) {
   // nothing happen
}
System.exit(0);

正确的是:

if( e.getKeyCode() == KeyEvent.VK_ESCAPE) {
    System.exit(0);
}

此致

答案 3 :(得分:0)

你写过System.exit(0);在if语句之后用“;”补充。 只需删除“;”在if语句的结束括号后面,它很好。 就像这样:

if (e.getKeyCode() == KeyEvent.VK_Escape) System.exit(0);

答案 4 :(得分:0)

希望为将来的观众发布此信息。

    package jframe_no_decoration;

    import java.awt.event.ActionEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.*;
    import javax.swing.*;


    public class Jframe_no_decoration {

        public static void main(String[] args) {


            //looking into swingutilities and canvas to make sure both methods work together.
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {

            JFrame frame = new JFrame ();
            JPanel panel = (JPanel)frame.getContentPane();

            addKeyBindToComponent(panel, "ESCAPE", "random");


            int FrameWidth = 400;
            int FrameHeight = 350;
// setBounds() allowed me to set frame in the center of the screen and decided not to use .setSize()
            frame.setBounds(0,0, FrameWidth, FrameHeight);


            // frame.setLayout(new GridBagLayout()); optional will be using Canvas
            // frame.add(new JLabel("Type Esc to exit")); Timer, FadingLabel,   Fader.FadeMode(jmonkeyengine).


            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setResizable(false);

            // setLocationRelativeTo() gets x y coordinate from setBounds() to center frame on screen.
            frame.setLocationRelativeTo(null);

            frame.setUndecorated(true);
            frame.setVisible(true);
            frame.getContentPane().setBackground(Color.black);




            }   
        });

    }   

        // keyBinding information listed in the comments.   
        private static void addKeyBindToComponent( JComponent component, String key, String identifier) {

                InputMap keyboardmap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                keyboardmap.put(KeyStroke.getKeyStroke(key), identifier);

                ActionMap framemap = component.getActionMap();
                framemap.put(identifier, new AbstractAction(){

                public void actionPerformed(ActionEvent e) {

                    System.exit(0);
                }

            });

        }



    }