小编典典
如果要使用多重标签,则只需HTML在其文本中使用,因为它们支持其使用。因此,使用行制动标签来中断行或在
不要忘记JLabel通过以标签开头的文本来标记要使用HTML 。
编辑:
对于一个工作示例,它显示了一种不同的方法,没有设置样式,并且使用了段落和标签来占用可用空间,请参见以下内容:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class LabelHTMLAutoResize {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
JLabel l = new JLabel("
Some verrrry long text Some verrrry long Some verrrry long text dsa ads oiosi o
");l.setVerticalAlignment(SwingConstants.TOP);
l.setOpaque(true);
l.setBackground(Color.green);
p.add(l);
f.setContentPane(p);
/* good practice is to use f.pack(); and let the size be automatically calculated but we want to show line wrapping thus frame size is set */
f.setSize(200, 200);
f.setVisible(true);
}
});
}
}
2020-11-19