在下面的程序中,我试图根据按下的键更改标签的文本,但我不知道该怎么做。按下键时要执行的语句在TimerListener InnerClass的actionPerformed()方法中定义。但我不明白如何从那里访问标签。
package aircraftPackage;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Controller extends JPanel {
private static final long serialVersionUID = 1L;
public static final int STEP = 3;
private static final int TIMER_DELAY = STEP * 8;
private BufferedImage playerImage = null;
private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
enum Direction {
UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1),
LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0);
private int keyCode;
private int xDirection;
private int yDirection;
private Direction(int keyCode, int xDirection, int yDirection) {
this.keyCode = keyCode;
this.xDirection = xDirection;
this.yDirection = yDirection;
}
public int getKeyCode() {
return keyCode;
}
public int getXDirection() {
return xDirection;
}
public int getYDirection() {
return yDirection;
}
}
public Controller() {
for (Direction direction : Direction.values()) {
directionMap.put(direction, false);
}
setKeyBindings();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
}
private void setKeyBindings() {
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
for (final Direction direction : Direction.values()) {
KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
inMap.put(pressed, direction.toString() + "pressed");
inMap.put(released, direction.toString() + "released");
actMap.put(direction.toString() + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, true);
}
});
actMap.put(direction.toString() + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, false);
}
});
}
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
boolean moved = false;
for (Direction direction : Direction.values()) {
if (directionMap.get(direction)) {
if (direction.keyCode == 37) {
System.out.println("go LEFT");
} else if (direction.getKeyCode() == 39) {
System.out.println("go RIGHT");
} else if (direction.getKeyCode() == 38) {
System.out.println("go UP");
}
else if (direction.getKeyCode()==40){
System.out.println("go DOWN");
}
}
}
}
}
public static void createAndShowUI() {
JFrame frame = new JFrame("MoveIcon");
JPanel panel = new JPanel();
JLabel jl = new JLabel();
jl.setText("testing....");
frame.add(jl);
frame.add(panel);
frame.getContentPane().add(new Controller());
new Controller();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
@SuppressWarnings("static-access")
public void run() {
createAndShowUI();
}
});
}
}
实际上,我想要做的是,创建一个包含Netbeans中各种标签的漂亮表单(在那里更容易做到)并使用上面的代码只知道哪个键被按下并更改每个键的值相应的标签。 我尝试过,但它没有用。 请帮帮我。 感谢。
PS :您可以在某些问题或某些网站上找到上述代码的部分内容,因为我自己并没有写完所有内容。我以前没有任何关于keybindings / keyListeners等的经验。这只是我正在做的项目的一部分。
答案 0 :(得分:3)
我尝试删除您现在不需要的所有内容,并添加了JLabel
,其中显示了以下指示:
这应该让你开始。
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class Controller extends JPanel {
private static final long serialVersionUID = 1L;
private static final int STEP = 3;
private static final int TIMER_DELAY = STEP * 8;
private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
private JLabel lblDirection = new JLabel();
enum Direction {
UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN),
LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT);
private int keyCode;
private Direction(int keyCode) {
this.keyCode = keyCode;
}
public int getKeyCode() {
return keyCode;
}
}
public Controller() {
add(lblDirection);
for (Direction direction : Direction.values()) {
directionMap.put(direction, false);
}
setKeyBindings();
Timer timer = new Timer(TIMER_DELAY, new TimerListener());
timer.start();
}
private void setKeyBindings() {
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
for (final Direction direction : Direction.values()) {
KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
inMap.put(pressed, direction.toString() + "pressed");
inMap.put(released, direction.toString() + "released");
actMap.put(direction.toString() + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, true);
}
});
actMap.put(direction.toString() + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
directionMap.put(direction, false);
}
});
}
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder s = new StringBuilder("go ");
for (Direction direction : Direction.values()) {
if (directionMap.get(direction)) {
s.append(direction.name() + " ");
}
}
lblDirection.setText(s.toString());
}
}
public static void createAndShowUI() {
JFrame frame = new JFrame("KeyMapping");
frame.getContentPane().add(new Controller());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
}