0

嘿,伙计们需要你的帮助来解决我的代码中出现的这个 java 问题,我已经创建了一个 GUI,它本质上被用作赛车模拟器,我的事件为我的 GUI 工作,除了我的 startrace 事件,这是一个启动按钮比赛,iv 只完成了比赛的第一部分,但没有成功,所以我想在继续之前解决问题。很难解释我显示代码会更容易(如下所示),我不想被勺子喂食,所以请不要开始给我糟糕的选票并说我没有提供足够的细节,因为我真的不知道是什么更多我可以说我要写的。我不知道问题是什么。当我调试它(​​因为我使用 Eclipse IDE)时,不起作用的部分只会使整个窗口变黑,大约一分钟内什么都没有显示,然后它会跳过大部分代码,甚至不做其余的工作。如果您需要更多信息,请询问我,我会告诉您我只是人类,不知道您希望我推迟的每个细节。我有很多课程,但它们可能会让你们更加困惑,所以我只粘贴我遇到问题的课程。

(注:SportCar- 是一个超类,有 5 个子类(Astonmatin、Audi、Ferrari、Porsche、Lamborghini))

(注意:Track 本身是一个带有 GUI 界面的类,它是一个大类,使用 SportsCar 及其子类中的变量和方法,所有这些方法和变量都不是问题)

(注意:问题出在actionPerformed(ActionEvent startrace)中)我希望我提供了足够的细节,欢迎任何帮助。提前谢谢你。

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.awt.GridLayout;




public class Track extends JFrame {

    private JComboBox ChooseYourCar;
    private JComboBox ChooseOppsCar; 
    private JButton StartRace;
    private JTextArea CheckYourCarAtts;
    private JTextArea CheckOppsCarAtts;
    private JTextArea Commentry; 
    private String temp;
    private String temp1;
    private String[] Cars = {"Choose Car Please ","Aston martin","Audi","Ferrari","Porsche","Lamborghini"};
    private int counter=0;
    private JTextField[] Green= new JTextField[11];
    private JTextField[] Green1= new JTextField[11];
    private JPanel Track = new JPanel();
    private SportCar first;
    private SportCar Second;

    public Track(){
        JPanel Center = new JPanel();
        JPanel West = new JPanel();
        JPanel East= new JPanel();
        JPanel North = new JPanel();
        Track.setLayout(new GridLayout(2,11,0,3));


        while (counter!=11)
        {

            Green[counter]= new JTextField("");
            Green[counter].setBackground(Color.GREEN);
            Green1[counter]= new JTextField("");
            Green1[counter].setBackground(Color.GREEN);
            counter++;  
             }


        counter= 0;
        while (counter!=11){
        Track.add(Green[counter]);
        counter++;

        }

        counter= 0;
        while (counter!=11){
            Track.add(Green1[counter]);
            counter++;

        }



        add(West, BorderLayout.WEST);
        add(Center, BorderLayout.CENTER);
        add(East, BorderLayout.EAST);
        add(North,BorderLayout.NORTH);
        add(Track,BorderLayout.SOUTH);



        ChooseYourCar = new JComboBox(Cars);
        West.add(ChooseYourCar);
        ChooseOppsCar = new JComboBox(Cars);
        West.add(ChooseOppsCar);
        StartRace = new JButton("Start Race");
        East.add(StartRace);
        CheckYourCarAtts = new JTextArea(12,30);
        CheckYourCarAtts.setBackground(Color.RED);
        Center.add(CheckYourCarAtts);
        CheckOppsCarAtts = new JTextArea(12,30);
        CheckOppsCarAtts.setBackground(Color.RED);
        Center.add(CheckOppsCarAtts);
        Commentry = new JTextArea(2,50);
        North.add(Commentry);



        event chooseyourcar = new event();
        ChooseYourCar.addActionListener(chooseyourcar);
        event1 Chooseoppscar = new event1();
        ChooseOppsCar.addActionListener(Chooseoppscar);
        event2 startrace = new event2();
        StartRace.addActionListener(startrace);



    }
    public class event implements ActionListener{
        public void actionPerformed(ActionEvent chooseyourcar){
            if(ChooseYourCar.getSelectedItem()=="Aston martin")
            {
                SportCar AstonMartin = new Astonmartin(400,1.0,1200,220);
                String str=("ASTON MARTIN\n"+"This is the Car You have currently Selected \n" +"Horsepower: " +AstonMartin.getHorsepower()+"\n"+ "Weight: "+ AstonMartin.getWeight()+ "\n"+ "Topspeed: "+ AstonMartin.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ AstonMartin.acceleration()+ "\n"+ "The Fuel Consumption is: "+ AstonMartin.GetFuelConsumption());
                CheckYourCarAtts.setText(str);
            }
            if(ChooseYourCar.getSelectedItem()=="Audi")
            {
                SportCar Audi = new Audi(300,1.2,1200,200);
                String str=("AUDI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Audi.getHorsepower()+"\n"+ "Weight: "+ Audi.getWeight()+ "\n"+ "Topspeed: "+ Audi.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Audi.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Audi.GetFuelConsumption());
                CheckYourCarAtts.setText(str);
            }
            if(ChooseYourCar.getSelectedItem()=="Ferrari")
            {
                SportCar Ferrari = new Ferrari(440,0.8,900,260);
                String str=("FERRARI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Ferrari.getHorsepower()+"\n"+ "Weight: "+ Ferrari.getWeight()+ "\n"+ "Topspeed: "+ Ferrari.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Ferrari.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Ferrari.GetFuelConsumption());
                CheckYourCarAtts.setText(str);
            }
            if(ChooseYourCar.getSelectedItem()=="Porsche")
            {
                SportCar Porsche = new Porsche(380,0.9,1000,220);
                String str=("PORSCHE\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Porsche.getHorsepower()+"\n"+ "Weight: "+ Porsche.getWeight()+ "\n"+ "Topspeed: "+ Porsche.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Porsche.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Porsche.GetFuelConsumption());
                CheckYourCarAtts.setText(str);
            }
            if(ChooseYourCar.getSelectedItem()=="Lamborghini")
            {
                SportCar Lamborghini = new Lamborghini(440,0.7,900,250);
                String str=("LAMBORGHINI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Lamborghini.getHorsepower()+"\n"+ "Weight: "+ Lamborghini.getWeight()+ "\n"+ "Topspeed: "+ Lamborghini.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Lamborghini.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Lamborghini.GetFuelConsumption());
                CheckYourCarAtts.setText(str);
            }


}

}

        public class event1 implements ActionListener{
            public void actionPerformed(ActionEvent chooseoppscar){
                if(ChooseOppsCar.getSelectedItem()=="Aston martin")
                {
                    SportCar AstonMartin = new Astonmartin(400,1.0,1200,220);
                    String str=("ASTON MARTIN\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +AstonMartin.getHorsepower()+"\n"+ "Weight: "+ AstonMartin.getWeight()+ "\n"+ "Topspeed: "+ AstonMartin.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ AstonMartin.acceleration()+ "\n"+ "The Fuel Consumption is: "+ AstonMartin.GetFuelConsumption());
                    CheckOppsCarAtts.setText(str);
                }
                if(ChooseOppsCar.getSelectedItem()=="Audi")
                {
                    SportCar Audi = new Audi(300,1.2,1200,200);
                    String str=("AUDI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Audi.getHorsepower()+"\n"+ "Weight: "+ Audi.getWeight()+ "\n"+ "Topspeed: "+ Audi.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Audi.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Audi.GetFuelConsumption());
                    CheckOppsCarAtts.setText(str);
                }
                if(ChooseOppsCar.getSelectedItem()=="Ferrari")
                {
                    SportCar Ferrari = new Ferrari(440,0.8,900,260);
                    String str=("FERRARI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Ferrari.getHorsepower()+"\n"+ "Weight: "+ Ferrari.getWeight()+ "\n"+ "Topspeed: "+ Ferrari.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Ferrari.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Ferrari.GetFuelConsumption());
                    CheckOppsCarAtts.setText(str);
                }
                if(ChooseOppsCar.getSelectedItem()=="Porsche")
                {
                    SportCar Porsche = new Porsche(380,0.9,1000,220);
                    String str=("PORSCHE\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Porsche.getHorsepower()+"\n"+ "Weight: "+ Porsche.getWeight()+ "\n"+ "Topspeed: "+ Porsche.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Porsche.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Porsche.GetFuelConsumption());
                    CheckOppsCarAtts.setText(str);
                }
                if(ChooseOppsCar.getSelectedItem()=="Lamborghini")
                {
                    SportCar Lamborghini = new Lamborghini(440,0.7,900,250);
                    String str=("LAMBORGHINI\n"+"This is the Car You have currently Selected\n" +"Horsepower: " +Lamborghini.getHorsepower()+"\n"+ "Weight: "+ Lamborghini.getWeight()+ "\n"+ "Topspeed: "+ Lamborghini.getTopspeed()+ "\n"+ "The car goes from 0-60Mph(secs) in: "+ Lamborghini.acceleration()+ "\n"+ "The Fuel Consumption is: "+ Lamborghini.GetFuelConsumption());
                    CheckOppsCarAtts.setText(str);
                }
    }
}   

        public class event2 implements ActionListener{
            public void actionPerformed(ActionEvent startrace){ 
                if(ChooseYourCar.getSelectedItem()=="Aston martin")
                {
                    SportCar AstonMartin = new Astonmartin(400,1.0,1200,220);
                    first= AstonMartin;
                }
                if(ChooseYourCar.getSelectedItem()=="Audi")
                {
                    SportCar Audi = new Audi(300,1.2,1200,200);
                     first= Audi;
                }
                if(ChooseYourCar.getSelectedItem()=="Ferrari")
                {
                    SportCar Ferrari = new Ferrari(440,0.8,900,260);
                      first= Ferrari;
                }
                if(ChooseYourCar.getSelectedItem()=="Porsche")
                {
                    SportCar Porsche = new Porsche(380,0.9,1000,220);
                     first= Porsche;
                }
                if(ChooseYourCar.getSelectedItem()=="Lamborghini")
                {
                    SportCar Lamborghini = new Lamborghini(440,0.7,900,250);
                    first= Lamborghini;
                }


                if(ChooseOppsCar.getSelectedItem()=="Aston martin")
                {
                    SportCar AstonMartin = new Astonmartin(400,1.0,1200,220);
                    Second= AstonMartin;
                }
                if(ChooseOppsCar.getSelectedItem()=="Audi")
                {
                    SportCar Audi = new Audi(300,1.2,1200,200);
                     Second= Audi;
                }
                if(ChooseOppsCar.getSelectedItem()=="Ferrari")
                {
                    SportCar Ferrari = new Ferrari(440,0.8,900,260);
                      Second= Ferrari;
                }
                if(ChooseOppsCar.getSelectedItem()=="Porsche")
                {
                    SportCar Porsche = new Porsche(380,0.9,1000,220);
                    Second= Porsche;
                }
                if(ChooseOppsCar.getSelectedItem()=="Lamborghini")
                {
                    SportCar Lamborghini = new Lamborghini(450,0.7,900,250);
                    Second= Lamborghini;
                }

                ChooseYourCar.setVisible(false);
                ChooseOppsCar.setVisible(false);

                Commentry.setText("IT IS TIME TO GET THE SIMLATION UNDER WAY, THE TWO CARS HAVE BEEN CHOOSEN AND THERE ATTRIBUTES ARE BELOW.");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Track.setLocation(1, 1);
                Track.setBackground(Color.BLACK);
                Track.setLocation(2, 1);
                Track.setBackground(Color.ORANGE);
                Commentry.setText("THE CARS ARE NOW IN PLACE IN BLACK WE HAVE "+ first.getName()+"\n"+"WHILE OUR OTHER CAR IN ORANGE, WE HAVE "+ Second.getName());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Commentry.setText("LETS RACE");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Commentry.setText("3");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Commentry.setText("2");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Commentry.setText("1");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Commentry.setText("GO");


                double firstCurrentPosition = (first.getcurrentspeed()+first.getGrip());
                double secondCurrentPosition = (Second.getcurrentspeed()+Second.getGrip());
                if(firstCurrentPosition>secondCurrentPosition){
                    Track.setLocation(1, 1);
                    Track.setBackground(Color.GREEN);
                    Track.setLocation(2, 1);
                    Track.setBackground(Color.GREEN);

                    Track.setLocation(1, 3);
                    Track.setBackground(Color.BLACK);
                    Track.setLocation(2, 2);
                    Track.setBackground(Color.ORANGE);
                   Commentry.setText("WOW "+ firstCurrentPosition+ " "+ secondCurrentPosition);
                  }

        }

        }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
       Track window = new Track();
       window.setDefaultCloseOperation(EXIT_ON_CLOSE);
       window.setSize(750,530);
       window.setVisible(true);
       window.setTitle("Car Race Simulator");


       }


}
4

1 回答 1

3

我很难抓住你的问题,过了一会儿我明白标题“Java GUI 不断崩溃”实际上意味着“Java GUI 挂起”。

非常清楚的案例:您在“event2.actionPerformed()”方法中做了太多工作[因为 Thread.sleep() 调用],所以事件调度线程 [也称为 EDT],它调用了“actionPerfomed( )" 所有侦听器的方法,在该长时间运行的方法完成之前被阻塞。因此,在您按下 StartRace 按钮后,GUI“挂起”了很长一段时间!

简单的答案:使用SwingWorker(自 Java 6 起在 Java 运行时可用)!这里有两个很好的例子:

我会更详细地解释对 SwingWorker 的需求,但是网上已经有很多很好的解释,例如

PS
更多信息:在工人类中移动长时间运行的代码,例如

    class RaceWorker extends SwingWorker<Void, Void> {
        @Override
        protected Void doInBackground() throws Exception {
            // ... long running code here ...
            return null;
        }
    }

所以“actionPerformed()”方法看起来像:

    public void actionPerformed(ActionEvent startrace) {
        new RaceWorker().execute();
    }

PPS
顺便说一句......我预测你会很艰难,不是实现,而是代码:例如,一旦你尝试从 StartWorker 访问当前名为“Track”的成员变量,编译器就会混淆它带有“Track”类名,将拒绝编译。我的(未提出的)建议:首先清理方法、变量和成员变量的命名,然后使用 SwingWorker 进行重构。这样,您将节省大量时间和麻烦。这篇关于命名约定/Java的维基百科文章对常用 Java 代码约定进行了简要概述,这对于您的代码应该足够了[我个人确实使用“实例变量上的下划线”,但这是一个品味和经验的问题]。为了完整起见:“官方”甲骨文

于 2013-03-20T08:42:26.143 回答