Исключение Java GridBagConstraints - PullRequest
1 голос
/ 20 июня 2011

Я получаю исключение: 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;   
}

Я полагаю, что я либоотсутствует что-то простое, или есть гораздо более крупная ошибка, с которой я ничего не могу поделать.Что скажете вы?

MirroredFate

1 Ответ

3 голосов
/ 20 июня 2011

JSplitPane имеет свой собственный менеджер макета - вы не должны менять его на GridBagLayout. Если вы хотите использовать GridBagLayout на панелях, создайте JPanel для вставки в JSplitPane и установите макет этой панели в GridBagLayout. Затем вы помещаете панель в панель JSplitPane, а элементы управления - на панель.

...