按空格键时打印文本

时间:2017-08-14 20:01:32

标签: java swing keyevent

这是我的主程序发生的一个例子。我想在按空格键时调用一个方法我想调用一个方法但是这个例子我写的只是打印出文本并更改一个布尔值是不行的。我不确定我在做什么导致它无法正常工作。

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test implements KeyListener {

    static boolean spacedPressed = false;
    static JTextField text = new JTextField();
    public static void main(String s[]) {

        JFrame frame = new JFrame("JFrame Test");

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        JLabel label = new JLabel("Test Label");

        JButton button = new JButton();
        button.setText("Press me");



        panel.add(label);
        panel.add(button);


        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        if(button.getModel().isPressed()){
            System.out.println("pressed");
            panel.add(text);
            System.out.println(spacedPressed);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            System.out.println("Pressed");
            spacedPressed = true;
            text.setText("Press worked.");
        } else {
            System.out.println("not pressed");
            text.setText("Press didn't work.");
            spacedPressed = false;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
}

2 个答案:

答案 0 :(得分:1)

您需要将keyListener添加到组件中,例如按钮:

    button.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                System.out.println("Pressed");
                spacedPressed = true;
                text.setText("Press worked.");
            } else {
                System.out.println("not pressed");
                text.setText("Press didn't work.");
                spacedPressed = false;
            }
        }
    });

目前您正在编写代码inside KeyKistener,它没有绑定到任何组件

答案 1 :(得分:1)

所以这里是修改后的代码,它将打印关键事件(我从面板中删除了按钮,因此它没有捕获空格键事件):

public class Test_1 implements KeyListener {

static boolean spacedPressed = false;
static JTextField text = new JTextField();

public static void main(String s[]) {

    JFrame frame = new JFrame("JFrame Test");

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());

    JLabel label = new JLabel("Test Label");

    JButton button = new JButton();
    button.setText("Press me");
    frame.addKeyListener(new Test_1());

    panel.add(label);
    //      panel.add(button);

    frame.add(panel);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    if (button.getModel().isPressed()) {
        System.out.println("pressed");
        panel.add(text);
        System.out.println(spacedPressed);
    }
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_SPACE) {
        System.out.println("Pressed");
        spacedPressed = true;
        text.setText("Press worked.");
    } else {
        System.out.println("not pressed");
        text.setText("Press didn't work.");
        spacedPressed = false;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println("Released");
}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println("Typed");
}
}

按JFrame上的空格键输出样本:

Pressed
Typed
Released