2011-02-24 51 views
0

此gui應該在稱爲「系統」的框架面板上繪製運動圖像。但首先我需要製作這些物體。在這裏,我試圖將它們添加到數組中,並在其他類中使用該數組。但數組一直空着! 我想這個問題是在聲明(代碼的底部)。沒有異常拋出,因爲我讓其他類只在這個數組(天文館)不是空的時候才能執行(它應該像那樣工作,因爲其他移動取決於創建的行星(那些對象))。但是,如果它是空的,這意味着什麼都沒有聲明... 如果我想填充在事件監聽器中執行的線程數組我應該怎麼做?無法在線程中填充數組

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

public class GUI extends Frame implements WindowListener,ActionListener { 
cpWindow window1 = new cpWindow(); 
cmWindow window2 = new cmWindow(); 
Delete window3 = new Delete(); 
SolarSystem system = new SolarSystem(300,300); 
Planet[] Planetarium = null;  // using this to make an array 
Moon[] Moonarium = null; 
/** 
* Frame for general window. 
*/ 
public void createFrame0(){ 
JFrame f0 = new JFrame("Choose what you want to do"); 
f0.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f0.addWindowListener(this); 
f0.setLayout(new GridLayout(3,1)); 
JButton cP = new JButton("Create a planet"); 
JButton cM = new JButton("Create a moon"); 
JButton Delete = new JButton("Annihilate a planet or moon"); 
f0.add(cP); 
f0.add(cM); 
f0.add(Delete); 
cP.addActionListener(this); 
cM.addActionListener(this); 
Delete.addActionListener(this); 
cP.setActionCommand("1"); 
cM.setActionCommand("2"); 
Delete.setActionCommand("3"); 
f0.pack(); 
f0.setVisible(true); 
} 
/** 
* Frame for planet adding window. 
*/ 
class cpWindow implements ActionListener,WindowListener{ 
JLabel name1 = new JLabel("Name"); 
JLabel color1 = new JLabel("Color"); 
JLabel diam1 = new JLabel("Diameter"); 
JLabel dist1 = new JLabel("Distance"); 
JLabel speed1 = new JLabel("Speed"); 
JTextField name2 = new JTextField(); 
JTextField color2 = new JTextField(); 
JTextField diam2 = new JTextField(); 
JTextField dist2 = new JTextField(); 
JTextField speed2 = new JTextField(); 
double distance; 
int Speed; 
double diameter; 

public void createFrame1() { 
    JFrame f1 = new JFrame("Add planet"); 
    f1.addWindowListener(this); 
    f1.setLayout(new GridLayout(6,2,5,5)); 
    JButton mygt = new JButton("Create planet"); 
    mygt.addActionListener(this); 
     name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText("60");spe ed2.setText("2"); 
    f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1); 
    f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2); 
    f1.add(mygt); 
    f1.pack(); 
    f1.setVisible(true); 
} 
public void createVariables(){ 
    try { 
      distance = Double.parseDouble(dist2.getText()); 
      Speed = Integer.parseInt(speed2.getText()); 
      diameter = Double.parseDouble(diam2.getText()); 
     } 
     catch(NumberFormatException i) { 
     } 
     Main.diametras = diameter; 
     Main.distancija = distance; 
     Main.greitis = Speed; 
     Main.vardas = name2.getText(); 
     Main.spalva = color2.getText(); 
     } 

public void actionPerformed(ActionEvent e) { 
    createVariables(); 
    new NewThread().start(); 
      list.display(); 
} 


public void windowClosing(WindowEvent e) { 
    dispose(); 
    System.exit(0); 
} 
public void windowClosed(WindowEvent e) {} 
public void windowActivated(WindowEvent arg0) {} 
public void windowDeactivated(WindowEvent arg0) {} 
public void windowDeiconified(WindowEvent arg0) {} 
public void windowIconified(WindowEvent arg0) {} 
public void windowOpened(WindowEvent arg0) {} 
} 
/** 
* Frame for moon adding window 
*/ 
CheckboxGroup planets = new CheckboxGroup(); 
String which; 
class cmWindow implements ActionListener,WindowListener, ItemListener{ 
JLabel name1 = new JLabel("Name"); 
JLabel color1 = new JLabel("Color"); 
JLabel diam1 = new JLabel("Diameter"); 
JLabel speed1 = new JLabel("Speed"); 
JTextField name2 = new JTextField(); 
JTextField color2 = new JTextField(); 
JTextField diam2 = new JTextField(); 
JTextField speed2 = new JTextField(); 
JLabel info = new JLabel("Which planet's moon it will be?"); 
JLabel corDist1 = new JLabel("Distance from centre of rotation"); 
JLabel corSpeed1 = new JLabel("Speed which moon centres"); 
JTextField corDist2 = new JTextField(); 
JTextField corSpeed2 = new JTextField(); 

int cordistance; 
int corspeed; 

public void createFrame1() { 
    JFrame f1 = new JFrame("Add moon"); 
    f1.addWindowListener(this); 
    f1.setLayout(new GridLayout(8,2,5,5)); 
    JButton mygt = new JButton("Create moon"); 
    mygt.addActionListener(this); 
    for(int i=0;i<Planetarium.length;i++){ 
     add(new Checkbox(Planetarium[i].nam,planets,false)); 
    } 
    corDist2.setText("15");corSpeed2.setText("3"); 
    f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2); 
    f1.add(diam1);f1.add(diam2);f1.add(speed1);f1.add(speed2); 
    f1.add(corDist1);f1.add(corDist2);f1.add(corSpeed1);f1.add(corSpeed2); 
    f1.add(mygt); 
    f1.pack(); 
    f1.setVisible(true); 
} 
int Speed; 
double diameter; 
public void createVariables(){ 
    try { 
      Speed = Integer.parseInt(speed2.getText()); 
      diameter = Double.parseDouble(diam2.getText()); 
      cordistance = Integer.parseInt(corDist2.getText()); 
      corspeed = Integer.parseInt(corSpeed2.getText()); 
     } 
     catch(NumberFormatException i) {} 
     Main.diametras = diameter; 
     Main.greitis = Speed; 
     Main.vardas = name2.getText(); 
     Main.spalva = color2.getText(); 
     Main.centGrt = corspeed; 
     Main.centAts = cordistance; 

} 
public void actionPerformed(ActionEvent e) { 
    createVariables(); 
    new NewThread().start(); 
} 
public void windowClosing(WindowEvent e) { 
    dispose(); 
    System.exit(0); 
} 
public void windowClosed(WindowEvent e) {} 
public void windowActivated(WindowEvent arg0) {} 
public void windowDeactivated(WindowEvent arg0) {} 
public void windowDeiconified(WindowEvent arg0) {} 
public void windowIconified(WindowEvent arg0) {} 
public void windowOpened(WindowEvent arg0) {} 
@Override 
public void itemStateChanged(ItemEvent e) { 
    which = planets.getSelectedCheckbox().getLabel(); 
} 
} 
/** 
* Deleting window 
*/ 
class Delete implements ActionListener,WindowListener{ 
Checkbox[] checkB = new Checkbox[100]; 
public void createFrame2(){ 
JFrame f2 = new JFrame("Death Start"); 
f2.addWindowListener(this); 
f2.setLayout(new GridLayout(6,2,5,5)); 
JButton Del = new JButton("Shoot it!"); 
Del.addActionListener(this);  
JLabel[] planetName = new JLabel[100]; 
    for(int i=0;i<Planetarium.length;i++){ 
     planetName[i].setText(Planetarium[i].nam); 
     checkB[i].setLabel("This"); 
     checkB[i].setState(false); 
     f2.add(planetName[i]);f2.add(checkB[i]); 
    } 
} 

public void actionPerformed(ActionEvent e) { 
    for(int i=0;i<Planetarium.length;i++){ 
     if(checkB[i].getState()){ 
      Planetarium[i] = null; 
     } 
    } 
}  

public void windowClosing(WindowEvent e) { 
    dispose(); 
    System.exit(0); 
} 
public void windowClosed(WindowEvent e) {} 
public void windowActivated(WindowEvent arg0) {} 
public void windowDeactivated(WindowEvent arg0) {} 
public void windowDeiconified(WindowEvent arg0) {} 
public void windowIconified(WindowEvent arg0) {} 
public void windowOpened(WindowEvent arg0) {} 
} 
//////////////////////////////////////////////////////// 
public GUI() { 
createFrame0(); 
} 


public void actionPerformed(ActionEvent e) { 
if ("1".equals(e.getActionCommand())) {this.window1.createFrame1();} 
else if ("2".equals(e.getActionCommand()) & Planetarium != null)  {this.window2.createFrame1();} 
else if ("3".equals(e.getActionCommand()) & Planetarium != null)  {this.window3.createFrame2();} 
} 

public void windowClosing(WindowEvent e) { 
dispose(); 
System.exit(0); 
} 
public void windowClosed(WindowEvent e) {} 
public void windowActivated(WindowEvent arg0) {} 
public void windowDeactivated(WindowEvent arg0) {} 
public void windowDeiconified(WindowEvent arg0) {} 
public void windowIconified(WindowEvent arg0) {} 
public void windowOpened(WindowEvent arg0) {} 
LinkedList list = new LinkedList(); 
class NewThread extends Thread { 
Thread t; 
NewThread() { 
    t = new Thread(this); 
    t.start(); 
} 
public void run() { 
    Moon moon = null; 
    Planet planet = new  Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis); 
    if(Planetarium != null){ 
    for(int i=0;i<Planetarium.length;i++){ 
     if (which == Planetarium[i].nam){ 
      moon = new Moon(Main.vardas,Planetarium[i].dist,Main.diametras,Main.spalva,Main.greitis,Main.centGrt,Main.centAts); 
     } 
    }} 
    int a=0,b=0; 
    int i = 0; 

     if (Main.centAts == 0){ 
      Planetarium[i] = planet;   //i guess  problem is here 
      a++; 
          list.add(planet); 
      for(i=0; i <= i+1 0; i++) { 
       planet.move(); 
       planet.drawOn(system); 
       system.finishedDrawing(); 
            if (i==360){i=0;} 
      } 
     } 
     else{ 
      Moonarium[i] = moon; 
      b++; 
      if(i==Main.greitis){ 
      for(int l = 0; l <= l+1; l++) { 
       moon.move(); 
       moon.drawOn(system); 
       system.finishedDrawing(); 
      }} 
     } 

} 
    } 
} 

編輯:無限循環前添加鏈表(仍然沒有顯示)之後,搬到聲明

回答

1

你似乎並沒有賦予這些陣列東西所以他們應該null,而不是空的。 你使用它們的方式列表可能會更好。

final List<Planet> planetarium = new ArrayList<Planet>(); 

planetarium.add(new Planet(....)); 

Planet p = planetarium.get(i); 

for(Planet p: planetarium){ 
    // something for each planet. 
} 

注意:你必須在使用它之前創建一個數組,並且你知道它的長度是固定的。列表可以有任意長度,但您仍然需要先創建它。

+0

首先,沒有人注意到,我試圖在無限循環後做點什麼。不過,我已經在無限循環前面移動了聲明,並試圖將對象添加到鏈表中,但在嘗試顯示它之後,什麼都沒有發生...... – Medardas 2011-02-24 18:39:19

+0

我仍然期望我建議的工作。我建議你再讀一次我的答案。當您嘗試調試應用程序時,您會看到什麼? – 2011-02-24 18:58:04

+0

是的,我忘了提及,我改變了數組創建條件,初始化爲空。依然沒有。 – Medardas 2011-02-24 19:11:47

1

看循環之前:

Planetarium[i] = planet; 

看起來它會無限循環

for(i=0; i >= 0; i++) 

i總是支持> = 0

+0

是的,它需要在窗格中移動的對象=]但是這並不相關 – Medardas 2011-02-24 18:37:12

+0

@Medardas它是相關的,因爲如果代碼永遠停留在該循環中,它將永遠不會到達它應該向天文館添加內容的下一個部分。 – Alb 2011-02-24 18:38:40

+0

你就在這裏,修好它,還是一無所有 – Medardas 2011-02-24 18:43:45