【问题标题】:Making Strings have equal lenght使字符串具有相等的长度
【发布时间】:2015-07-09 19:58:46
【问题描述】:

我想在 JList 中添加一些文本并对齐它,但它不起作用。它没有正确对齐。假设我有两个字符串:String a = "Apple juice is good"; String b = "Fun"; 使用我的代码后,字符串 b 仍然更短

while(isOk){
    String string = String.format("%-100s",text);

    // here i am adding the strings to a JList
    menuModel.add(menuList.getModel().getSize(), string + "L"); 
}   

【问题讨论】:

标签: java


【解决方案1】:

您需要使用等宽字体来呈现文本:

list.setFont( new Font("monospaced", Font.PLAIN, 12) );

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2012-12-21
  • 2021-08-11
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多