另一个例子,表明使用正确的布局管理器,HTML标签中包裹的文本将自动包装到可用空间...
在此处输入图片说明
public class TestHTMLLabel {
public static void main(String[] args) {
new TestHTMLLabel();
}
public TestHTMLLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
StringBuilder sb = new StringBuilder(64);
sb.append("I have something to say, it's beter to burn out then to fade away.").
append(" This is a very long String to see if you can wrap with in").
append("the available space");
JLabel label = new JLabel(sb.toString());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(label);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
本文介绍了一个Java示例程序,该程序演示了如何利用Swing组件JLabel显示自动换行的HTML文本。通过设置合适的布局管理器,可以使得包含长字符串的HTML内容能够在界面上正确显示而不溢出。
850

被折叠的 条评论
为什么被折叠?



