我正在尝试制作一个程序,使用箭头键在java swing窗口中移动一个圆圈。键绑定工作正常,但显示圆圈总是有问题。这是代码:
public class ShapesMove extends JFrame{
public static int x = 40;
public static int y = 40;
public static void main(String[] args){
final JFrame frame = new JFrame("Movement of 2d Shapes");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = (JPanel) frame.getContentPane();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Action actionRight = new AbstractAction(){
public void actionPerformed(ActionEvent actionRightEvent){
x++;
}
};
Action actionLeft = new AbstractAction(){
public void actionPerformed(ActionEvent actionLeftEvent){
x--;
}
};
Action actionUp = new AbstractAction(){
public void actionPerformed(ActionEvent actionUpEvent){
y++;
}
};
Action actionDown = new AbstractAction(){
public void actionPerformed(ActionEvent actionDownEvent){
y--;
}
};
KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
KeyStroke left = KeyStroke.getKeyStroke("LEFT");
KeyStroke up = KeyStroke.getKeyStroke("UP");
KeyStroke down = KeyStroke.getKeyStroke("DOWN");
InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(right, "RIGHT");
inputMap.put(left, "LEFT");
inputMap.put(up, "UP");
inputMap.put(down, "DOWN");
content.getActionMap().put("RIGHT", actionRight);
content.getActionMap().put("LEFT", actionLeft);
content.getActionMap().put("UP", actionUp);
content.getActionMap().put("DOWN", actionDown);
}
public void draw(Graphics g){
g.drawOval(x, y, 60, 60);
}
}
我没有包含导入行,因为我知道我拥有所有正确的模块。编译总是很好,但是当我运行它时,圆圈不会显示。我尝试使用相同的代码来显示它自己独立的fie,当我运行时,圆圈出现了,所以我在这里做错了什么?
答案 0 :(得分:2)
您需要覆盖paintComponent
来进行绘制。在您添加到JFrame
而不是框架本身的组件上执行此操作,因为JFrame
是一个具有contentPane的容器,这将使事情变得更复杂,并且不太灵活以进行进一步修改。
答案 1 :(得分:1)
更改了您的代码,它运行正常。覆盖paintComponent
方法并在其中调用您的draw
方法。并将JFrame
更改为JPanel
。
public class ShapesMove extends JPanel{
public static int x = 40;
public static int y = 40;
public static void main(String[] args){
final JFrame frame = new JFrame("Movement of 2d Shapes");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ShapesMove m = new ShapesMove();
frame.getContentPane().add(m);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Action actionRight = new AbstractAction(){
public void actionPerformed(ActionEvent actionRightEvent){
x++;
m.repaint();
}
};
Action actionLeft = new AbstractAction(){
public void actionPerformed(ActionEvent actionLeftEvent){
x--;
m.repaint();
}
};
Action actionUp = new AbstractAction(){
public void actionPerformed(ActionEvent actionUpEvent){
y++;
m.repaint();
}
};
Action actionDown = new AbstractAction(){
public void actionPerformed(ActionEvent actionDownEvent){
y--;
m.repaint();
}
};
KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
KeyStroke left = KeyStroke.getKeyStroke("LEFT");
KeyStroke up = KeyStroke.getKeyStroke("UP");
KeyStroke down = KeyStroke.getKeyStroke("DOWN");
InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(right, "RIGHT");
inputMap.put(left, "LEFT");
inputMap.put(up, "UP");
inputMap.put(down, "DOWN");
m.getActionMap().put("RIGHT", actionRight);
m.getActionMap().put("LEFT", actionLeft);
m.getActionMap().put("UP", actionUp);
m.getActionMap().put("DOWN", actionDown);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(x, y, 60, 60);
}
}