2014-07-06 8 views
0

버튼 그룹이 패널 중간에 있습니다. 유동적 레이아웃 제약 조건에서 패널 맨 위에 배치하려고합니다. 이를 성취하는데 도움이된다면 큰 도움이 될 것입니다. 나는 도크를 시도하고 정렬했지만 아무 것도하지 않았습니다.Miglayout 구성 요소를 셀 위쪽에 정렬

private class ProductPanel extends JPanel { 

    private JLabel lblProd; 
    private JButton butAdd; 
    private JButton butRemove; 
    private JButton butEdit; 
    private Product_Table_Model ptm; 
    private JScrollPane scroll; 
    private JPanel buttonPanel; 
    private JTable table; 

    ProductPanel() { 

     setLayout(new MigLayout("debug")); 

     ptm = new Product_Table_Model(
       (ArrayList<Product>) client 
         .receiveObject("Get_Product_Data")); 

     initComponents(); 

    } 

    public void initComponents() { 

     lblProd = new JLabel ("Product List: "); 

     buttonPanel = new JPanel (new MigLayout()); 
     butAdd = new JButton ("Add"); 
     butRemove = new JButton ("Remove"); 
     butEdit = new JButton ("Edit"); 
     buttonPanel.add(butAdd, "cell 0 0"); 
     buttonPanel.add(butRemove, "cell 0 1"); 
     buttonPanel.add(butEdit, "cell 0 2"); 

     butAdd.setPreferredSize(new Dimension(40, 50)); 

     add(lblProd, "wrap"); 

     table = new JTable(ptm); 
     table.setFillsViewportHeight(true); 
     scroll = new JScrollPane(table); 

     add(scroll); 
     add(buttonPanel); 


    } 

} 
+0

전체 예제를 제공해야합니다. 또 다른 문제가있다. 유동 모드의 버튼은 기본적으로 상단에 정렬됩니다. 코드로 업데이트 된 –

+0

은 이미지 작업을 시도했지만 10 개의 평판이 없습니다. – Grim

답변

2

단추가 예상대로 정렬됩니다. 이 (가) 해당 셀 내에 정렬되지 않은 패널 자체입니다.

add(buttonPanel, "top");

당신은 setPrefferedSize() 방법 단련 시킨다면 크기를 설정해서는 안 : 그래서 다음은 문제를 해결합니다. 대신 크기 그룹을 사용하십시오.

MigLayout은 매우 강력한 레이아웃 관리자이므로 두 개의 레이아웃 관리자가있는 두 개의 패널을 만들 필요가 없습니다. 레이아웃을 훨씬 쉽게 작성할 수 있습니다. 세 개의 서브 셀로 분할 테이블 옆

package com.zetcode; 

import java.awt.EventQueue; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import net.miginfocom.swing.MigLayout; 

public class ProductPanel2 extends JPanel { 

    private JLabel lblProd; 
    private JButton butAdd; 
    private JButton butRemove; 
    private JButton butEdit; 
    private JScrollPane scroll; 
    private JTable table; 

    public ProductPanel2() { 

     initComponents(); 
    } 

    private void initComponents() { 

     setLayout(new MigLayout()); 

     lblProd = new JLabel("Product List: "); 

     butAdd = new JButton("Add"); 
     butRemove = new JButton("Remove"); 
     butEdit = new JButton("Edit"); 

     table = new JTable(); 
     table.setFillsViewportHeight(true); 
     scroll = new JScrollPane(table);   

     add(lblProd, "wrap"); 
     add(scroll); 
     add(butAdd, "split 3, flowy, top, sgx"); 
     add(butRemove, "sgx"); 
     add(butEdit, "sgx");   
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       ProductPanel2 pane = new ProductPanel2(); 
       frame.setContentPane(pane); 
       frame.setSize(350, 250); 
       frame.setLocationRelativeTo(null); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     });   

    } 
} 

셀 : 다음 예는 이러한 하나의 해결책이다. 이 하위 셀에는 세 개의 단추 ( )가 삽입되어 있습니다. 수직 흐름 모드는 flowy 제약 조건으로 설정됩니다. top 제약 조건은 단추를 맨 위에 맞 춥니 다. 마지막으로 sgx 제약 조건은 버튼의 크기를 동일하게 만듭니다.

Snapshot of the example

+0

sgx는 무엇을합니까? – Grim

+0

MigLayout 치트 시트를보십시오. 거기에 모든 제약 조건이 설명되어 있습니다. 그것은 "구성 요소에 크기 그룹 이름을 제공하며, 크기 그룹 이름을 공유하는 모든 구성 요소는 동일한 BoundSize (최소/기본/최대)를 가져옵니다"라고 말합니다. 구문은 sg [이름], sgx [이름], sgy [이름] 중 하나입니다. 버튼의 높이는 기본적으로 같기 때문에 sgx이면 충분합니다. 예제에는 하나의 크기 그룹 만 있으므로 이름을 지정하지 않아도됩니다. –