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

単純なものが欠けているか、私にはどうしようもない大きなバグがあるかのどちらかだと思います。何て言う?

ミラードFate

4

1 に答える 1

3

JSplitPane には独自のレイアウト マネージャーがあります。これを GridBagLayout に変更しないでください。ペインで GridBagLayout を使用する場合は、JSplitPane に配置する JPanel を作成し、そのパネルのレイアウトを GridBagLayout に設定します。次に、パネルを JSplitPane に配置し、コントロールをパネルに配置します。

于 2011-06-20T17:57:29.473 に答える