使用默认布局将JPanels添加到JFrame

时间:2013-03-19 17:08:19

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

很快你就可以阅读我的代码并查看我把它作为图片的输出

/*
 * Suppose I have 4 buttons vertically on the right hand side in First PAnel
 * and 4 buttons on bottom horizantally in second Panel
 * and 4 text fiedls in the center in 4 rows in third Panel
 * Using Frame's default border 
 */
       JPanel p1= new JPanel();
        for (int i = 0; i < right.length; i++) {
            right[i]=new JButton("right "+(i+1));
            p1.add(right[i]);
        }
        JPanel p2 = new JPanel();
        for (int i = 0; i < down.length; i++) {
            down[i] = new JButton("Down "+(i+1));
            p2.add(down[i]);
        }

        JPanel p3=new JPanel();
        for(int i = 0 ; i<text.length;i++){
            text[i]=new JTextField(30);
            p3.add(text[i]);
        }
        Container c =getContentPane();
        c.add(p1,"East");
        c.add(p2,"South");
        c.add(p3,"Center");
        setSize(300,400);
        setVisible(true);
        setDefaultCloseOperation(3);

输出: enter image description here

我想这样做 enter image description here

注意第二个输出我使用了Null布局和setBounds方法

有任何建议吗?

2 个答案:

答案 0 :(得分:1)

为右侧和底部的按钮创建面板 - 使用FlowLayout。为标签和文本字段创建另一个面板,并使用GridLayoutGridBagLayout

答案 1 :(得分:1)

首先,您应始终使用一个或多个布局管理器。

标签和字段将添加到带有GridBagLayout的JPanel。

第一个,上一个,下一个和最后一个按钮将添加到带有BoxLayout,LINE_AXIS的JPanel中。按钮应按照我给出的顺序,第一个,上一个,下一个和最后一个。这就是用户习惯的。

添加,清除,编辑,保存和删除按钮将添加到带有BoxLayout,PAGE_AXIS的JPanel中。首先是编辑和保存按钮,然后是添加,清除和删除按钮。我在保存和添加按钮之间以及清除和删除按钮之间放置了一些空格,以便在视觉上分离功能并最小化意外点击删除按钮。我还在删除按钮上放了一个“你确定”的对话框。

退出按钮将添加到带有FlowLayout的JPanel。

将使用GridBagLayout将四个JPanel添加到主JPanel中。

主JPanel将添加到JFrame。

编辑添加:这是我将四个JPanel添加到主JPanel的代码。

protected static final Insets spaceInsets = new Insets(10, 10, 4, 10);

protected JPanel panel;

protected JPanel formPanel;
protected JPanel nextPanel;
protected JPanel editPanel;
protected JPanel exitPanel;

protected void createPartControl() {
    panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    int gridy = 0;
    gridy = createPanelLayout(gridy);
}

protected int createPanelLayout(int gridy) {
    addComponent(panel, formPanel, 0, gridy, 1, 1, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    addComponent(panel, editPanel, 1, gridy++, 1, 1, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    addComponent(panel, nextPanel, 0, gridy, 1, 1, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    addComponent(panel, exitPanel, 1, gridy++, 1, 1, spaceInsets,
            GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    return gridy;
}

protected void addComponent(Container container, Component component,
        int gridx, int gridy, int gridwidth, int gridheight, 
        Insets insets, int anchor, int fill) {
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
            gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
    container.add(component, gbc);
}