2016-06-22 2 views
1

스윙 프로그래밍을 배우려고합니다.자바 스윙 레이아웃 - 사용할 레이아웃

다른 레이아웃의 Java 설명서를 읽었습니다. 나는 또한 약간의 튜토리얼을 읽었다. 그러나 나는 레이아웃이 매우 단순한 Dialog보다 더 많이 사용될 것이라고 정말로 생각할 수 없다. 나는 (WindowBuilder Pro를 통하지 않고) 코드로이 작업을하고 싶다.

빌드하고 싶은 대화 상자입니다. 메모를 제외하고

enter image description here

아무것도 편집 할 수 없습니다.

이 종류의 대화 상자에 가장 적합한 레이아웃은 무엇입니까?

+0

한 가지가 일반적으로 수행이 레이아웃을 결합하는 것입니다. 나는 그것을 여기에서 할 것이다. –

답변

5

레이아웃을 수행하기 위해 GridBagLayout 또는 GroupLayout을 사용할 수 있습니다. 여기

유용한 링크 이해하는 레이아웃 : 그림에 나타난 바와 같이 https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

, 당신은 몇 가지 열 (행에 의해 2)를 가지고해야합니다.


GridBagLayout은 구성 요소를 셀 격자 안에 배치하여 정렬합니다.
반면 GroupLayout은 가로 및 세로 레이아웃과 별도로 작동합니다. 레이아웃은 각 차원에 대해 독립적으로 정의됩니다. 단순하고 전력을 원한다 손으로 GUI 레이아웃을 작성하는 자바 개발자를위한 - 당신 그래서 예를 들면

, 당신은 그룹 또는 GridBagLayout에

+0

나는이 질문을하기 전에 5 번 같은 링크를 읽었다. 그것을 읽은 후, 왜 그리드백이나 그룹이 내 필요에 더 적합한 지 알 수 없었습니다. 그리고 왜 다른 레이아웃도 없습니다. 이유를 이해하는 데 관심이 있습니다 – user93353

+0

다이어그램에서 볼 수 있듯이 일부 열 (행 단위)이 있어야합니다. 이를 위해 GridBagLayout은 구성 요소를 셀 격자 안에 배치하여 정렬합니다. GroupLayout은 수평 및 수직 레이아웃과 별도로 작동합니다. 레이아웃은 각 차원에 대해 독립적으로 정의됩니다. 예를 들어 Group 또는 GridBagLayout으로 정의 된 3 개의 패널 (CustInfo, Lastorders 및 Notes)을 정의해야합니다. –

+2

@ JérèmLeBlond 답변에이 주석을 추가해야합니다. 그것은 대답 자체보다 더 유용합니다. – Endery

4

MigLayout에 의해 정의 된 3 개 패널 (CustInfo, Lastorders 및 메모)를 정의해야합니다 .

1

MigLayout 첫 번째는 GroupLayout 초입니다.

MigLayout와 샘플 솔루션 :

package com.zetcode; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 
import net.miginfocom.swing.MigLayout; 

/** 
* A practical example of MigLayout manager. 
* @author Jan Bodnar 
* Website: zetcode.com 
*/ 
public class CustomerDetailsMigLayoutEx extends JFrame { 

    public CustomerDetailsMigLayoutEx() { 

     initUI(); 
    } 

    private void initUI() { 

     JLabel custId1 = new JLabel("Cust Id"); 
     JLabel custId2 = new JLabel("A525"); 

     JLabel name1 = new JLabel("Name"); 
     JLabel name2 = new JLabel("Joe Beer"); 

     JLabel address1 = new JLabel("Address"); 
     JLabel address2 = new JLabel("112, 1st Street, City, State, Country"); 

     JLabel orders = new JLabel("<html><u style='font-size:13px'>Last 3 Orders</u></html>"); 

     JLabel date1 = new JLabel("11 Dec 2015"); 
     JLabel date2 = new JLabel("17 Dec 2015"); 
     JLabel date3 = new JLabel("19 Dec 2015"); 

     JTextArea area1 = new JTextArea(7, 28); 
     area1.setBorder(BorderFactory.createEtchedBorder()); 

     JTextArea area2 = new JTextArea(7, 28); 
     area2.setBorder(BorderFactory.createEtchedBorder()); 

     JTextArea area3 = new JTextArea(7, 28); 
     area3.setBorder(BorderFactory.createEtchedBorder()); 

     JTextArea area4 = new JTextArea(7, 28); 
     area4.setBorder(BorderFactory.createEtchedBorder());   

     JButton btn1 = new JButton("Submit"); 
     JButton btn2 = new JButton("Cancel"); 

     createLayout(custId1, custId2, name1, name2, address1, address2, 
       orders, date1, area1, date2, area2, date3, area3, 
       area4, btn1, btn2); 

     setTitle("MigLayout example"); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private void createLayout(JComponent... arg) { 

     setLayout(new MigLayout("insets dialog, align 50% 50%, gap 5lp 7lp")); 

     add(arg[0], "split 2, sgx"); 
     add(arg[1], "gapx 15lp, wrap"); 
     add(arg[2], "split 2, sgx"); 
     add(arg[3], "gapx 15lp, wrap"); 
     add(arg[4], "split 2, sgx"); 
     add(arg[5], "gapx 15lp, wrap");   
     add(arg[6], "gapy unrel, wrap"); 
     add(arg[7], "gapy rel, split 2"); 
     add(arg[8], "wrap"); 
     add(arg[9], "split 2"); 
     add(arg[10], "wrap"); 
     add(arg[11], "split 2"); 
     add(arg[12], "wrap"); 
     add(arg[13], "growx");   
     add(arg[14], "split 2, flowy");  
     add(arg[15]);  
     pack(); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(() -> { 
      CustomerDetailsMigLayoutEx ex = new CustomerDetailsMigLayoutEx(); 
      ex.setVisible(true); 
     }); 
    } 
} 

스크린 샷 :

Screenshot of the code example