我有一个非常简单的JLabel,其文本内容相当长。
父布局是GridBagLayout。
我希望容器框架的宽度不超过一定值,比如160像素。
所以我将文本设为“html”,以便JLabel能够将文本换行成多行。
现在我想要“固定”JLabel的宽度。 但是我还没有找到方法。
由于布局管理器不检查JFrame和JLabel的最大大小,因此我不知道是否存在“普通”的解决方案。
以下是代码示例:
public class SSCE extends JFrame {
public static void main(String [] args) {
new SSCE().setVisible(true);
}
public SSCE() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JLabel label = new JLabel("<html>one two three four five six seven eight nine ten eleven twelve thirteen");
add(label, gbc);
gbc.gridy=1;
JLabel label2 = new JLabel("other content");
add(label2, gbc);
pack();
}
}
150%
)在这里不起作用,因为没有父HTML组件,对吧? - Nate W.