无法在线程中填充数组

时间:2011-02-24 17:23:34

标签: java multithreading arrays user-interface event-listener

这个gui应该在框架面板上绘制称为“系统”的运动图像。但首先我需要制作这些物品。在这里,我正在尝试将它们添加到数组中,并在其他类中使用该数组。但阵列一直是空的! 我想问题在于声明(代码的底部)。抛出没有异常,因为我只允许其他类在此数组(Planetarium)不为空时执行(它应该像那样工作,因为其他移动取决于创建的行星(这些对象))。但如果它是空的,那意味着什么都没有宣布...... 如果我想在事件监听器中执行的线程中填充数组,我该怎么办?

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();
            }}
        } 

}
  }
}

编辑:添加链表(显示后仍然没有)并在无限循环之前移动声明

2 个答案:

答案 0 :(得分: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.
}

注意:您必须在使用它之前创建一个数组,并且您知道它的长度是固定的。列表可以有任何长度,但您仍需要先创建它。

答案 1 :(得分:1)

看看之前的循环:

 Planetarium[i] = planet; 

看起来它会无限循环

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

i总是&gt; = 0。