how to visualize console java in JFrame/JPanel(如何在 JFrame/JPanel 中可视化控制台 java)
问题描述
我使用 Swing 库制作了一个 Java 程序.现在我想将我的控制台输出重定向到 JFrame 或 JPanel.
I made a Java program using Swing libraries. Now I would like to redirect my console outputs into a JFrame or JPanel.
推荐答案
你需要制作一个OutputStream,将输出重定向到文本区域并实现OutputStream接口的所有必要方法,然后在你的主程序中,将您的标准输出重定向到此流中.我在我的一个程序中使用了类似的东西:
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:
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 == '
')
return;
if (b == '
') {
final String text = sb.toString() + "
";
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();
}
});
}
}
这篇关于如何在 JFrame/JPanel 中可视化控制台 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JFrame/JPanel 中可视化控制台 java
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01