• <legend id='cGgdV'><style id='cGgdV'><dir id='cGgdV'><q id='cGgdV'></q></dir></style></legend>

    1. <small id='cGgdV'></small><noframes id='cGgdV'>

        <bdo id='cGgdV'></bdo><ul id='cGgdV'></ul>

        <tfoot id='cGgdV'></tfoot>

      1. <i id='cGgdV'><tr id='cGgdV'><dt id='cGgdV'><q id='cGgdV'><span id='cGgdV'><b id='cGgdV'><form id='cGgdV'><ins id='cGgdV'></ins><ul id='cGgdV'></ul><sub id='cGgdV'></sub></form><legend id='cGgdV'></legend><bdo id='cGgdV'><pre id='cGgdV'><center id='cGgdV'></center></pre></bdo></b><th id='cGgdV'></th></span></q></dt></tr></i><div id='cGgdV'><tfoot id='cGgdV'></tfoot><dl id='cGgdV'><fieldset id='cGgdV'></fieldset></dl></div>

        如何在 jTextArea(或其他类型的控制台)中保留命令

        时间:2023-09-29
        <legend id='I4Nxa'><style id='I4Nxa'><dir id='I4Nxa'><q id='I4Nxa'></q></dir></style></legend>

            <bdo id='I4Nxa'></bdo><ul id='I4Nxa'></ul>

                  <small id='I4Nxa'></small><noframes id='I4Nxa'>

                  <i id='I4Nxa'><tr id='I4Nxa'><dt id='I4Nxa'><q id='I4Nxa'><span id='I4Nxa'><b id='I4Nxa'><form id='I4Nxa'><ins id='I4Nxa'></ins><ul id='I4Nxa'></ul><sub id='I4Nxa'></sub></form><legend id='I4Nxa'></legend><bdo id='I4Nxa'><pre id='I4Nxa'><center id='I4Nxa'></center></pre></bdo></b><th id='I4Nxa'></th></span></q></dt></tr></i><div id='I4Nxa'><tfoot id='I4Nxa'></tfoot><dl id='I4Nxa'><fieldset id='I4Nxa'></fieldset></dl></div>

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

                  问题描述

                  当我在 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模板网!

                  上一篇:Java 格式小时和分钟 下一篇:如何用点格式化双精度?

                  相关文章

                  最新文章

                    • <bdo id='RJKAh'></bdo><ul id='RJKAh'></ul>

                    <small id='RJKAh'></small><noframes id='RJKAh'>

                  1. <legend id='RJKAh'><style id='RJKAh'><dir id='RJKAh'><q id='RJKAh'></q></dir></style></legend>
                    <tfoot id='RJKAh'></tfoot>
                  2. <i id='RJKAh'><tr id='RJKAh'><dt id='RJKAh'><q id='RJKAh'><span id='RJKAh'><b id='RJKAh'><form id='RJKAh'><ins id='RJKAh'></ins><ul id='RJKAh'></ul><sub id='RJKAh'></sub></form><legend id='RJKAh'></legend><bdo id='RJKAh'><pre id='RJKAh'><center id='RJKAh'></center></pre></bdo></b><th id='RJKAh'></th></span></q></dt></tr></i><div id='RJKAh'><tfoot id='RJKAh'></tfoot><dl id='RJKAh'><fieldset id='RJKAh'></fieldset></dl></div>