問(wèn)題描述
當(dāng)我在 Java 程序中輸出流時(shí),我無(wú)法弄清楚如何保留命令提示符的格式.有人有什么建議嗎?
I cannot figure out how to preserve command prompt's formatting when I output the stream inside a Java program. Anyone have any suggestions?
推薦答案
根據(jù)數(shù)據(jù)的派生方式,您可以使用多種可能的解決方案...
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));
}
}
}
如果您的管道內(nèi)容來(lái)自其他來(lái)源(如外部命令),那就太好了.
This is great if your piping content from another source (like an external command).
如果您可以控制內(nèi)容,則可以使用 String.format
,但同樣,除非您使用固定寬度的字體,否則不會(huì)有任何區(qū)別,您仍然可以格式問(wèn)題.
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 表格,然后字體無(wú)關(guān)緊要
Another solution would be to use a html table in a JEditorPane
, then the font doesn't matter
另一種解決方案可能是使用 JTable
,它旨在以表格形式呈現(xiàn)數(shù)據(jù).有關(guān)詳細(xì)信息,請(qǐng)參閱如何使用表
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 中自動(dòng)生成的代碼的一個(gè)錯(cuò)誤.只是讓追查問(wèn)題變得很困難.
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.
我非常懷疑這是一個(gè)錯(cuò)誤,因?yàn)槊刻於加袛?shù)百人(甚至數(shù)千人)使用它......但沒(méi)有 可運(yùn)行的示例,它展示了您無(wú)法確定的問(wèn)題...
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));但是,如果您切換到粗體或斜體或粗斜體,則會(huì)生成該行并正常工作.
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);
}
}
}
這篇關(guān)于如何在 jTextArea(或其他類(lèi)型的控制臺(tái))中保留命令提示符的格式?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!