简体   繁体   English

如何调整Java Swing布局中的组件?

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

I have been trying to make a Java Swing GUI in Eclipse Window Builder . 我一直试图在Eclipse Window Builder Java Swing GUI。 After much of toil, I have been able to get to this. 经过大量的辛苦工作,我已经能够做到这一点。 What I did was make a JFrame , add a BorderLayout to it. 我所做的就是制作一个JFrame ,向其中添加BorderLayout I added two panels in the north and south positions of this layout. 我还增加了两个面板northsouth此布局的位置。 I added a few buttons in the bottom panel. 我在底部面板中添加了一些按钮。

My question is from the top panel. 我的问题来自顶部面板。 This panel has a Flow Layout , and has two more panels . 该面板有一个Flow Layout ,并且还有two more panels The left panel has buttons which are at the right place. 左侧面板上的按钮位于右侧。 The right panel is itself not at the right place. 右侧面板本身不在正确的位置。

  1. I want the right panel to be aligned to the right side of the parent window (and the left panel should stay on the left) - so ultimately there will be some space between the right and left panels. 我希望右侧面板与父窗口的右侧对齐 (左侧面板应留在左侧)-因此最终,右侧面板和左侧面板之间会有一些空间。 That is both the right and the left panels should be right and left aligned) and there should be space between them, because I want 4 buttons to the left, and 2 buttons to the right side of my screen. 左右面板都应该左右对齐),并且它们之间应该有间隔,因为我想在屏幕的左侧向左4个按钮,在屏幕右侧向2个按钮。

  2. In the right panel, the two rightmost button among the two buttons should be right aligned , and there should be some space between both the buttons . 在右侧面板中, 两个按钮中最右边的两个按钮应右对齐 ,并且两个按钮 之间应有一定的间距

I am providing the source code. 我正在提供源代码。 I will be grateful if somebody can help/guide me to achieve what I want. 如果有人可以帮助/指导我实现我想要的目标,我将不胜感激。

在此处输入图片说明


SOURCE CODE:- 源代码:-

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);
    }

}

Try this one 试试这个

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);

Note : remove extra setLayout() set for above JPanel s. 注意:删除为上述JPanel设置的额外setLayout()

在此处输入图片说明

The layout you set to the parentPanel_top panel is flowLayout you cant just call the BorderLayout.WEST to specify it to the west.. BorderLayout can only use it not FlowLayout.. 您设置到parentPanel_top面板的布局是flowLayout,您不能只调用BorderLayout.WEST来将其指定为西。BorderLayout只能使用它,而不能使用FlowLayout。

Solution: 解:

change the layout to BorderLayout.. 将布局更改为BorderLayout。

parentPanel_top.setLayout(new BorderLayout());

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

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