Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSwing中的GridBagLayout_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

JavaSwing中的GridBagLayout

JavaSwing中的GridBagLayout,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我有以下问题。我有一个JPanel和GridBagLayout()集合。我使用相同的布局管理器向它添加另一个JPanel。我有几个不同的东西在里面。问题是我无法设置它们,使标有2号的对象位于1号对象的前面。也就是说,2号对象应该位于左侧的第一个位置 代码如下: public MainPanel() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); Sec

我有以下问题。我有一个
JPanel
GridBagLayout()
集合。我使用相同的布局管理器向它添加另一个
JPanel
。我有几个不同的东西在里面。问题是我无法设置它们,使标有2号的对象位于1号对象的前面。也就是说,2号对象应该位于左侧的第一个位置

代码如下:

public MainPanel() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    SecondPanel secondPanel = new SecondPanel();
    gbc.insets = new Insets(10, 20, 10, 20);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.LINE_END;
    gbc.weightx = 0.1;
    add(secondPanel , gbc);
}

class SecondPanel extends JPanel {
        public SecondPanel () {
        setLayout(new GridBagLayout());
        JPanel main = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(ope, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(opeV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(am, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(amV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(cry, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(cryV, gbc);

        gbc.anchor = GridBagConstraints.BOTH;
        main.add(addButton);

        BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.RED);
        g2d.drawLine(0, 0, 320, 240);
        g2d.drawLine(320, 0, 0, 240);
        g2d.dispose();

        mainL.setIcon(new ImageIcon(img));
        mainL.setBorder(new LineBorder(Color.RED));

        gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        add(mainL, gbc);

        add(main);

        setVisible(true);
    }
}
截图:

编辑:我解决了这个问题,因为这个代码中的第一个错误是错误
add(mainL,gbc)应为
main.add(mainL,gbc)。其次,我分别从
SecondPanel
MainPanel
制作了两个不同的面板。

停下来,去阅读,确保你手头有和

GridBagLayout
是一种令人惊讶的基于“网格”的布局,具有灵活性

如果要将一个组件置于另一个组件之上,则必须告诉布局,两个组件共享同一单元格。这就是
gridx
gridy
非常重要的地方

所以当你做一些像

add(mainL, gbc);
add(main);
您允许布局管理器决定如何最好地布局
main
,这不是您想要的,相反,您需要为这两个组件提供相同的
gridx
/
gridy
约束,例如

我离开了
main
不透明
,添加了一个边框,以便您可以看到它。还要注意的是,组件是以FILO(反向)顺序显示的

GridBagLayout
是一种艺术形式,你只需努力就能掌握它

但这并不能解决我的问题

你是说像

add(mainL, gbc);
add(main);

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(main, gbc);
gbc.gridx++;
add(mainL, gbc);