| 
| 
查看: 1123|回复: 0
 | 
java code 问题~
[复制链接] |  
 |  | 
 
| 请问有大神可以帮帮我吗?为什么我的人物射出的子弹只会往下掉,而不能射上去呢?? 如果能帮忙真是感激不尽~~
 以下是我的code~
 
 1.class Fly
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import java.awt.Image;
 import javax.swing.Icon;
 import javax.swing.ImageIcon;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.KeyAdapter;
 import java.awt.BorderLayout;
 import javax.swing.JPanel;
 
 public class Fly extends JFrame implements KeyListener {
 public JLabel l1 = new JLabel();
 
 
 Image Icon1 = new ImageIcon(getClass().getResource("Shoot.png" + "")).getImage().
 getScaledInstance(100, 100, Image.SCALE_DEFAULT);
 Icon imgIcon1 = new ImageIcon(Icon1);
 
 Image Icon2 = new ImageIcon(getClass().getResource("images.jpg")).getImage().
 getScaledInstance(100, 100, Image.SCALE_DEFAULT);
 Icon imgIcon2 = new ImageIcon(Icon2);
 
 public Fly(){
 super ("Airoplane");
 setLayout(null);
 l1.setIcon(imgIcon1);
 l1.setBounds(200, 350,100,100);
 add(l1);
 this.addKeyListener(this);
 
 }
 @Override
 public void keyPressed(KeyEvent e){
 
 if (e.getKeyCode()==KeyEvent.VK_LEFT){
 l1.setLocation(l1.getX()-100,l1.getY());
 repaint();
 }
 if(e.getKeyCode()==KeyEvent.VK_RIGHT){
 l1.setLocation(l1.getX()+100,l1.getY());
 repaint();
 }
 if(e.getKeyCode()==KeyEvent.VK_SPACE){
 JLabel l2 = new JLabel();
 l2.setIcon(imgIcon2);
 add(l2);
 l2.setBounds(l1.getX(),l1.getY(),100,100);
 RunBaby r = new RunBaby(l2);
 r.start();
 repaint();
 }
 }
 
 @Override
 public void keyReleased(KeyEvent e){
 
 }
 @Override
 public void keyTyped(KeyEvent e){
 
 }
 
 }
 
 2.class RunBaby
 import javax.swing.JLabel;
 public class RunBaby implements Runnable {
 private Thread t ;
 private String threadName;
 private JLabel label;
 RunBaby(JLabel label){
 this.label = label;
 }
 
 public void run(){
 System.out.print("Running"+threadName);
 try{
 for(int i = 0; ;i++){
 label.setLocation(label.getX(),label.getY()+(i%10));
 Thread.sleep(500);
 }
 }
 
 catch (InterruptedException e){
 System.out.println("Thread"+threadName+"interrupted");
 }
 System.out.println("Thread"+threadName+"exiting");
 }
 
 
 public void start(){
 System.out.println("Starting"+threadName);
 if(t==null){
 t = new Thread(this);
 t.start();
 }
 }
 
 
 }
 
 3.Main Method RunFly
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 
 public class RunFly {
 public static void main(String [] args){
 Fly f = new Fly();
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.setVisible(true);
 f.setSize(500,500);
 f.setResizable(false);
 }
 }
 
 
 
 
 | 
 |  |  |  |
 
|  |  |  
|  |  |  |  | 
            本周最热论坛帖子 |