Java 标签中的文本对齐方式
我正在开发一个以充满数字的表格为中心的 Java 应用程序。在某些列中,我想对齐列中的数字(当然是文本),以便小数点在单元格的中心垂直对齐。
我没有看到任何适用于这种情况的对齐常量。 Java中有没有官方的机制来处理这种情况?
预先感谢您的任何帮助。
约翰·多纳
I am developing a Java app centered on a table full of numbers. In some columns, I would like to align the numbers (text, of course) in a column so that the decimal points are aligned vertically at the center of the cells.
I don't see any of the alignment constants which apply to this sort of situation. Is there any official mechanism in Java to handle this situation?
Thanks in advance for any help.
John Doner
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对此并不专业,但它确实可以像我上面提到的那样工作,通过使用 JTextPane 作为自定义单元格渲染器并将选项卡属性设置为小数点对齐。如果执行此操作,则必须注意将数据表示为字符串,并且在编辑数据时将制表符添加到数据字符串的开头。
I'm no pro at this, but it does work as I noted above by using a JTextPane as a custom cell renderer and setting tab attribute to be decimal-aligned. If you do this, you must take care that the data is represented as a String and that when editing data that a tab character be prepended to the beginning of the data String.
如何使用 DecimalFormat 对象使每个数字在小数点后具有相同的位数,并右对齐列。
What about using a DecimalFormat object to make each number have the same count of digits after the decimal and just right aligning the column.