Java/Java Concept

[Java] 레이아웃 매니져

군우 2018. 3. 14. 16:01

배치관리자도 직접 만들 수 있다.

배치관리자 레이아웃 매니져


FlowLayout 배치관리자

 - 왼쪽에서 오른쪽으로 배치한다.

 - 중앙으로 정렬해가며 배치한다.

 - 한 줄에 모든 컴포넌트를 배치할 수 없을때는 다음 줄에 배치한다.


JPanel 컴포넌트

 - 컨테이너 클래스를 상속해서 JFrame 처럼 다른 컴퓨턴트를 얹을 수 있고,

배치 관리자의 지정도 가능하다. 

- jpanel은 눈에 보이는 성격의 컴포넌트가 아니다.


보더 레이아웃.

그리드 레이아웃.



getPreferredSize ()   

추천할만한 크기를 반환하는함수. 

플로우레이아웃도 이 메소드를 사용한다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package swing25;
 
import javax.swing.*;
import java.awt.*;
 
public class FlowLayoutManager {
 
    public static void main(String[] args)
    {
        JFrame frm = new JFrame("FlowLayout Test");
        frm.setBounds(120120400200);
        frm.setLayout(new FlowLayout());
        
        frm.add(new JButton("Hi"));
        frm.add(new JButton("Swing"));
        frm.add(new JButton("button"));
        
        frm.add(new LargeButton("Hi"));
        frm.add(new LargeButton("Swing"));
        frm.add(new LargeButton("button"));
        
        frm.setVisible(true);
    }
}
 
class LargeButton extends JButton 
{
    LargeButton(String str)
    {
        super(str); //상위생성자로 str넘겨주는 부분
    }
    
    public Dimension getPreferredSize()
    {
        Dimension largeBtmSz = new Dimension(
                super.getPreferredSize().width+30,
                super.getPreferredSize().height+15
                );
        return largeBtmSz;
    }
}
cs