【问题标题】:How to adjust components in Java Swing Layouts?如何调整 Java Swing 布局中的组件?
【发布时间】:2014-04-28 22:46:44
【问题描述】:

我一直在尝试在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);
    }

}

【问题讨论】:

  • 尝试使用BoxLayoutGridBagLayuout

标签: java swing layout-manager border-layout flowlayoutpanel


【解决方案1】:

试试这个

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

注意:删除为上述JPanels 设置的额外setLayout()

【讨论】:

  • 这里发生了其他事情。我正在上传屏幕截图
  • 看看我的代码parentPanel_top.add(toprightpanel, BorderLayout.EAST); 删除BorderLayout.EAST 因为现在它是GridLayout
  • parentPanel_top.add(topleftpanel, BorderLayout.WEST); 相同
【解决方案2】:

你给parentPanel_top面板设置的布局是flowLayout你不能只调用BorderLayout.WEST来指定它向西..BorderLayout只能用它不能用FlowLayout..

解决方案:

将布局更改为 BorderLayout..

parentPanel_top.setLayout(new BorderLayout());

【讨论】:

  • 很抱歉,我无法理解您刚才所说的话。你的意思是说我应该使用边框布局而不是flowlayout?
猜你喜欢
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2013-10-12
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2013-08-20
相关资源
最近更新 更多