2011-01-30 161 views
14
  1. 是否可以爲整行/列設置邊距/填充GridBagLayout?然而,我在約束對象上使用插入,使用這種方法,我需要在每個組件的底部設置填充。GridBagLayout中的邊距/填充Java

  2. 是否可以填充JFrame中的所有內容?因爲現在每個組件都與框架對齊。

constraints.weightx = 2; 
    constraints.weighty = 1; 
    constraints.fill = GridBagConstraints.BOTH; 
    addComponent(this, layout, constraints, questionPanel = new QuestionPanel(), 0, 0, 1, 1); 

    constraints.weightx = 1; 
    constraints.weighty = 1; 
    constraints.fill = GridBagConstraints.BOTH; 
    addComponent(this, layout, constraints, categoryPanel = new CategoryPanel(), 0, 1, 1, 1); 

    constraints.weightx = 1; 
    constraints.weighty = 0; 
    constraints.fill = GridBagConstraints.HORIZONTAL; 
    addComponent(this, layout, constraints, answerPanel = new AnswerPanel(), 1, 0, 2, 1); 

    constraints.weightx = 1; 
    constraints.weighty = 2; 
    constraints.fill = GridBagConstraints.BOTH; 
    addComponent(this, layout, constraints, tabPane = new JTabbedPane(), 2, 0, 2, 1); 

    constraints.weightx = 1; 
    constraints.weighty = 0; 
    constraints.fill = GridBagConstraints.NONE; 
    constraints.anchor = GridBagConstraints.SOUTHEAST; 
    addComponent(this, layout, constraints, buttonPanel = new ButtonPanel(), 3, 1, 1, 1); 

我使用的是私有方法addComponent:

private void addComponent(Container context, GridBagLayout _layout, GridBagConstraints   _constraints, Component component, int row, int column, int width, int height) { 
    _constraints.gridx = column; 
    _constraints.gridy = row; 
    _constraints.gridwidth = width; 
    _constraints.gridheight = height; 

    _layout.setConstraints(component, _constraints); 
    context.add(component); 
} 

我如何可以添加一些 「空氣(填充/保證金)」 的細胞之間?

+0

我會說是和是的......但是,沒有看到一些代碼,很難說清楚。你可以發佈一個片段嗎?這將有很大的幫助,你的問題是非常通用的。 – chahuistle 2011-01-30 10:20:39

回答

6

據我所知,做第一個的唯一方法是爲每個約束設置一個Insets對象......(手工編碼佈局顯然不是在Java中設計得很簡單:P)

但是有一個非常簡單的方法來完成第二個任務,而不會混淆任何Insets或任何東西:將contentPane的佈局設置爲FlowLayout,並且需要任何垂直和水平間隙,將面板添加到contentPane中,然後而不是將所有內容添加到contentPane,將其添加到新面板。

+0

謝謝:)我可能會有關於insets的惡夢.. – LuckyLuke 2011-01-30 11:18:59

30

使用GridBagConstraints

inset屬性例如:

GridBagConstraints c = new GridBagConstraints(); 
c.insets = new Insets(3,3,3,3); 

希望這是你在找什麼。