2012-12-05 84 views
0

我在gridbagLayout中添加四個組件。我想增加他們之間的垂直距離如何添加垂直距離? 組件是向gridBag佈局中的組件添加垂直距離

datfeild(JLabel的) billno(JTextFEILD) 日期(JTextField中) 搜索JButton的)刪除(JButton的)

我試圖通過使widthy = 1.0;但沒有得到。請幫我..謝謝..

enter code here 


    b() 
{ 
    GridBagLayout gridBag = new GridBagLayout(); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    this.setLayout(gridBag); 
    panel.setLayout(gridBag); 

    JButton search=new JButton(); 
    JButton delete=new JButton(); 

    JLabel dateFieldLabel = new JLabel("Date Field"); 
    JTextField thingNameField = new JTextField("Billno"); 
    JTextField thingdateField = new JTextField("Date"); 


gbc.gridx = 0; 
gbc.gridy = 0; 
this.add(new JScrollPane(table),gbc); 


gbc.gridx = 0; 
gbc.gridy = 1; 
gbc.insets = new Insets(2,2,2,2); 
gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0; 

panel.add(dateFieldLabel, gbc); 




gbc.gridx = 0; 
gbc.gridy = 2; 
gbc.insets = new Insets(2,2,2,2); 
gbc.fill = GridBagConstraints.BOTH; 
gbc.gridwidth = 2; 
gbc.fill = GridBagConstraints.HORIZONTAL; 
gbc.weightx = 0.0; 
gbc.weighty = 0.0; 
panel.add(thingNameField, gbc);   





gbc.gridx = 0; 
gbc.gridy = 3; 
gbc.insets = new Insets(2,2,2,2); 
gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 0.0; 
gbc.weighty = 1.0; 
gbc.gridwidth = 2; 
gbc.fill = GridBagConstraints.HORIZONTAL; 
panel.add(thingdateField, gbc); 

gbc.gridx = 0; 
gbc.gridy = 4; 
gbc.gridwidth = 1; 
gbc.fill = GridBagConstraints.BOTH; 
gbc.fill = GridBagConstraints.NONE; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0; 

gbc.insets = new Insets(2,2,2,2); 
panel.add(search, gbc); 

gbc.gridx = 1; 
gbc.gridy = 4; 
gbc.gridwidth = 1; 
gbc.fill = GridBagConstraints.BOTH; 
gbc.fill = GridBagConstraints.NONE; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0; 
gbc.insets = new Insets(2,2,2,2); 
panel.add(delete, gbc); 
gbc.anchor = GridBagConstraints.NORTHEAST; 

this.add(panel); 


    } 

回答

2

有類GridBagConstraints其中有場insets。這允許設置組件插圖:

Component c = ... 
GridBagLayout gbl = ... 
Container container = new JPanel(gbl); 
GridBagConstraints gbc = gbl.getConstraints(c); 
gbc.insets = new Insets(....); 

container.add(c, gbc); 
2

你有權在你的鼻子了答案:用Insets

gbc.insets = new Insets(2,2,2,2); 

可能成爲

gbc.insets = new Insets(12,2,12,2);