簡體   English   中英

如何調整Java Swing布局中的組件?

[英]How to adjust components in Java Swing Layouts?

我一直試圖在Eclipse Window Builder Java Swing GUI。 經過大量的辛苦工作,我已經能夠做到這一點。 我所做的就是制作一個JFrame ,向其中添加BorderLayout 我還增加了兩個面板northsouth此布局的位置。 我在底部面板中添加了一些按鈕。

我的問題來自頂部面板。 該面板有一個Flow Layout ,並且還有two more panels 左側面板上的按鈕位於右側。 右側面板本身不在正確的位置。

  1. 我希望右側面板與父窗口的右側對齊 (左側面板應留在左側)-因此最終,右側面板和左側面板之間會有一些空間。 左右面板都應該左右對齊),並且它們之間應該有間隔,因為我想在屏幕的左側向左4個按鈕,在屏幕右側向2個按鈕。

  2. 在右側面板中, 兩個按鈕中最右邊的兩個按鈕應右對齊 ,並且兩個按鈕 之間應有一定的間距

我正在提供源代碼。 如果有人可以幫助/指導我實現我想要的目標,我將不勝感激。

在此處輸入圖片說明


源代碼:-

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

public class ParentFrame extends JFrame {
    public ParentFrame() {
        getContentPane().setLayout(new BorderLayout(50, 50));

        JPanel parentPanel_bottom = new JPanel();
        getContentPane().add(parentPanel_bottom, BorderLayout.SOUTH);
        JButton btnInstr = new JButton("1");
        parentPanel_bottom.add(btnInstr);
        JButton btnCourse = new JButton("2");
        parentPanel_bottom.add(btnCourse);
        JButton btnModule = new JButton("3");
        parentPanel_bottom.add(btnModule);
        JButton btnDays = new JButton("4");
        parentPanel_bottom.add(btnDays);
        JButton btnXtra = new JButton("5");
        parentPanel_bottom.add(btnXtra);

        JPanel parentPanel_top = new JPanel();
        getContentPane().add(parentPanel_top, BorderLayout.NORTH);
        parentPanel_top.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));


        JPanel topleftpanel = new JPanel();
        parentPanel_top.add(topleftpanel, BorderLayout.WEST);
        JButton button = new JButton("1");
        topleftpanel.add(button);
        JButton button_1 = new JButton("2");
        topleftpanel.add(button_1);
        JButton button_2 = new JButton("3");
        topleftpanel.add(button_2);
        JButton button_3 = new JButton("4");
        topleftpanel.add(button_3);

        JPanel toprightpanel = new JPanel();
        parentPanel_top.add(toprightpanel, BorderLayout.EAST);
        parentPanel_top.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JButton button_4 = new JButton("1");
        toprightpanel.add(button_4);
        JButton button_5 = new JButton("2");
        toprightpanel.add(button_5);
    }

}

試試這個

JPanel parentPanel_top = new JPanel(new GridLayout(1, 2));
...
JPanel topleftpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
parentPanel_top.add(topleftpanel);    
...
JPanel toprightpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
parentPanel_top.add(toprightpanel);

注意:刪除為上述JPanel設置的額外setLayout()

在此處輸入圖片說明

您設置到parentPanel_top面板的布局是flowLayout,您不能只調用BorderLayout.WEST來將其指定為西。BorderLayout只能使用它,而不能使用FlowLayout。

解:

將布局更改為BorderLayout。

parentPanel_top.setLayout(new BorderLayout());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM