13. GUI_03

2021. 8. 26. 18:45· 국기훈련과정/JAVA 복습노트

배치관리자 (Layout)

1. FlowLayout 배치관리자

- 좌 -> 우 형식으로 배치

- 상단 중앙에 컴포넌트를 배치함.

- 기본적으로 가운데 정렬됨.

- 화면이 넘칠 경우에는 바로 밑 중앙에 배치.

- 배치관리자를 지정하지 않았을 경우 FlowLayout이 기본적용됨.

 

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
package GUI;
 
import java.awt.FlowLayout;
 
import javax.swing.*;
 
public class GUI_FlowLayout extends JFrame {
    
    public GUI_FlowLayout() {
        setTitle("FlowLayout 배치관리자");
        JPanel container = new JPanel();
        
        //1.컴포넌트 만들기
        JButton b1 = new JButton("버튼 1");
        JButton b2 = new JButton("버튼 2");
        JButton b3 = new JButton("버튼 3");
        JButton b4 = new JButton("버튼 4");
        //2.컨테이너에 컴포넌트 올리기
        //new FlowLayout()
        //new FlowLayout(정렬 - 왼쪽, 오른쪽, 중앙(default))
        //new FlowLayout(정렬, 수평간격, 수직간격)
        //*수평간격: 좌우 컴포넌트 사이의 간격. 픽셀단위 - 기본은 5px
        container.setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 10));        
        container.add(b1);container.add(b2);container.add(b3);container.add(b4);
        //3.프레임에 컨테이너 올리기.
        add(container);
        setBounds(200, 200, 300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
 
    public static void main(String[] args) {
        new Ex18_FlowLayout();
 
    }
 
}
 
Colored by Color Scripter
cs

.RIGHT 때문에 버튼4가 오른쪽에 생성됨.

2. BorderLayout

-NORTH, CENTER, EAST, SOUTH, WEST 로 배치하는 관리자.

 

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
42
43
44
45
46
47
48
package GUI;
 
import java.awt.BorderLayout;
 
import javax.swing.*;
 
public class GUI_BorderLayout extends JFrame{
    
    public GUI_BorderLayout() {
        setTitle("BorderLayout 배치관리자");
        JPanel container = new JPanel();
        
        //1.컴포넌트 만들기
        JButton button1 = new JButton("North");
        JButton button2 = new JButton("South");
        JButton button3 = new JButton("East");
        JButton button4 = new JButton("West");
        JButton button5 = new JButton("Center");
        
        //2.컨테이너에 컴포넌트를 올리기
        //new BorderLayout()
        //new BorderLayout(수평간격, 수직간격)
        //-수평간격: 좌우 컴포넌트 사이의 간격(픽셀단위) - 기본값은 0
        //-수직간격: 상하 컴포넌트 사이의 간격(픽셀단위) - 기본값은 0
        container.setLayout(new BorderLayout(20, 30));
        container.add(button1, BorderLayout.NORTH);
        container.add(button2, BorderLayout.SOUTH);
        container.add(button3, BorderLayout.EAST);
        container.add(button4, BorderLayout.WEST);
        container.add(button5, BorderLayout.CENTER);
        
        
        
        //3.프레임에 컨테이너 올리기
        add(container);
        setBounds(200, 200, 300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        
    }
 
    public static void main(String[] args) {
        new GUI_BorderLayout();
 
    }
 
}
 
Colored by Color Scripter
cs

3. GridLayout

-그리드 형식으로 배치

-행과 열로 구성됨.

-무조건 행 기준.

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
42
43
44
45
46
47
package GUI;
 
import java.awt.GridLayout;
 
import javax.swing.*;
 
public class GUI_GridLayout extends JFrame {
    public GUI_GridLayout() {
        JPanel container = new JPanel();
        //1.컴포넌트 생성.
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b10 = new JButton("*");
        JButton b11 = new JButton("0");
        JButton b12 = new JButton("#");
        //2.컨테이너에 컴포넌트 올리기.
        //형식)
        //new GridLayout(행, 열)
        //new GridLayout(행, 열, 수평간격, 수직간격)
        //수평, 수직: 좌우, 상하 픽셀단위 간격. 기본값은 역시 0
        container.setLayout(new GridLayout(4, 3, 10, 10));
        
        container.add(b1);container.add(b2);container.add(b3);
        container.add(b4);container.add(b5);container.add(b6);
        container.add(b7);container.add(b8);container.add(b9);
        container.add(b10);container.add(b11);container.add(b12);
        //3.프레임에 컨테이너 올리기.
        add(container);
        setBounds(200, 200, 300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        new GUI_GridLayout();
 
    }
 
}
 
Colored by Color Scripter
cs

 

예제.

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package GUI;
 
import java.awt.BorderLayout;
 
import javax.swing.*;
 
public class GUI_Layout03 extends JFrame {
    public GUI_Layout03() {
        setTitle("계산기 예제 -3");
        
        JPanel container1 = new JPanel(); //상단
        JPanel container2 = new JPanel(); //상단
        JPanel container3 = new JPanel(); //중단
        JPanel container4 = new JPanel(); //하단
        
        //1.컴포넌트 만들기
        //1-1 상단 컨테이너 컴포넌트1
        JLabel su1 = new JLabel("수 1");
        JTextField su1_box = new JTextField(5);
        JLabel su2 = new JLabel("수 2");
        JTextField su2_box = new JTextField(5);
        
        //1-2 상단 컨테이너 컴포넌트2
        JLabel op = new JLabel("연산자");
        JRadioButton jrb1 = new JRadioButton("+");
        JRadioButton jrb2 = new JRadioButton("-");
        JRadioButton jrb3 = new JRadioButton("*");
        JRadioButton jrb4 = new JRadioButton("/");
        ButtonGroup bg = new ButtonGroup();
        bg.add(jrb1); bg.add(jrb2); bg.add(jrb3); bg.add(jrb4);
        
        //1-3 중앙 컨테이너 컴포넌트
        JTextArea contents = new JTextArea(10, 20);
        JScrollPane jsp = new JScrollPane(contents, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        contents.setLineWrap(true); //줄바꿈메서드
        
        //1-4 하단 컨테이너 컴포넌트
        JButton result = new JButton("계산");
        JButton exit= new JButton("종료");
        JButton cancel = new JButton("취소");
        
        //2-1 상단 컨테이너 구현
        container1.add(su1); container1.add(su1_box); container1.add(su2); container1.add(su2_box);
        container2.add(op); 
        container2.add(jrb1); container2.add(jrb2); container2.add(jrb3); container2.add(jrb4);
        //2-2 중단 컨테이너
        container3.add(jsp);
        //2-3 하단 컨테이너
        container4.add(result); container4.add(exit); container4.add(cancel);
        
        //같은 구역 정렬시 가려지는 현상 해결하기.
        //새 컨테이너 두개 생성.
        //JPanel containerGroup1 = new JPanel(new BorderLayout());
        JPanel containerGroup2 = new JPanel(new BorderLayout());
        
        //새로운 컨테이너에 기존 컨테이너 올리기
        //containerGroup1.add(container1, BorderLayout.NORTH);
        containerGroup2.add(container2, BorderLayout.NORTH); 
        containerGroup2.add(container3, BorderLayout.CENTER); 
        containerGroup2.add(container4, BorderLayout.SOUTH);
        
        
        //3. 프레임 설계
        add(container1, BorderLayout.NORTH);
        add(containerGroup2, BorderLayout.CENTER);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setBounds(200, 200, 300, 300);
        setVisible(true);
    }
 
    public static void main(String[] args) {
        new GUI_Layout03();
 
    }
 
}
 
Colored by Color Scripter
cs

 

예제2

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package exam;
 
import java.awt.BorderLayout;
 
import javax.swing.*;
 
public class Exam03 extends JFrame {
    public Exam03() {
        
        setTitle("커피 자판기");
        
        //컨테이너 생성.
        JPanel con1 = new JPanel();
        JPanel con2 = new JPanel();
        JPanel con3 = new JPanel();
        JPanel con4 = new JPanel();
        JPanel con5 = new JPanel();
        //컨테이너 그룹화.
        JPanel conGroup1 = new JPanel(new BorderLayout());
        JPanel conGroup2 = new JPanel(new BorderLayout());
        
        conGroup1.add(con1, BorderLayout.NORTH); conGroup1.add(con2, BorderLayout.CENTER);
        conGroup2.add(con3, BorderLayout.NORTH); conGroup2.add(con4, BorderLayout.CENTER); conGroup2.add(con5, BorderLayout.SOUTH);
        
        //컴포넌트 생성
        //최상단
        JLabel title = new JLabel("원하는 커피 선택");
        //선택지
        JRadioButton jb1 = new JRadioButton("아메리카노(2500)");
        JRadioButton jb2 = new JRadioButton("카페모카(3500)");
        JRadioButton jb3 = new JRadioButton("에스프레소(2500)");
        JRadioButton jb4 = new JRadioButton("카페라떼(4000)");
        ButtonGroup bg = new ButtonGroup();
        bg.add(jb1); bg.add(jb2); bg.add(jb3); bg.add(jb4);
        //수량, 입금액
        JLabel count = new JLabel("수   량");
        JTextField countBox = new JTextField(10);
        JLabel money = new JLabel("입금액");
        JTextField moneyBox = new JTextField(10);
        //내용박스
        JTextArea contents = new JTextArea(20, 50);
        //최하단 버튼
        JButton b1 = new JButton("계산");
        JButton b2 = new JButton("종료");
        JButton b3 = new JButton("취소");
        
        //2. 컨테이너에 컴포들 배치
        con1.add(title);
        con2.add(jb1); con2.add(jb2); con2.add(jb3); con2.add(jb4);
        con3.add(count); con3.add(countBox); con3.add(money); con3.add(moneyBox);
        con4.add(contents);
        con5.add(b1); con5.add(b2); con5.add(b3);
        
        //3.프레임에 배치
        add(conGroup1, BorderLayout.NORTH);
        add(conGroup2, BorderLayout.CENTER);
        setBounds(200, 200, 400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        pack();
    }
 
    public static void main(String[] args) {
        new Exam03();
    }
 
}
 
Colored by Color Scripter
cs

'국기훈련과정 > JAVA 복습노트' 카테고리의 다른 글

13. GUI_다중 화면 예제  (0) 2021.09.01
13. GUI_Event 예제  (0) 2021.08.27
13. GUI_Event  (0) 2021.08.27
13. GUI_02  (0) 2021.08.26
13. GUI_01  (0) 2021.08.26
'국기훈련과정/JAVA 복습노트' 카테고리의 다른 글
  • 13. GUI_Event 예제
  • 13. GUI_Event
  • 13. GUI_02
  • 13. GUI_01
Purewater
Purewater
Purewater
프로그램 공부 일기장
Purewater
전체
오늘
어제
  • 분류 전체보기 (237)
    • Front-end (8)
      • Vue (8)
    • 포트폴리오 (3)
      • JSP SHOP (1)
      • Travelanner (1)
      • OAuth2 Blog (1)
    • 취업 준비 (70)
      • Spring boot (43)
      • JPA (1)
      • Spring security (14)
      • React (11)
      • 면접 기록 (1)
    • 국기훈련과정 (57)
      • JAVA 복습노트 (12)
      • Oracle SQL (9)
      • Web (15)
      • JSP (6)
      • Spring (10)
      • 프로젝트 (4)
      • Python (1)
    • 알고리즘 문제풀이 (17)
      • Python3 (13)
      • Kotlin (4)
    • 이것저것 지식 (9)
    • 클라우드 서버 (7)
      • Linux (7)
    • Backend (20)
      • node.js (9)
      • JAVA (6)
      • spring boot (5)
    • DB (1)
      • SQL (1)
    • 잡담 (1)
    • 강의 수강 (1)
    • CS 지식 (41)
      • 디자인패턴 (6)
      • 네트워크 (22)
      • 데이터베이스 (4)

블로그 메뉴

  • 대문
  • 태그
  • 방명록
  • 글쓰기

공지사항

인기 글

태그

  • git
  • 노드
  • Kotlin
  • redis
  • GUI
  • node
  • Oracle
  • node.js
  • Postman
  • js
  • 오라클
  • 디자인패턴
  • JPA
  • db
  • SQL
  • Java
  • 자바스크립트
  • spring
  • gitignore
  • PostgreSQL

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
Purewater
13. GUI_03
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.