java - 如何在 jTextArea (或其他类型的控制台)中保留命令提示符的格式?

标签 java swing format command-prompt

当我在 Java 程序中输出流时,我无法弄清楚如何保留命令提示符的格式。大家有什么建议吗?

Output

最佳答案

根据数据的导出方式,您可以使用多种可能的解决方案...

最基本的是确保您使用固定宽度的字体...

enter image description here

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,它旨在以表格形式呈现数据。请参阅How to Use Tables了解更多详情

已更新

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.

我非常怀疑这是一个错误,因为每天有数百甚至数千人使用它......但没有 runnable example这表明你的问题是不可能确定的......

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.

我不敢苟同……

Fixed Width Font

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);
        }

    }
}

关于java - 如何在 jTextArea (或其他类型的控制台)中保留命令提示符的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922360/

相关文章:

java - 在 JXTree 中搜索

java - 如果子查询的结果集列值为 NULL,则显示某个值

java - 当移动到辅助显示器时,我的 JApplet 消失了

c++ - 测试用例之间的不同打印格式

javascript - 格式化 HTML 输入文本产品 key

java - 如何在int中添加空格?

java - 使用自定义类加载器中的 javax.tools.ToolProvider?

java - JSP 自定义标签 : Is it possible to have a more than start/close tags?

java - 我的组件在 JPanel 上不可见

java - 如何使用 getGraphics() 覆盖 PaintComponent() 外部绘制到 JPanel 的 BufferedImage