I made a Java program using Swing libraries. Now I would like to redirect my console outputs into a JFrame or JPanel.
我使用Swing库创建了一个Java程序。现在我想将我的控制台输出重定向到JFrame或JPanel。
2 个解决方案
#1
16
You need to make an OutputStream that re-directs output to the text area and that implements all the necessary methods of the OutputStream interface, and then in your main program, redirect your Standard output into this stream. I've used something like this for one of my programs:
您需要创建一个OutputStream,它将输出重定向到文本区域并实现OutputStream接口的所有必要方法,然后在主程序中将标准输出重定向到此流中。我在我的一个程序中使用过这样的东西:
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TextAreaOutputStream extends OutputStream {
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public TextAreaOutputStream(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title + "> ");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title + "> ");
return;
}
sb.append((char) b);
}
}
And you can demonstrate it with this:
你可以用这个证明它:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;
@SuppressWarnings("serial")
public class TextAreaOutputStreamTest extends JPanel {
private JTextArea textArea = new JTextArea(15, 30);
private TextAreaOutputStream taOutputStream = new TextAreaOutputStream(
textArea, "Test");
public TextAreaOutputStreamTest() {
setLayout(new BorderLayout());
add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
System.setOut(new PrintStream(taOutputStream));
int timerDelay = 1000;
new Timer(timerDelay , new ActionListener() {
int count = 0;
@Override
public void actionPerformed(ActionEvent arg0) {
// though this outputs via System.out.println, it actually displays
// in the JTextArea:
System.out.println("Count is now: " + count + " seconds");
count++;
}
}).start();
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextAreaOutputStreamTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#2
6
Once you have your JFrame
or JPanel
, add a text field to it.
获得JFrame或JPanel后,请为其添加文本字段。
JTextArea
is a good choice, because it's multiple lines. Once it is added, you can .append('text');
to it instead of writing the System.out.print();
JTextArea是一个不错的选择,因为它有多行。一旦添加,你就可以.append('text');而不是编写System.out.print();
JFrame jFrame = new JFrame();
JTextArea jTextArea = new JTextArea();
jTextArea.append( "Hello World." );
jFrame.add( jTextArea );
#1
16
You need to make an OutputStream that re-directs output to the text area and that implements all the necessary methods of the OutputStream interface, and then in your main program, redirect your Standard output into this stream. I've used something like this for one of my programs:
您需要创建一个OutputStream,它将输出重定向到文本区域并实现OutputStream接口的所有必要方法,然后在主程序中将标准输出重定向到此流中。我在我的一个程序中使用过这样的东西:
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TextAreaOutputStream extends OutputStream {
private final JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public TextAreaOutputStream(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title + "> ");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title + "> ");
return;
}
sb.append((char) b);
}
}
And you can demonstrate it with this:
你可以用这个证明它:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;
@SuppressWarnings("serial")
public class TextAreaOutputStreamTest extends JPanel {
private JTextArea textArea = new JTextArea(15, 30);
private TextAreaOutputStream taOutputStream = new TextAreaOutputStream(
textArea, "Test");
public TextAreaOutputStreamTest() {
setLayout(new BorderLayout());
add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
System.setOut(new PrintStream(taOutputStream));
int timerDelay = 1000;
new Timer(timerDelay , new ActionListener() {
int count = 0;
@Override
public void actionPerformed(ActionEvent arg0) {
// though this outputs via System.out.println, it actually displays
// in the JTextArea:
System.out.println("Count is now: " + count + " seconds");
count++;
}
}).start();
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextAreaOutputStreamTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#2
6
Once you have your JFrame
or JPanel
, add a text field to it.
获得JFrame或JPanel后,请为其添加文本字段。
JTextArea
is a good choice, because it's multiple lines. Once it is added, you can .append('text');
to it instead of writing the System.out.print();
JTextArea是一个不错的选择,因为它有多行。一旦添加,你就可以.append('text');而不是编写System.out.print();
JFrame jFrame = new JFrame();
JTextArea jTextArea = new JTextArea();
jTextArea.append( "Hello World." );
jFrame.add( jTextArea );