2011-02-24 48 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