我正在编写一个程序,该程序使用每个操作系统的.c文件的编译版本。我想显示该程序的输出,该输出在终端/命令提示符下实时运行。
我已经测试了我的程序的简化版本,以证明它在与终端或控制台交互时正常工作。这是代码:
package mcve;
import com.sun.glass.ui.Window;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.*;
public class WindowsVsMacExample {
JDialog dialog;
JTextArea textArea;
JProgressBar bar;
public WindowsVsMacExample() {
new BackgroundTask().execute();
}
private void getLoadingGUI() {
dialog = new JDialog();
JPanel panel = new JPanel();
panel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(5, 5));
textArea = new JTextArea(20, 20);
JScrollPane sp = new JScrollPane();
sp.setBorder(BorderFactory.createTitledBorder("Output:"));
sp.setViewportView(textArea);
bar = new JProgressBar();
bar.setIndeterminate(true);
JPanel barPanel = new JPanel();
barPanel.add(bar);
panel.add(sp, BorderLayout.CENTER);
panel.add(barPanel, BorderLayout.PAGE_END);
dialog.setContentPane(panel);
dialog.setUndecorated(true);
dialog.setAlwaysOnTop(true);
dialog.setLocationByPlatform(true);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.pack();
dialog.setVisible(true);
}
private class BackgroundTask extends SwingWorker<Integer, String> {
private int status;
public BackgroundTask() {
}
@Override
protected Integer doInBackground() {
try {
List<String> command = new ArrayList<String>();//Create command
command.add("ping");
String name = System.getProperty("os.name");
if (name.startsWith("Win")) {
command.add("-n");
} else {
command.add("-c");
}
command.add("10");
command.add("google.com");
ProcessBuilder pb = new ProcessBuilder(command); //Create process
pb.redirectErrorStream(true);
Process p = pb.start();
String s;
BufferedReader stdout = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null && !isCancelled()) {
publish(s);
}
if (!isCancelled()) {
status = p.waitFor();
}
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
p.destroy();
} catch (IOException | InterruptedException ex) {
ex.printStackTrace(System.err);
}
return status;
}
@Override
protected void process(java.util.List<String> messages) {
for (String message : messages) {
textArea.append(message + "\n");
}
}
@Override
protected void done() {
bar.setIndeterminate(false);
dialog.dispose();
System.out.println("Command finished, dialog disposed.");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Ping google");
JPanel panel = new JPanel();
panel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Ping start.");
WindowsVsMacExample example = new WindowsVsMacExample();
example.getLoadingGUI();
System.out.println("Loading dialog displayed");
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
当我从ping切换到为每个操作系统编译的.c程序时,在MAC OS系统中,它通过实时显示输出来正常工作。但是当我在Windows中运行它时,输出对话框没有显示任何内容,因为所有文本都在最后。我可以在netbeans的输出中看到它有效,但作为一个用户你什么也看不见,它做了它应该做的但却没有显示它正在做什么。
如果我在Windows中从命令提示符运行此程序,它可以在执行任务时显示每一行。
那么,如果我在Windows中执行它时程序是否正常工作,那么我编程的程序在MAC中完美运行可能会出现问题?我几乎整天都试图弄清楚这一点,但我可以找到解释。