1

我遇到了一个异常: java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint当我尝试执行此代码时:

    //creating the right splitpane
    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    GridBagLayout paneLayout = new GridBagLayout();
    sp.setLayout(paneLayout);
    sp.setContinuousLayout(true);
    sp.setDividerLocation(100);

    //setting constraints
    c = this.setConstraints(GridBagConstraints.ABOVE_BASELINE_TRAILING, GridBagConstraints.NORTH, 1, 1, 2, 2, .5, .5, new Insets(1,1,1,1), 5, 5);
    paneLayout.setConstraints(treeView, c);
    c = this.setConstraints(GridBagConstraints.BELOW_BASELINE_TRAILING, GridBagConstraints.SOUTH, 0, 0, 2, 2, .5, .5, new Insets(1,1,1,1), 5, 5);
    paneLayout.setConstraints(info, c);

    //adding components
    sp.setTopComponent(treeView); // Line with the error
    sp.setBottomComponent(info);

这是在哪里setConstraints

private GridBagConstraints setConstraints(int fill, int anchor, int gheight, int gwidth, int x, int y, double d, double e, Insets insets, int padx, int pady){
    GridBagConstraints c = new GridBagConstraints();
    c.fill = fill;
    c.anchor = anchor;
    c.gridheight = gheight;
    c.gridwidth = gwidth;
    c.gridx = x;
    c.gridy = y;
    c.weightx = d;
    c.weighty = e;
    c.insets = insets;
    c.ipadx = padx;
    c.ipady = pady;
    return c;   
}

我想我要么遗漏了一些简单的东西,要么有一个更大的错误,我无能为力。你说什么?

镜像命运

4

1 回答 1

3

JSplitPane 有自己的布局管理器——您不应该将它更改为 GridBagLayout。如果要在窗格中使用 GridBagLayout,请创建一个 JPanel 以放入 JSplitPane,并将该面板的布局设置为 GridBagLayout。然后你把面板放在 JSplitPane 里,把控件放在面板里。

于 2011-06-20T17:57:29.473 回答