2015-11-05 197 views
0

我有需要被加入Int一排此起彼伏:与调整大小,固定大小的Swing布局标签

First View

但是当第一JLabel包含很长的文本我想它被截断,但显示整个第二JLabel,像以下:

enter image description here

我试过BorderLayout为此,但它使第二个标签文本始终对齐JPanel这不正是我需要的权利。

您能否建议正确的方法来达到所需的布局?

+0

截断代码中第一个标签的文本。 –

+1

文本应该被自动截断。它应该是LayoutManager的责任。 –

回答

2

您可以使用横向BoxLayout

关键是要使第一个元件的最小尺寸(0,0);

Box box = Box.createHorizontalBox(); 

JLabel longLabel = new JLabel("this has lots fo text"); 
longLabel.setMinimumSize(new Dimension(0, 0)); 
box.add(longLabel); 

box.add(Box.createHorizontalStrut(5)); 

JLabel shortLabel = new JLabel("little text"); 
box.add(shortLabel); 
+0

完美!正是我需要的!非常感谢您的帮助。 –