2012-06-27 88 views
3

我有兩個表格和兩個按鈕。我想要table-1在第一列和3個單位的寬度,然後我希望表格右側的兩個按鈕更窄(寬度爲1單位),我想要頂部的按鈕1和頂部的按鈕2底端。現在在這些按鈕的右邊,我想要另一個表格(表格-2)再寬達3個單位。爲了實現這一點,我使用了以下約束:Java GridBag佈局不正常

 //table1 
     c = new GridBagConstraints(0, 0, 3, 2, 1, 1, 
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
       new Insets(0, 0, 0, 0), 0, 0); 
     c.fill = GridBagConstraints.BOTH; 
     outerPanel.add(all_app_scrollpane, c); 

     //button1 
     c = new GridBagConstraints(3, 0, 1, 1, 1, 1, 
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
       new Insets(0, 0, 0, 0), 0, 0); 
     outerPanel.add(addButton, c); 

     //button2 
     c = new GridBagConstraints(3, 1, 1, 1, 1, 1, 
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
       new Insets(0, 0, 0, 0), 0, 0); 
     outerPanel.add(removeButton, c); 

     //table2 
     c = new GridBagConstraints(4, 0, 3, 2, 1, 1, 
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
       new Insets(0, 0, 0, 0), 0, 0); 
     c.fill = GridBagConstraints.BOTH; 
     outerPanel.add(curr_app_scrollpane, c); 

讓我們來看看table1的GridBagCOnstraints。據我所知,第一個參數指出它將從0列開始,並將是3列寬(第三個參數)。 然後button1將從第三列開始,並將是一列寬。但我認爲我知道錯誤,因爲結果與我預期的截然不同。

enter image description here

我想這些按鈕更窄和表寬得多。我怎樣才能做到這一點?我想用GridBag來完成這個任務,因爲這個小面板只是一個非常複雜的GUI的一小部分,整個GUI都是使用gridbag設計的。所以我不想改變編碼風格。

回答

3

您必須設置佈局的增長設置(weightx),以便只有左側和右側列正在增長。

像這樣:

contentPane.setLayout(new GridBagLayout()); 
    ((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0, 0}; 
    ((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 10, 0, 0, 0}; 
    ((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0, 1.0E-4}; 
    ((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4}; 

    //---- leftBtn ---- 
    leftBtn.setText("left Button"); 
    contentPane.add(leftBtn, new GridBagConstraints(0, 0, 1, 5, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
     new Insets(0, 0, 0, 0), 0, 0)); 

    //---- rightBtn ---- 
    rightBtn.setText("right Button"); 
    contentPane.add(rightBtn, new GridBagConstraints(2, 0, 1, 5, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
     new Insets(0, 0, 0, 0), 0, 0)); 

    //---- addBtn ---- 
    addBtn.setText("add Button"); 
    contentPane.add(addBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
     new Insets(0, 0, 0, 0), 0, 0)); 

    //---- remBtn ---- 
    remBtn.setText("rem Button"); 
    contentPane.add(remBtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
     new Insets(0, 0, 0, 0), 0, 0)); 

因此給出了: demo

問候弗洛裏安

編輯:下面一個從IDE截圖,與按鍵之間的額外空間:demo2

+0

你的代碼產生了一個非常不同的結果,並且與我想要的東西非常相似。 GridBagConstraints構造函數的第一個參數是gridx,第三個參數是gridwidth。在你的代碼左邊的按鈕位於gridx 0上,寬度爲1. add和rem按鈕位於gridx1上,寬度爲1.右邊的按鈕位於gridx 2上,寬度爲1.因此,我沒有達到我想要的效果。 – Alptugay

+0

這裏是從IDE的屏幕截圖,你可以看到我已經添加了一些行來將按鈕居中並且在它們之間創建間隙 http://imgur.com/DHEMD – itshorty

+0

在你的屏幕截圖中,所有按鈕都具有相同的寬度。我想要的是使中間的按鈕更窄(我的意思是更寬) – Alptugay

3

在button1和button2上,將它們的weightx減小到0.0。事情是這樣的:

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.util.Vector; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 

public class Test4 { 

    protected void initUI() { 
     JFrame frame = new JFrame("test"); 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 0.5; 
     gbc.weighty = 1.0; 
     gbc.gridheight = 2; 
     panel.add(getTable(), gbc); 
     gbc.gridx = 2; 
     panel.add(getTable(), gbc); 

     GridBagConstraints gbc2 = new GridBagConstraints(); 
     gbc2.fill = GridBagConstraints.NONE; 
     gbc2.anchor = GridBagConstraints.CENTER; 
     gbc2.gridx = 1; 
     gbc2.weighty = 1.0; 
     panel.add(new JButton("Button 1"), gbc2); 
     gbc2.gridy = 1; 
     panel.add(new JButton("Button 2"), gbc2); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private Component getTable() { 
     Vector<Vector<String>> data = new Vector<Vector<String>>(); 
     for (int i = 0; i < 10; i++) { 
      Vector<String> row = new Vector<String>(); 
      for (int j = 0; j < 3; j++) { 
       row.add("Cell (" + i + "," + j + ")"); 
      } 
      data.add(row); 
     } 
     Vector<String> columns = new Vector<String>(); 
     columns.add("Column 1"); 
     columns.add("Column 2"); 
     columns.add("Column 3"); 
     DefaultTableModel model = new DefaultTableModel(data, columns); 
     JTable table = new JTable(model); 
     JScrollPane scroll = new JScrollPane(table); 
     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     return scroll; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test4().initUI(); 
      } 
     }); 
    } 
} 

很抱歉的約束重用,這是不好的可讀性。