gpt4 book ai didi

java - 等待在循环内按下一个键

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:20 25 4
gpt4 key购买 nike

我有一个 Java 应用程序,我想等待按下一个键来执行另一个操作。直到现在我发现:

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
//Right arrow key code
}
}

但我希望它在循环内或等待键匹配的东西。

请帮忙。

更新:

我希望程序一直等到按键被按下,当它被按下时我想触发另一个 Action 。例如:

public void something(){
//do something
wait until a key is pressed
if( key pressed is a arrow key){
something();
}else{
wait for the key to be pressed
}
}

public void dootherthing(){
//do other thing
}

我在 swing 工作,我不希望 GUI 不负责任。即当我通过单击按钮调用 dootherthing 时。它应该这样做,等待应该结束。

最佳答案

Swing(和大多数 GUI)是事件驱动的环境。也就是说,某些事情发生了,您对此做出了 react 。

让循环等待某种 Action 有点违反直觉(恕我直言)。

一般来说,您应该尽可能避免使用 KeyListener。他们在重点方面存在问题。 key bindings API 有办法克服这个限制。

它允许您针对 Action 注册一个 KeyStroke 并允许您的程序坐下来等待事情发生...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class KeyBindings {

public static void main(String[] args) {
new KeyBindings();
}

public KeyBindings() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;

add(new KeyPane(KeyEvent.VK_UP, 0), gbc);

gbc.gridy = 2;
add(new KeyPane(KeyEvent.VK_DOWN, 0), gbc);

gbc.gridx = 0;
gbc.gridy = 1;
add(new KeyPane(KeyEvent.VK_LEFT, 0), gbc);

gbc.gridx = 2;
add(new KeyPane(KeyEvent.VK_RIGHT, 0), gbc);

}
}

public class KeyPane extends JPanel {

public KeyPane(int keyCode, int modifier) {

setBorder(new LineBorder(Color.DARK_GRAY));

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(keyCode, modifier, false), "keyPressed");
im.put(KeyStroke.getKeyStroke(keyCode, modifier, true), "keyReleased");

am.put("keyPressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setBackground(Color.RED);
}
});

am.put("keyReleased", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setBackground(UIManager.getColor("Panel.background"));
}
});

}

@Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
}

}

关于java - 等待在循环内按下一个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18326958/

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