【发布时间】:2012-01-25 15:28:13
【问题描述】:
我正在尝试对齐包含图像的 3 个 JLabel 的底部。 3 个 JLabel 保存在一个大 JPanel 中。 I found a tutorial about GUI using Java Swing here. 但由于某种原因,如果我应用示例代码(为按钮提供),它不适用于 JLabels 或 JPanel。 这是来自 Oracle 网站的示例代码:
button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
知道出了什么问题吗?我可以发送我的代码,但我认为这可能会让它变得太混乱,因为对于这里的大多数人来说,这可能是一个简单的答案太简单的问题。
提前致谢。
编辑:
public class LayoutOef_01 extends JFrame{
JPanel paneel;
JLabel label1, label2, label3;
ImageIcon pic1, pic2, pic3;
Border panelBord, labelBord;
public Layout_01(String titel){
super(titel);
paneel = new JPanel();
pic1 = new ImageIcon("images/simon1.png");
pic2 = new ImageIcon("images/simon2.png");
pic3 = new ImageIcon("images/simon3.png");
label1 = new JLabel(pic1);
label2 = new JLabel(pic2);
label3 = new JLabel(pic3);
paneel.add(label1);
paneel.add(label2);
paneel.add(label3);
panelBoord = BorderFactory.createLineBorder(Color.WHITE, 30);
paneel.setBorder(panelBord);
paneel.setBackground(Color.WHITE);
labelBoord = BorderFactory.createLineBorder(Color.BLACK, 2);
label1.setBorder(labelBord);
label2.setBorder(labelBord);
label3.setBorder(labelBord);
this.getContentPane().add(paneel);
this.pack();
}
public static void main(String[] args) {
Layout_01 lay1 = new LayoutOef_01("Layout_01");
lay1.setVisible(true);
}
}
所以我尝试将以下代码 - 在不同的地方 - 放在上面的代码中,但没有任何变化:
label1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label3.setAlignmentY(Component.BOTTOM_ALIGNMENT);
【问题讨论】:
-
添加您的代码。这可能是
Glue的问题,可能是BoxLayout被配置为垂直而不是水平堆叠所有内容。 -
你的代码怎么会太混乱?测试它应该是大约 20 行代码。如果它适用于按钮,它应该适用于标签。发布您的 SSCCE 来证明问题。
标签: java swing user-interface alignment