Swing(和大多数 GUI)是事件驱动的环境。也就是说,有些事情发生了,你对它做出反应。
循环等待某种动作有点违反直觉(恕我直言)。
一般来说,KeyListener
如果可以的话,你应该避免使用 s。他们在重点方面存在问题。键绑定API 有办法克服这个限制。
它允许你注册一个KeyStroke
反对一个Action
并允许你的程序坐下来等到事情发生......
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);
}
}
}