由于某种原因,String.Format严重错位 [英] String.Format is misaligned horribly for some reason

查看:94
本文介绍了由于某种原因,String.Format严重错位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm used to using printf, but my research led me to believe I could use String.Format for setting up tables in a JTextArea and that it was essentially the same thing. This is for a rhythm game app. My code is:

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);

对于数组中的每个组件analytics.toString返回显示的字符串.我的格式字符串是相同的,只是表头中的所有内容都是字符串,而表中的大多数内容都是整数,所以我不知道为什么我的表看起来像这样:

for each component in an array analyze.toString returns the string shown. My format strings are identical sans everything being strings in the header and most everything being integers in the table so I don't know why my table comes out looking like this:

推荐答案

就像注释中所述,您应该使用固定宽度字体(或等宽字体)以使这种对齐方式起作用.

Like stated in the comments you should use a fixed-width font (or monospaced font) for such alignments to work.

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

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

Or as stated Denis Tulskiy, directly use the JTable component.

这篇关于由于某种原因,String.Format严重错位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆