问题描述
我正在尝试为程序创建GUI面板,我想要通常打印到命令提示符的所有内容都打印到TextArea对象。我的GUI面板大部分都是格式化的,我无法将文本打印到TextArea,这里是我的文件:
I'm trying to create a GUI Panel for a program and I'd like everything, that would normally print to my command prompt, to print to a TextArea object. I have the GUI panel formatted for the most part, I can't get the text to print to the TextArea though, here's my file:
package guipanel;
import javax.swing.*;
import java.awt.*;
import java.io.*;
/**
*
* @author Dan
*/
public class GUIPanel extends JFrame {
public GUIPanel() {
initComponents();
}
private void setOutputStream(boolean catchErrors) {
System.setOut(aPrintStream);
setVisible(true);
requestFocus();
if (catchErrors) {
System.setErr(aPrintStream);
}
}
private void addTabs(JTabbedPane jTabbedPane1) {
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
jTabbedPane1.add("Main", textArea1);
jTabbedPane1.add("Commands", jPanel);
jTabbedPane1.add("Rules", jPanel1);
jTabbedPane1.add("Links", jPanel2);
jTabbedPane1.add("Information", jPanel3);
jTabbedPane1.add("Shutdown", jPanel4);
setOutputStream(true);
}
@SuppressWarnings("unchecked")
private void initComponents() {
textArea1 = new java.awt.TextArea();
jTabbedPane1 = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
textArea1.setPreferredSize(new java.awt.Dimension(432, 343));
textArea1.getAccessibleContext().setAccessibleParent(jTabbedPane1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Evolution-X 639");
setBounds(new java.awt.Rectangle(0, 0, 400, 450));
setResizable(false);
getContentPane().setLayout(new java.awt.FlowLayout());
addTabs(jTabbedPane1);
getContentPane().add(jTabbedPane1);
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUIPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUIPanel().setVisible(true);
}
});
}
private JMenu jMenu1;
private JMenu jMenu2;
private JMenuBar jMenuBar1;
private JTabbedPane jTabbedPane1;
private TextArea textArea1;
private JPanel jPanel = new JPanel();
private PrintStream aPrintStream =
new PrintStream(
new FilterOutputStream(
new ByteArrayOutputStream()));
}
推荐答案
您需要重定向打印流到您可以控制的输出流...
You need to redirect the print stream to an output stream you can control...
这是我为正在工作的应用程序开发的概念示例。当我们在用户站点运行时,我们使用它来调出输出控制台,这样我们可以看到发送到标准输出的内容......直到我们修复了我们的日志记录;)
This is an example of concept I developed for an application I'm working on for work. We use this to bring up the output console when it's running at user sites so we can see what's being sent to standard out...until we fixed our logging that is ;)
基本上它在打印流和控制台之间放置一个自定义 OutputStream
来捕获输出,但仍允许将内容打印到控制台。如果您从命令行或IDE运行该程序,这将非常有用。你可以根据需要设置一个开关来停止它......
Basically it puts a custom OutputStream
in between the print stream and the console to capture the output, but still allows the content to printed to the console. This is helpful if you're running the program from the command line or IDE. You could put a switch to stop this if you wanted...
public class TestRedirect {
public static void main(String[] args) {
new TestRedirect();
}
public TestRedirect() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
CapturePane capturePane = new CapturePane();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(capturePane);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
PrintStream ps = System.out;
System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, ps)));
System.out.println("Hello, this is a test");
System.out.println("Wave if you can see me");
}
});
}
public class CapturePane extends JPanel implements Consumer {
private JTextArea output;
public CapturePane() {
setLayout(new BorderLayout());
output = new JTextArea();
add(new JScrollPane(output));
}
@Override
public void appendText(final String text) {
if (EventQueue.isDispatchThread()) {
output.append(text);
output.setCaretPosition(output.getText().length());
} else {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
appendText(text);
}
});
}
}
}
public interface Consumer {
public void appendText(String text);
}
public class StreamCapturer extends OutputStream {
private StringBuilder buffer;
private String prefix;
private Consumer consumer;
private PrintStream old;
public StreamCapturer(String prefix, Consumer consumer, PrintStream old) {
this.prefix = prefix;
buffer = new StringBuilder(128);
buffer.append("[").append(prefix).append("] ");
this.old = old;
this.consumer = consumer;
}
@Override
public void write(int b) throws IOException {
char c = (char) b;
String value = Character.toString(c);
buffer.append(value);
if (value.equals("\n")) {
consumer.appendText(buffer.toString());
buffer.delete(0, buffer.length());
buffer.append("[").append(prefix).append("] ");
}
old.print(c);
}
}
}
更新了工作示例。在Windows 7,Java 6和Mac OS Lion Java 7上进行测试
Updated with working example. Test on Windows 7, Java 6 and Mac OS Lion Java 7
这篇关于如何将输出流设置为TextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!