为何使用HTML格式化JList单元格时,文本会在单元格内居中显示?
问题原因
Swing 内置的HTML渲染引擎会为<ul>标签默认添加左内边距与外边距,这部分默认空白就是文本无法贴紧JList单元格边缘的原因,该问题可以完全修复。
修复方案
方案1:清除ul标签的默认边距
直接在现有<ul>的style属性中追加margin:0;padding:0;即可消除默认空白,修改后的代码如下:
public static void receiveDataEmailList(String data) { Scanner scLine = new Scanner(data).useDelimiter("&&"); int num = scLine.nextInt(); String[] emails = new String[num]; for (int i = 0; i < num; i++) { emails[i] = "<html><ul style=\"list-style-type:none;margin:0;padding:0;\"><li style=\"font-size:10px\">" + scLine.next() + "</li><li style=\"font-size:8px\">" + "Subject: " + "Hello" + "</li></ul></html>"; } EmailList.setListOfEmails(emails); }
方案2:简化HTML结构
仅实现两行文本展示的需求不需要使用ul/li结构,直接用<br/>换行实现更简洁,也不会存在默认边距问题:
emails[i] = "<html><span style=\"font-size:10px\">" + scLine.next() + "</span><br/><span style=\"font-size:8px\">Subject: Hello</span></html>";
内容的提问来源于stack exchange,提问作者betatelo
相关产品推荐
相关产品推荐

