Java GridBagConstraints 异常

发布于 2024-11-16 02:34:46 字数 1486 浏览 3 评论 0原文

当我尝试执行此代码时,出现异常:java.lang.IllegalArgumentException:无法添加到布局:约束必须是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;   
}

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

镜像命运

I am getting an exception: java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint when I attempt to execute this code:

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

Where setConstraints does this:

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;   
}

I figure I am either missing something simple, or there is a much larger bug that I can't do anything about. What say you?

MirroredFate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-11-23 02:34:46

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

JSplitPane has its own layout manager-- you shouldn't change it to GridBagLayout. If you want to use GridBagLayout in the panes, create a JPanel to put in the JSplitPane, and set the layout of that panel to GridBagLayout. Then you put the panel in the JSplitPane, and the controls in the panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文