我运行这段代码,我看到的只是一个大空白框架,为什么我看不到汽车上的图像,我创建了一个包含标签的面板,其中包含汽车图像,我创建了一个向后和向前的keylkisteners,我运行它,什么也看不见,只是钝器框架?
public class carGame implements KeyListener {
JFrame frame;
JPanel panel;
JLabel carPane;
public carGame(){
frame=new JFrame();
frame.setBounds(300, 400, 1200, 600);
panel=new JPanel();
panel.setBounds(300, 400, 1200, 100);
ImageIcon car=new ImageIcon("rsz_1car-blogspot-blue.png");
carPane=new JLabel();
carPane.setBounds(400, 400, 100, 100);
carPane.setIcon(car);
panel.add(carPane);
frame.add(panel);
frame.show(true);
frame.setVisible(true);
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
this.carPane.setBounds(carPane.getX()+30, carPane.getY(), carPane.getWidth(), carPane.getHeight());
if(e.getKeyCode()==KeyEvent.VK_LEFT)
this.carPane.setBounds(carPane.getX()-30, carPane.getY(), carPane.getWidth(), carPane.getHeight());
}
public class testing {
public static void main(String[] args) {
// TODO Auto-generated method stub
//windowMa frame=new windowMa();
carGame game=new carGame();
}
}
答案 0 :(得分:0)
尝试使用.setVisible()方法!此外,它需要自己涂抹。但首先尝试使用setVisible方法