2011-02-23 32 views
0

私はJavaで新しいです、だから私はそれは簡単な問題だと思います。私のイベントリスナーでは、私は変数を宣言するメソッドがあり、私は同じイベントで使用するスレッドでこれらの変数を使用しようとしています。それは動作しませんが、ここにコードです:スレッドは実行されません

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(); 
SolarSystem system = new SolarSystem(300,300); 
Planet Planetarium; 
/** 
* 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("Delete 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(); 
     Planetarium = new    Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis); 
    } 

public void actionPerformed(ActionEvent e) { 
    createVariables(); 
    new NewThread().run(); 
} 


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 
*/ 
class cmWindow 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"); 
JLabel corDist1 = new JLabel("Distance from centre of rotation"); 
JLabel corSpeed1 = new JLabel("Speed which moon centres"); 
JTextField name2 = new JTextField(); 
JTextField color2 = new JTextField(); 
JTextField diam2 = new JTextField(); 
JTextField dist2 = new JTextField(); 
JTextField speed2 = new JTextField(); 
JTextField corDist2 = new JTextField(); 
JTextField corSpeed2 = new JTextField(); 

double distance; 
int Speed; 
double diameter; 
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); 
     name2.setText("Belekoks");color2.setText("BLUE");diam2.setText("15");dist2.setText("60");sp eed2.setText("2");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(dist1);f1.add(dist2);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); 
} 
public void createVariables(){ 
    try { 
      distance = Double.parseDouble(dist2.getText()); 
      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.distancija = distance; 
     Main.greitis = Speed; 
     Main.vardas = name2.getText(); 
     Main.spalva = color2.getText(); 
*/  Main.centGrt = corspeed; 
     Main.centAts = cordistance; 
} 
public void actionPerformed(ActionEvent e) { 
    createVariables(); 
} 
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) {} 
} 
/** 
* Deleting window 
*/ 
class DelWindow implements ActionListener,WindowListener{ 



public void actionPerformed(ActionEvent e) { 

} 
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())) {this.window2.createFrame1();} 
// else if ("3".equals(e.getActionCommand())) {this.window2.createFrame1();} 
} 

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) {} 

//Create a new thread. 
class NewThread implements Runnable { 
Thread t; 
NewThread() { 
    // Create a new, second thread 
    t = new Thread(this); 
    t.start(); // Start the thread 
} 
// This is the entry point for the second thread. 
public void run() { 
    Planet planet = new Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis); 
    try { 
     for(int i = 0; i >= 0; i++) { 
      planet.move(); 
      planet.drawOn(system); 
      system.finishedDrawing(); 
     } 
    } catch (Exception e) {} 
} 
} 

} 

任意のアイデア?

+2

あなたはそれを小さなテストケースに沸かせることができますか?それはウェイドを通すための多くのコードです。 –

+0

何がうまくいかないのですか? – AlexR

+0

今度は無限ループになり、ボタンとフレームを非アクティブにします – Medardas

答えて

1

スレッドを実際に起動する必要があります。ただThreadオブジェクトをインスタンス化するだけでは不十分です。

public void actionPerformed(ActionEvent e) { 
    createVariables(); 
    new NewThread().start(); 
} 
+0

大変ありがとうございます、あなたはほぼ間違いありませんでしたが、スレッドを拡張する必要があることを忘れてしまいました。 – Medardas

0

あなたはnew Thread()Threadを作成すると、あなたがスレッドで実行する実際のコードが含まれているRunnableのインスタンスを渡す必要があります。

だから、NewThreadのあなたのコンストラクタは、私が何をしたいと思いNewThread#run()内のコードを、実行するスレッドを作成します

NewThread() { 
    // Create a new, second thread 
    t = new Thread(this); 
    t.start(); // Start the thread 
} 

次のようになります。

NewThreadのフィールドの必要な初期化を完了してから、これを実行してください。

関連する問題