标签:java format command-prompt swing
当我在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,它被设计为以表格形式呈现数据.有关详细信息,请参见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.
我不敢苟同…
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,format,command-prompt,swing 来源: https://codeday.me/bug/20190929/1833031.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。