线程不执行

时间:2011-02-23 10:55:06

标签: java multithreading event-listener

我是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 :(得分:1)

实际上需要启动Thread,只是实例化Thread对象是不够的。

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

答案 1 :(得分:0)

当您使用Thread创建new Thread()时,您必须传递一个Runnable实例,其中包含您要在该主题中执行的实际代码。

所以NewThread的构造函数应该看起来像

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

这将创建一个执行NewThread#run()中代码的线程,我想这就是你想要的。

在执行此操作之前,请务必完成NewThread中字段的任何必要初始化。