按下 space 栏时打印文本
Printing out text when space bar is pressed
这是我的主程序正在发生的事情的一个例子。我想在按下空格键时调用一个方法我想调用一个方法但是我写的这个例子只是打印出文本并更改一个布尔值是行不通的。我不确定我在做什么导致它不起作用。
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
}
}
您需要为您的组件添加 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,它没有绑定到任何组件
所以这里是修改后的代码,它将打印按键事件(我从面板上删除了按钮,所以它没有捕捉到 space 按键事件):
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 上按下 space 键时的示例输出:
Pressed
Typed
Released
这是我的主程序正在发生的事情的一个例子。我想在按下空格键时调用一个方法我想调用一个方法但是我写的这个例子只是打印出文本并更改一个布尔值是行不通的。我不确定我在做什么导致它不起作用。
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
}
}
您需要为您的组件添加 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,它没有绑定到任何组件
所以这里是修改后的代码,它将打印按键事件(我从面板上删除了按钮,所以它没有捕捉到 space 按键事件):
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 上按下 space 键时的示例输出:
Pressed
Typed
Released