0
我正在研究一個簡單的文件閱讀器。它讀取.txt文件,然後格式化輸出並在JTextArea中顯示輸出。出於某種原因,輸出無法正確顯示。我已經給出了我的當前代碼,後面跟着下面的文本文件內容。我該如何解決這個JTextArea格式錯誤?
代碼
public static JTextArea display = new JTextArea();
public static void main(String[] args) {
// GUI
JFrame frame = new JFrame("Haberdasher");
frame.setSize(450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel container = new JPanel();
container.setLayout(null);
frame.setContentPane(container);
JScrollPane scroll = new JScrollPane(display);
scroll.setBounds(10, 10, 415, 150);
container.add(scroll);
frame.toFront();
frame.setVisible(true);
// Logic
String path = "src//employees.txt";
boolean endOfFile = false;
String output = "Name" + "\t\t" + "Weekly Sales" + "\t\t" + "Weekly Pay" + "\n";
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while (!endOfFile) {
String name = br.readLine();
if(name == null) {
endOfFile = true;
} else {
int sale = Integer.parseInt(br.readLine());
if(name.length() >= 16) {
output += name + "\t" + sale + "\t\t" + "300" + "\n";
} else {
output += name + "\t\t" + sale + "\t\t" + "300" + "\n";
}
}
}
br.close();
System.out.println(output);
display.setText(output);
} catch (IOException e) {
System.out.println(e);
}
}
employees.txt內容:http://hastebin.com/ijuyedizil.nginx
預期輸出:http://hastebin.com/epesipatot.nginx
要麼不打印整個名稱或使用其他選項卡 –
您必須根據要打印的字符串的長度來製作打印的標籤數量(\ t)。 –
您的預期產出是多少?這種對齊問題是否是問題? – Coder