sho*_*ota 2 java format swing command-prompt
当我在 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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的管道内容来自其他来源(例如外部命令),这非常有用。
如果您可以控制内容,则可以使用String.format,但同样,除非您使用固定宽度字体,否则不会有任何区别,您仍然会遇到格式问题。
另一种解决方案是在 a 中使用 html 表JEditorPane,然后字体并不重要
另一种解决方案可能是使用JTable旨在以表格形式呈现数据的 。有关更多详细信息,请参阅如何使用表格
更新
我很确定这是 Netbeans 8.0 中自动生成的代码的一个错误。这使得追查问题变得更加困难。
我非常怀疑这是一个错误,因为每天都有数百甚至数千人使用它......但是如果没有可运行的示例来证明你的问题,就不可能确定......
ta.setFont(new Font("等宽", 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
533 次 |
| 最近记录: |