嘿,夥計們需要你們對我在代碼中開發的java問題的幫助,我已經創建了一個基本上被用作賽車模擬器的GUI,我的事件爲我的開始事件事件這是一個開始比賽的按鈕,iv只完成比賽的第一部分,但它沒有工作,所以我想在我繼續前解決問題。它很難解釋它會更容易顯示代碼(下面是哪個),我不想嘗試用勺子喂,所以請不要開始給我不好的投票,並說我沒有給出足夠的細節,因爲我真的不知道什麼我可以說更多的是我要寫的東西。我不知道問題是什麼。當我調試它(因爲即時通訊使用Eclipse IDE)不工作的部分只是使整個窗口變黑,沒有什麼顯示出來大約一分鐘,然後它跳過大部分代碼,甚至不做其餘。如果你需要更多的信息,請問我,我會告訴你只有人類,而不知道你想讓我推遲的每一個細節。我有很多班,但他們可能會讓你更加困惑,所以我只會粘貼我遇到問題的班。Java GUI不斷懸掛
(注:SportCar-是一個超級班,有5個子班(Astonmatin,Audi,法拉利,保時捷,蘭博基尼))
(注意: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");
}
}
當然不是所有的代碼都是相關的。嘗試通過刪除GUI元素和功能來減少代碼示例,直到找到使其崩潰的位。 – millimoose 2013-03-19 23:08:15
另外,對於崩潰,您應該提供堆棧跟蹤,並註釋您的代碼示例中堆棧跟蹤元素指向的位置。 – millimoose 2013-03-19 23:09:35
哈哈,這是我試圖降低它,但以前,如果我從班上拿出代碼人們對我很生氣,所以試圖安撫每個人,大部分是相關的,它表明我試圖達到甚至有人將不知道:)。至於堆棧跟蹤我該怎麼做,它是在調試部分內,因爲我沒有在它運行的編譯器上有任何錯誤? – dawodus 2013-03-19 23:09:56