[英]I created code for controlling a image in swing, but I can't see anything, why?
我運行這段代碼,然后看到的只是一個大的空白框,為什么我看不到汽車的圖像,所以我創建了一個面板,其中包含一個持有汽車圖像的標簽,我創建了一個向后和向前的鍵盤鎖,我運行它時什么也沒看到,只是笨拙的幀?
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方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.