String.Format由于某种原因可怕地错位

时间:2012-09-17 04:17:35

标签: java user-interface jtextarea string.format

我习惯使用printf,但是我的研究让我相信我可以使用String.Format在JTextArea中设置表格并且它本质上是相同的。这是一个节奏游戏应用程序。我的代码是:

private final String HEADER = "%-10s%-15s%-90s%-9s%-6s%-9s%-7s%-6s%-9s";
...
ranks.setText("");
ranks.append(String.format(HEADER + "\n", "Rank", "Difficulty", "Song Name",     "Perfects", "Goods", "Averages", "Misses", "Boos", "MaxCombo"));
ranks.append(analyze.toString());
...
return String.format("%-10d%-15d%-90s%-9d%-6d%-9d%-7d%-6d%-9d\n ", rank, difficulty,   songName, perfects, goods, averages, misses, boos, maxCombo);

对于数组中的每个组件,analyze.toString返回显示的字符串。我的格式字符串是相同的sans一切都是标题中的字符串,大多数都是表中的整数所以我不知道为什么我的表看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:4)

如评论中所述,您应使用fixed-width font (or monospaced font)进行此类排列。

JTextArea area = new JTextArea();
area.setFont(new Font("Monospaced", Font.PLAIN, 12));

或者如Denis Tulskiy所述,直接使用JTable组件。