[英]I created code for controlling a image in swing, but I can't see anything, why?
I run this code and what I see is just big blank frame, why can't I see the image of car on it, I created a panel that contains a label that holds a image of car, I created a keylkisteners for backward and foreward, I run it and see nothing, just the blunk frame? 我运行这段代码,然后看到的只是一个大的空白框,为什么我看不到汽车的图像,所以我创建了一个面板,其中包含一个持有汽车图像的标签,我创建了一个向后和向前的键盘锁,我运行它时什么也没看到,只是笨拙的帧?
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();
}
}
Try the .setVisible() method ! 尝试.setVisible()方法! Also, the swing it needs to paint itself . 另外,它需要自己绘制秋千。 but first try the setVisible method 但首先尝试setVisible方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.