【发布时间】:2014-01-05 19:37:17
【问题描述】:
我借用了我在 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();
}
}
【问题讨论】:
-
天哪,这是一个多么可怕的想法,重定向标准错误。当然,这将使调试此问题变得困难。注释掉
System.setErr(new PrintStream(out, true));行,然后重试。你在控制台/终端窗口上看到什么了吗? -
尝试了你的建议,我仍然遇到同样的问题。 GUI 没有输出
-
在控制台或终端窗口上呢?我不是在谈论 GUI。您确实打开了控制台或终端窗口,不是吗?
-
当我在 IDE 中输出到控制台时,程序运行良好。只有当我从 GUI 内部运行它时才会出现问题。
-
在你注释掉我让你注释掉的那一行之后,任何错误(如异常)都应该出现在控制台上。如果您没有可见的控制台,您将无法看到错误。您的 GUI 运行情况如何?
标签: java swing user-interface