简体   繁体   English

JComboBox和字符串格式

[英]JComboBox and string formatting

I've been trying to use String.format to display in the JComboBox, something like this: 我一直在尝试使用String.format在JComboBox中显示,如下所示:

@Override
public String toString(){ return String.format("%-40s %4d", title, n); }

But the result is: JComboBox's appearance 但是结果是: JComboBox的外观

Is there a way to fix this? 有没有办法解决这个问题?

Edit: Desired output 编辑: 所需的输出

Try this when on your addItem: 在您的addItem上尝试以下操作:

cmb1.addItem(String.format("%-40s %39d", "123",123)); // %4d to %39d
cmb1.addItem(String.format("%-40s %4d", "123456789012345678901234567890",-12));

It is different in console and when you add it in JComboBox because in console, It will automatically find the largest length and pad spaces to the other String you format, but in JComboBox , you need to manually change your second argument to align to the next Item in JComboBox . 它在控制台中和在JComboBox添加时有所不同,因为在控制台中,它将自动找到要格式化的其他String的最大长度和填充空间,但是在JComboBox中 ,您需要手动更改第二个参数以与下一个对齐JComboBox中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM