簡體   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