我正在尝试对齐包含图像的 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);
最佳答案
检查此示例:http://www.java2s.com/Code/JavaAPI/java.awt/ComponentBOTTOMALIGNMENT.htm
记住: - 设置面板上的布局。 - 设置按钮的对齐方式 - 将按钮添加到面板。
关于java - 如何对齐 JPanel 中的 3 个 JLabel 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8596252/