gpt4 book ai didi

java - 将控制台输出重定向到 GUI

转载 作者:行者123 更新时间:2023-11-29 03:26:02 25 4
gpt4 key购买 nike

我借用了在 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();
}
}

最佳答案

您需要在新线程中调用 startMeUp,因为您的控制台程序正在阻塞事件分发线程。像这样:

new Thread () {
@Override public void run () {
GreenhouseControls.startMeUp();
}
}.start();

不仅仅是

GreenhouseControls.startMeUp();

关于java - 将控制台输出重定向到 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20938189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com