【发布时间】:2014-04-28 22:46:44
【问题描述】:
我一直在尝试在Eclipse Window Builder 中制作 Java Swing GUI。经过大量的努力,我已经能够做到这一点。我所做的是创建一个JFrame,然后添加一个BorderLayout。我在此布局的north 和south 位置添加了两个面板。我在底部面板中添加了一些按钮。
我的问题来自顶部面板。 这个面板有一个Flow Layout,有一个two more panels。左侧面板的按钮位于正确的位置。右侧面板本身不在正确的位置。
我希望右侧面板与父窗口的右侧对齐(左侧面板应保持在左侧) - 所以最终两者之间会有一些空间右侧和左侧面板。 即左右面板都应该左右对齐)并且它们之间应该有空间,因为我希望屏幕左侧有 4 个按钮,右侧有 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);
}
}
【问题讨论】:
-
尝试使用
BoxLayout或GridBagLayuout
标签: java swing layout-manager border-layout flowlayoutpanel