我正在使用 JTextPane 并希望对齐从 StringBuffer 收到的文本结果。运行结果的线程返回一个包含所有请求结果的字符串,稍后,在另一个线程中,我将数据放置在 JTextPane 中。
获取结果(字符串)的代码如下所示:
info.append("\n\nResult:\n");
for (TResult result : results)
info.append(result.getID());
info.append("\n");
for (TResult result : results)
info.append(result.getApprovalDateStr+" "); //the space is used for the alignment
info.append("\n");
for (TResult result : results)
info.append(result.getState+","+result.getCity()+" ");
显然,根据州/城市的长度,屏幕上的结果不一致。任何人都可以指出应该使用什么来整齐地对齐它。这可以通过 StringBuffer 或稍后在 JTextPane 中完成。
谢谢
下面是所需结果的屏幕截图。
最佳答案
通话
textPane.setContentType("text/html");
并使用 html 表格:
info.append("<html><table>");
for (TResult result : results)
info.append("<tr><td>"+result.getID()+"</td><td>"+result.getApprovalDateStr+"</td><td>"+result.getState+","+result.getCity()+"</td></tr>");
info.append("</table></html>");
//then
textPane.setText(info.toString());
关于java - 如何对齐 JtextPane 或 StringBuffer 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178315/