当我在 Java 程序中输出流时,我无法弄清楚如何保留命令提示符的格式.有人有什么建议吗?
I cannot figure out how to preserve command prompt's formatting when I output the stream inside a Java program. Anyone have any suggestions?
根据数据的派生方式,您可以使用多种可能的解决方案...
There are a number of possible solutions available to you depending on how your data is derived...
最基本的方法是确保您使用的是固定宽度的字体...
The most basic would be to ensure you are using a fixed width font...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class OutputTest {
public static void main(String[] args) {
new OutputTest();
}
public OutputTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
String[] lines = {
"Idx Met MTU State Name ",
"--- --------- ---------- ------------ --------------------------",
" 1 50 4294967295 connected Loopback Psudo-Interface 1",
" 11 10 1500 connected Local Area Connection ",
" 11 5 1500 disconnected Local Area Connection 3 ",
};
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 40);
ta.setFont(new Font("Monospaced", Font.PLAIN, 13));
for (String text : lines) {
ta.append(text + "
");
}
add(new JScrollPane(ta));
}
}
}
如果您的管道内容来自其他来源(如外部命令),那就太好了.
This is great if your piping content from another source (like an external command).
如果您可以控制内容,则可以使用 String.format
,但同样,除非您使用固定宽度的字体,否则不会有任何区别,您仍然可以格式问题.
If you have control of the content, you could use String.format
, but again, unless you're using a fixed width font, it won't make any difference, you will still have formatting issues.
另一种解决方案是在 JEditorPane
中使用 html 表格,然后字体无关紧要
Another solution would be to use a html table in a JEditorPane
, then the font doesn't matter
另一种解决方案可能是使用 JTable
,它旨在以表格形式呈现数据.有关详细信息,请参阅如何使用表
Another solution might be to use a JTable
which is designed to present data in a tabular form. See How to Use Tables for more details
更新
我很确定这是 Netbeans 8.0 中自动生成的代码的一个错误.只是让追查问题变得很困难.
It's a bug with the auto generated code in Netbeans 8.0 I'm pretty sure. It just made it tough to track down the issue.
我非常怀疑这是一个错误,因为每天都有数百人(甚至数千人)使用它......但没有 可运行的示例,它展示了您无法确定的问题...
I doubt very highly that it's a bug, as this is been used by hundreds if not thousands of people every day...but without a runnable example which demonstrates your problem it's impossible to know for sure...
ta.setFont(new Font("Monospaced", Font.PLAIN, 13));但是,如果您切换到粗体或斜体或粗斜体,则会生成该行并正常工作.
ta.setFont(new Font("Monospaced", Font.PLAIN, 13)); BUT if you switch to bold or italics or bold italics then the line is generated and works correctly.
我不同意...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTable {
public static void main(String[] args) {
new TestTable();
}
public TestTable() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(4, -1));
String[] lines = {
"Idx Met MTU State Name ",
"--- --------- ---------- ------------ --------------------------",
" 1 50 4294967295 connected Loopback Psudo-Interface 1",
" 11 10 1500 connected Local Area Connection ",
" 11 5 1500 disconnected Local Area Connection 3 ",};
Font baseFont = new Font("Monospaced", Font.PLAIN, 13);
addTextArea(baseFont, lines);
addTextArea(baseFont.deriveFont(Font.ITALIC), lines);
addTextArea(baseFont.deriveFont(Font.BOLD), lines);
addTextArea(baseFont.deriveFont(Font.BOLD | Font.ITALIC), lines);
}
protected void addTextArea(Font font, String... lines) {
JTextArea ta = new JTextArea(20, 40);
ta.setFont(font);
for (String text : lines) {
ta.append(text + "
");
}
ta.setCaretPosition(0);
add(new JScrollPane(ta));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
这篇关于如何在 jTextArea(或其他类型的控制台)中保留命令提示符的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!