【问题标题】:Adding JPanels to JFrame wiht it's default layout使用默认布局将 JPanel 添加到 JFrame
【发布时间】:2013-03-19 17:08:19
【问题描述】:

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

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

输出:

我想变成这样

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

有什么建议吗?

【问题讨论】:

  • 你可能想看看 MigLayout。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    标签和字段将通过 GridBagLayout 添加到 JPanel。

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

    添加、清除、编辑、保存和删除按钮将被添加到具有 BoxLayout、PAGE_AXIS 的 JPanel。首先应该是编辑和保存按钮,然后是添加、清除和删除按钮。我会在保存和添加按钮之间以及清除和删除按钮之间放置一些空间,以在视觉上分离功能并最大限度地减少意外单击删除按钮。我还会在删除按钮上放一个“你确定”对话框。

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

    四个 JPanel 将通过 GridBagLayout 添加到主 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);
    }
    

    【讨论】:

    • 谢谢,我知道要使用很多 Layout Managers,但我在 BoxLayout 中的按钮之间有空格问题,你能给我举个例子吗?
    • 当然。 Box.createRigidArea(new Dimension(width, height));
    • 在两个 panel.add 语句之间创建一个空格。
    • 再次感谢,如果我不打扰你,我正在最后一步将所有面板添加到主面板,你能告诉我如何做到这一点,这是第一次使用GridBagLayout
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多