Summer's Blog

No life,No fun

[求助]自己编的一个JAVA小程序,出现BUG不知道是什么问题.

Summer posted @ 2008年6月05日 10:05 in Java with tags java 问题 触发器 , 1789 阅读

问题描述: 程序的主要目的是在一个Frame上面画四个小车,通过四个输出窗口输入速度,点start开始跑动,现在的问题是在输入了速度以后多次点击START后小车的速度会不断加快(按观察得是加倍的).初度判断是触发器的问题,但是由于ECLIPSE的DEBUG不会用...导致无法找到问题的根本原因所在.所以求教各位...

 

在线等解答,谢谢了.

 

代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

import javax.swing.*;


public class CarMain {
        public  static void main(String[] args){
                JFrame frame=new CarFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
        }
}

class CarFrame extends JFrame{
        
           public CarFrame(){
                   setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
                   setTitle("CarFrame");
                  
                   JPanel Timepanel=new JPanel();
                  
                   Timepanel.add(new JLabel("Car1:"));
                   Rate1Field= new JTextField("0",5);
                   Timepanel.add(Rate1Field);
                  
                   Timepanel.add(new JLabel("Car2:"));
                   Rate2Field= new JTextField("0",5);
                   Timepanel.add(Rate2Field);
                  
                   Timepanel.add(new JLabel("Car3:"));
                   Rate3Field= new JTextField("0",5);
                   Timepanel.add(Rate3Field);
                  
                
                   Timepanel.add(new JLabel("Car4:"));
                   Rate4Field= new JTextField("0",5);
                   Timepanel.add(Rate4Field);
                   panel= new CarPanel();
                   panel.CarlistInitialisation();
                   add(panel,BorderLayout.CENTER);
                  
                   addButton(Timepanel,"Start",
                                   new ActionListener()
                   {
                           public void actionPerformed(ActionEvent event){
                                   setRateto();
                                   for(int n=0;n<4;n++){
                                           panel.setRatetoCar(CarRate[n], n);
                                           Runnable r=new CarRunnable(panel.getCar(n),panel);
                                           Thread t=new Thread(r);
                                           t.start();
                                           }
                                  
                                  
                           }
                   });
                   addButton(Timepanel,"Close",new ActionListener(){
                           public void actionPerformed(ActionEvent event){
                                   System.exit(0);
                                  
                           }
                          
                           });
                   add(Timepanel,BorderLayout.NORTH);
                  
                  
                  
                  
                  
                  
                  
                   }
          
           public void setRateto(){
                         try{
                                   CarRate[0]=Integer.parseInt(Rate1Field.getText().trim());
                                   CarRate[1]=Integer.parseInt(Rate2Field.getText().trim());
                                   CarRate[2]=Integer.parseInt(Rate3Field.getText().trim());
                                   CarRate[3]=Integer.parseInt(Rate4Field.getText().trim());
                                  
                           }catch(NumberFormatException e){}
                        
                   }
          
           public void addButton(Container c,String title,ActionListener listener){
                 JButton button= new JButton(title);
                 c.add(button);
                 button.addActionListener(listener);
                
           }          
                  
                  
                  
                
            
            
             private CarPanel panel;
             private JTextField Rate1Field,Rate2Field,Rate3Field,Rate4Field;
             private int CarRate[]={0,0,0,0},mark=0,count=0;
             public static final int DEFAULT_WIDTH=600;
             public static final int DEFAULT_HEIGHT=400;
             public static final int STEPS=1000;
             public static final int DELAY=3;
        }
          
          

        class CarPanel extends JPanel{
                private Car[] Carlist=new Car[4];
               
                  public void add(Car car,int n){
                           Carlist[n]=car;
                          
                   }
                  public void setRatetoCar(double rate,int n){
                          Carlist[n].setRate(rate);
                  }
               
                public void CarlistInitialisation(){
                        Carlist[0]= new Car(0,0);
                     Carlist[1]= new Car(0,90);
                     Carlist[2]= new Car(0,180);
                     Carlist[3]= new Car(0,270);
                       
                }

                public void paintComponent(Graphics g){
                     int i=0,n=0;
                     super.paintComponent(g);
                     Graphics2D g2=(Graphics2D) g;
                     for(i=0;i<4;i++){
                          g2.fill(Carlist[i].getShape());
                     }
                          
                     }
                public Car getCar(int n){
                     return Carlist[n];
                }
                     
                }
          
          
          
class Car{
                        public Car(int n,int y){
                                dx=n;
                                this.y=y;
                        }
                        public void move(Rectangle2D bounds){
                                x+=dx;
                                if(y<400){
                                if(x<bounds.getMinX()){
                                        x=bounds.getCenterX();
                                }
                                if(x+XSIZE>=600)
                                        x=bounds.getMinX();}
                                else{x=0;y=0;}
                                       
                               
                        }
                        public Ellipse2D getShape(){
                                return new Ellipse2D.Double(x,y,XSIZE,YSIZE);
                        }
                        public Ellipse2D getShape(int Y){
                                return new Ellipse2D.Double(x,Y,XSIZE,YSIZE);
                        }
                        public void setRate(double rate){
                                dx=rate;
                        }
                        public double getRate(){
                                return dx;
                        }
                       
                        private static final int XSIZE =15;
                        private static final int YSIZE =15;
                        private double x=0;
                        private double y=0;
                        private double dx=1;
                        private double dy=1;
                }

class CarRunnable implements Runnable{
        private Car car;
        private Component component;
        public static final int DELAY=20;
        public CarRunnable(Car aCar,Component aComponent){
                car=aCar;
                component=aComponent;}
        public void run(){
                try{
                        while(true)
                        {
                                car.move(component.getBounds());
                                component.repaint();
                                Thread.sleep(DELAY);
                        }
                }catch(InterruptedException e){
                       
                }
        }
}
Head_small
Summer 说:
2008年6月09日 05:40

在博客上求教``

恩....如果有人解答就好,不过最终还是靠自己.

仔细DEBUG了一下还是自己把问题解决了.

这样挺好.

Avatar_small
ant 说:
2008年6月18日 06:27

bs帖代码求教的……

Avatar_small
professional cleanin 说:
2021年9月27日 16:39

Moreover, quality cleanup companies need to offer various services, like sanitary disposal and also other types involving cleaning solutions. After most, cleaning is not merely about ensuring that the home is easily wiped down plus the floors are generally mopped along with vacuumed. Cleaning is a lot more than that will, and it will require up time and effort you may better commit doing other pursuits like passing time with all your family members, relaxing, or building your small business.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter