如何在jTextArea(或其他类型的控制台)内保留命令提示符的格式? [英] How to preserve command prompt's formatting inside a jTextArea (or some other type of console)?

查看:151
本文介绍了如何在jTextArea(或其他类型的控制台)内保留命令提示符的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



解决方案

有几种可能的解决方案可供您使用,具体取决于您的数据如何派生...



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


$ p $ 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();

$ b 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 {
$ b $ public TestPane(){
String [] lines = {
Idx Met MTU州名,
--- --------- ---------- ------------ ------- -------------------,
1 50 4294967295已连接Loopback Psudo-Interface 1,
11 10 1500已连接本地连接 ,
11 5 1500断开本地连接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));




code


$ b <如果你的管道内容来自其他来源(如外部命令),这是非常好的。



如果你有内容的控制权,你可以使用 String.format ,但是,除非您使用的是固定宽度的字体,否则不会有任何区别,您仍然有格式问题。



另一个解决方案是在 JEditorPane 中使用html表格,那么字体无关紧要

另一个解决方案可能是使用一个 JTable ,它被设计成以表格形式呈现数据。有关详细信息,请参阅如何使用表格


$ b $ p 更新
$ b


这是自动生成的代码Netbeans 8.0我很确定。这个问题很难找到。


我很怀疑这是一个错误,因为这已经被数百如果不是每天有成千上万的人......但没有一个可运行的例子,这证明了你的问题是不可能的


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

$ b $ 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 {
$ b $ public TestPane(){$ b $ setLayout(new GridLayout(4,-1));
String [] lines = {
Idx Met MTU State Name,
--- --------- ---------- - ----------- --------------------------,
1 50 4294967295连接Loopback Psudo - 接口1,
11 10 1500连接本地连接,
11 5 1500断开本地连接3,};

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

}
}


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 + "\n");
            }
            add(new JScrollPane(ta));
        }

    }

}

This is great if your piping content from another source (like an external command).

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.

Another solution would be to use a html table in a JEditorPane, then the font doesn't matter

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

Updated

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)); BUT if you switch to bold or italics or bold italics then the line is generated and works correctly.

I beg to differ...

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

    }
}

这篇关于如何在jTextArea(或其他类型的控制台)内保留命令提示符的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆