2012-06-27 2 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); 

표 1의 GridBagCOnstraints를 살펴 보겠습니다. 내가 아는 한, 첫 번째 매개 변수는 0 열에서 시작하여 3 열 (세 번째 매개 변수)이됩니다. 그리고 button1 세 번째 열에서 시작하고 한 열 너비가됩니다. 그러나 나는 그 결과가 매우 다르다는 것을 알고 있기 때문에 나는 틀린 것을 알고 있다고 생각한다.

enter image description here

나는 훨씬 더 넓은 폭이 좁고 테이블이 버튼을합니다. 어떻게해야합니까? 이 작은 패널은 매우 복잡한 GUI의 작은 부분이므로 GridBag를 사용하여이 작업을 수행하고 싶습니다. 전체 GUI는 그리드 백을 사용하여 설계되었습니다. 따라서 코딩 스타일을 변경하고 싶지는 않습니다.

답변

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에 있습니다. 추가 및 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(); 
      } 
     }); 
    } 
} 

죄송합니다, 그것은 읽기 쉽도록 나쁘다.