我借用了我在stackoverflow上找到的设计将控制台输出重定向到GUI。它工作正常,直到我开始阅读我的程序中的文本文件。现在,当我使用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));
}
}
}
我敢肯定我的问题,从代码中的“文件名”路径读取开始后面,因为我的时候我发表意见没有这个问题'filename'变量声明出来。我认为将控制台输出重定向到我的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();
}
}
我的善良什么可怕的想法,重定向标准错误。当然这会使调试这个问题变得困难。注释掉'System.setErr'行(新的PrintStream(out,true));'并重试。你有没有在控制台/终端窗口看到任何东西? –
试过你的建议,我仍然有同样的问题。没有输出到GUI – LooMeenin
在*控制台*或*终端窗口*上怎么样?我不是在谈论GUI。你有一个控制台或终端窗口打开你不? –