我借用了stackoverflow上的设计,将控制台输出重定向到GUI。它工作正常,直到我开始从我的程序中的文本文件中读取。现在,当我使用GUI运行程序时,不会显示任何输出,GUI会冻结,然后最终自行关闭。这是我的GUI代码的精简版:
public class GreenhouseControlsGUI {
public static class MyGui extends JFrame implements ActionListener {
// Start button
JButton Start = new JButton("Start");
/..................................../
/**
* The constructor.
*/
public MyGui(){
super("Greenhouse Controls");
/............................../
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(Start);
/............................../
getContentPane().add(holdAll, BorderLayout.CENTER);
Start.addActionListener(this);
/............................../
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == Start)
GreenhouseControls.startMeUp(); // Start program...
/............................../
}
public static void main(String[] args){
MyGui myApplication = new MyGui();
// redirect output to GUI
myApplication.redirectSystemStreams();
// Specify where will it appear on the screen:
myApplication.setLocation(10, 10);
myApplication.setSize(500, 300);
// Show it!
myApplication.setVisible(true);
}
private void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myText.append(text);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
}
}
我非常确定我的问题始于从'文件名'以下代码中的路径,因为当我评论'文件名时,我没有遇到此问题。变量声明。我认为将控制台输出重定向到我的GUI的方法只是重定向输出....当我从文件中读取时为什么会搞砸一切?我是编程新手,我可能忽略了一些明显的东西,但我无法弄清楚。
这是在GUI类中调用的静态startMeUp()方法:
public static void startMeUp() {
try {
String option = "-f"; // (-f) to start program or (-d) to restore program
filename = "src/greenhouse/examples3.txt"; // read file from here
dumpFile = "dump.out"; // restore program from here
// if option is invalid invoke corresponding print statement
if ( !(option.equals("-f")) && !(option.equals("-d")) ) {
System.out.println("Invalid option");
printUsage();
}
GreenhouseControls gc = new GreenhouseControls(); // Create GreenhouseControls object
Restart restart = new Restart(0,filename, gc); // Create Restart object
Restore restore = new Restore(0,dumpFile); // Create Restore object
// if the option value is -f ...
if (option.equals("-f")) {
gc.addEvent(restart); // add new Restart object to addEvent()
}
gc.run();
// if the option value is -d ...
if (option.equals("-d")) {
gc.addEvent(restore); // add new Restore object to addEvent()
}
gc.run();
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid number of parameters");
printUsage();
}
}
答案 0 :(得分:4)
您需要在新线程中调用startMeUp
,因为您的控制台程序阻止了事件派发线程。像这样:
new Thread () {
@Override public void run () {
GreenhouseControls.startMeUp();
}
}.start();
而不仅仅是
GreenhouseControls.startMeUp();