我正在制作一个线程应用程序,主类也是可运行的类。出于某种原因,我的主题不会开始。任何想法为什么? updateThread是罪魁祸首......
这是代码:
package avtech.software.bitprice.display;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import avtech.software.bitprice.dialogs.*;
import avtech.software.bitprice.listeners.*;
@SuppressWarnings("serial")
public class Display extends JFrame implements Runnable {
/**
* variables
*/
private static final int WIDTH = 200, HEIGHT = 200;
private static double currentPrice = 0.0;
private static double pauseLength;
private static boolean running = false;
private static String greaterLess = "";
/**
* objects
*/
private static Display d;
private static Thread updateThread;
private static JLabel currentPriceLabel = new JLabel("Current Price: $" + currentPrice);
private static JTextField pullDelayArea = new JTextField("Price Pull Delay In Minutes...");
private static JTextField priceValueArea = new JTextField("Price You Are Waiting For...");
private static JButton update = new JButton("UPDATE DATA");
public Display() {
running = true; // program is now running
updateThread = new Thread(d); // create the local thread
/**
* create the frame
*/
setLayout(null);
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BitPrice");
/**
* set bounds of the components
*/
currentPriceLabel.setBounds(((WIDTH / 2) - (currentPriceLabel.toString().length() / 5)), 5, WIDTH, 50);
pullDelayArea.setBounds(10, 40, WIDTH - 25, 25);
priceValueArea.setBounds(10, 70, WIDTH - 25, 25);
update.setBounds(10, 100, WIDTH - 25, 25);
/**
* set up the listeners to the components
*/
pullDelayArea.addMouseListener(new PullDelayAreaListener(pullDelayArea));
pullDelayArea.addActionListener(new PullDelayAreaListener(pullDelayArea));
priceValueArea.addMouseListener(new PriceValueAreaListener(priceValueArea));
priceValueArea.addActionListener(new PriceValueAreaListener(priceValueArea));
update.addActionListener(new UpdateButtonListener());
/**
* add everything
*/
add(currentPriceLabel);
add(pullDelayArea);
add(priceValueArea);
add(update);
setLocationRelativeTo(null);
setVisible(true);
requestFocus();
/**
* start the update process
*/
System.out.println("hi");
updateThread.start();
}
/**
* everything that happens when the thread is updated, including updating
* the price label, and all that good stuff :)
*/
public static void update() {
/**
* make sure that there is data in the boxes taht is valid
*/
if (pullDelayArea.getText().equals("Price Pull Delay In Minutes...") || pullDelayArea.getText().equals("")) {
new MessageDialog(d, "Please enter a valid number into Price Pull Delay.");
}
if (priceValueArea.getText().equals("Price You Are Waiting For...") || priceValueArea.getText().equals("")) {
new MessageDialog(d, "Please enter a valid number into Price Value.");
}
// set the new price double from the website
// update the label
currentPriceLabel.setText("Current Price: $" + currentPrice);
currentPriceLabel.setBounds(((WIDTH / 2) - (currentPriceLabel.toString().length() / 5)), 5, WIDTH, 50);
currentPriceLabel.repaint();
/**
* check to see if the current value is what the client is waiting for
*/
String priceValueString = priceValueArea.getText();
double priceValue = Double.parseDouble(priceValueString);
}
/**
* this thread is checking the price of the Bitcoin, and will send out the
* notification if it reaches a certain price
*/
public void run() {
System.out.println("started");
/**
* initial pause, letting the client get all set up
*/
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
new ErrorDialog(this, "Error delaying the pull request. Please restart client.");
}
System.out.println("started");
while (running) {
update();
/**
* add a pause to not just destroy internet speeds, pause settable
* through the GUI
*/
try {
Thread.sleep((long) (pauseLength * 1000000)); // 1 million cause
// its in
// milliseconds
} catch (InterruptedException e) {
new ErrorDialog(this, "Error delaying the pull request. Please restart client.");
}
}
}
public static void main(String[] args) {
d = new Display();
}
}
非常感谢任何帮助。
PS:我的互联网即将关闭,因此回复可能会延迟到明天。
答案 0 :(得分:1)
简短版本是在构造函数退出之前不会设置对d
的引用,因此在Display
中,您将创建一个新的线程对象并将静态引用传递给{{1那时仍然是null。我会从构造函数中删除d
行和new Thread(d)
行,只需从main方法中调用.start()
。