2011-06-20 54 views
1

我得到一個異常:java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint當我嘗試執行此代碼:的Java GridBagConstraints的異常

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

回答

3

調整JSplitPane有自己的佈局manager--你不應該將其更改爲GridBagLayout的。如果要在窗格中使用GridBagLayout,請創建一個JPanel以放入JSplitPane,並將該面板的佈局設置爲GridBagLayout。然後將面板放入JSplitPane和麪板中的控件。