简体   繁体   English

Java居中多行JLabel文本正确

[英]Java Centring multiline JLabel Text properly

I need to centre two lines of text in a JLabel. 我需要在JLabel中将两行文本居中。 My code is: 我的代码是:

lblSolution.setHorizontalAlignment(JLabel.CENTER);
...
lblSolution.setText("<html>" + result.getNearest() + "<br>" + "<br>" + result.getSolutionString() + "</html>");

The problem is, it centres the longer of the two lines, and aligns the shorter at the same x position as the other. 问题是,它使两条线中的较长的线居中,而将较短的线与另一条线在同一x位置对齐。

这修复了它:

lblSolution.setText("<html><div style='text-align:center'>" + result.getNearest() + "<br>" + "<br>" + result.getSolutionString() + "</div></html>");

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

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