我运行这段代码,我看到的只是大的空白帧,为什么我看不到上面的汽车图像,我创建了一个面板,其中包含一个包含汽车图像的标签,我创建了一个用于向后和向前的 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();
}
}
试试 .setVisible() 方法!此外,秋千它需要自己画 .但首先尝试 setVisible 方法