gpt4 book ai didi

java - 按下空格键时打印文本

转载 作者:行者123 更新时间:2023-12-02 02:39:48 25 4
gpt4 key购买 nike

这是我的主程序正在发生的示例。我想在按下空格键时调用一个方法 我想调用一个方法,但我编写的这个示例仅打印出文本并更改 boolean 值不起作用。我不确定我在做什么导致它不起作用。

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

}
}

最佳答案

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

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

关于java - 按下空格键时打印文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45682192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com