繁体   English   中英

如何对齐JPanel中保存的3个JLabel的底部

[英]How to align the bottom of 3 JLabels that are held in a JPanel

我试图对齐包含图像的3个JLabel的底部。 3个JLabel被放在一个大的JPanel中。 我在这里找到了一个关于使用Java Swing的GUI的教程。 但由于某些原因,如果我应用示例代码(为按钮提供),它在JLabel或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

请记住: - 在面板上设置布局。 - 在按钮上设置对齐 - 将按钮添加到面板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM