如何在jTextArea(或其他类型的控制台)中保留命令提示符的格式?
当我在Java程序中输出流时,我无法弄清楚如何保留命令提示符的格式。 有人有什么建议吗?
根据您的数据来源,您可以使用多种可能的解决方案……
最基本的是确保您使用固定宽度的字体…
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 + "\n"); } add(new JScrollPane(ta)); } } }
如果您的管道内容来自其他来源(如外部命令),这非常有用。
如果您可以控制内容,则可以使用String.format
,但是,除非您使用的是固定宽度字体,否则它将没有任何区别,您仍然会遇到格式问题。
另一个解决方案是在JEditorPane
使用html表,然后字体无关紧要
另一种解决方案可能是使用JTable
,它旨在以表格forms呈现数据。 有关更多详细信息,请参见如何使用表
更新
这是Netbeans 8.0中自动生成的代码的一个错误我很确定。 它只是让追踪问题变得困难。
我非常怀疑这是一个错误,因为每天有成百上千的人使用它……但是如果没有一个可以certificate你的问题的可运行的例子 ,那么肯定不可能知道……
ta.setFont(new Font(“Monospaced”,Font.PLAIN,13)); 但是,如果您切换为粗体或斜体或粗体斜体,则生成线并正常工作。
我不敢苟同…
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 + "\n"); } ta.setCaretPosition(0); add(new JScrollPane(ta)); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } } }