Keyword
-
GUI
-
swing
-
delegation
-
event-driven programming
Java Swing에서는 아래와 같은 component를 지원한다.
게임에서 필요한 몇 가지 component에 대해 알아보자.
컨테이너 class는 다양한 컴포넌트를 담아서 하나의 컴포넌트처럼 관리할 수 있도록 지원하는 class이다.
-
TextField, Label 등과 같은 다른 구성 요소를 포함할 수 있는 awt 구성 요소이다
-
Frame, Dialog, Panel class가 여기에 속한다.
-
구성 요소를 특정 위치에 배치되는 화면으로써, 구성 요소의 레이아웃을 포함하고 제어한다.
컨테이너의 4가지 유형은 다음과 같다.
-
Window
-
테두리와 메뉴 표시줄이 없다
-
-
Panel
-
제목 표시줄, 테두리 또는 메뉴 표시줄을 포함하지 않는다.
-
Button, TextField 등의 다른 구성 요소들을 배치하기 위한 용도로 사용된다.
-
-
Frame
-
제목 표시줄, 테두리 또는 메뉴 표시줄을 포함하지 않는다.
-
Button, TextField 등의 다른 구성 요소들을 배치하기 위한 용도로 사용된다.
-
-
Dialog
AWT에서는 컨테이너 안에서 구성 요소들의 위치를 자동으로 지정해 주는 다양한 layout을 지원한다.
Layout의 종류는 다음과 같다.
-
BorderLayout
-
FlowLayout
-
GridLayout
-
GridBagLayout
-
BoxLayout
-
CardLayout
-
GroupLayout
-
SpringLayout
몇 가지 layout에 대해 알아보자.
다음 코드는 버튼을 Frame의 동쪽에 붙이는 코드이다.
결과는 다음과 같다.
각 버튼의 속성을 보면 아래와 같다.
Button |
가로 위치 |
세로 위치 |
폭 가중치 |
가로 격자수 |
높이 |
1 |
0 |
0 |
|||
2 |
1 |
0 |
0.5 |
||
3 |
2 |
0 |
0.5 |
||
4 |
3 |
0 |
0.5 |
||
5 |
1 |
1 |
0.5 |
2 |
40 |
예제 코드
import java.awt.*;
import javax.swing.*;
public class Exam_11_3_1_2_1 {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
// 제목 설정
frame.setTitle("GridBagLayout");
//// 크기 설정
frame.setSize(400, 130);
//
frame.setLayout(new GridBagLayout());
JButton button = new JButton("Button 1");
constraints.gridx = 0;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 2");
constraints.weightx = 0.5;
constraints.gridx = 1;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 3");
constraints.weightx = 0.5;
constraints.gridx = 2;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 4");
constraints.weightx = 0.5;
constraints.gridx = 3;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 5");
constraints.weightx = 0.5;
constraints.gridx = 1;
constraints.gridwidth = 2;
constraints.gridy = 1;
constraints.ipady = 40;
frame.add(button, constraints);
frame.setVisible(true);
}
}
-
아래 그림과 같은 계산기를 구성한다.
-
버튼
-
0~9 숫자(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-
사칙 연산자(+, -, *, /)
-
초기화(AC)
-
계산(=)
-
-
절대 좌표를 이용한 구성요소 배치
-
SetBounds
-
자동 배치가 되지 않아 원하는 구성이 나오지 않을 수 있음
-
-
LayoutManager 없음
-
setLayout(null)
-
Java Swing에서는 아래와 같은 component를 지원한다.
classDiagram
Object <|-- Component
Component <|-- Container
Container <|-- Window
Window <|-- Frame
Window <|-- Dialog
Frame <|-- JFrame
Dialog <|-- JDialog
Container <|-- JComponent
JComponent <|-- Button
JComponent <|-- JLabel
JComponent <|-- JCheckbox
JComponent <|-- JList
JComponent <|-- JProgressBar
JComponent <|-- AbstractButton
AbstractButton <|-- JButton
AbstractButton <|-- JToggleButton
JToggleButton <|-- JCheckBox
게임에서 필요한 몇 가지 component에 대해 알아보자.
컨테이너 class는 다양한 컴포넌트를 담아서 하나의 컴포넌트처럼 관리할 수 있도록 지원하는 class이다.
-
TextField, Label 등과 같은 다른 구성 요소를 포함할 수 있는 awt 구성 요소이다
-
Frame, Dialog, Panel class가 여기에 속한다.
-
구성 요소를 특정 위치에 배치되는 화면으로써, 구성 요소의 레이아웃을 포함하고 제어한다.
컨테이너의 4가지 유형은 다음과 같다.
-
Window
-
테두리와 메뉴 표시줄이 없다
-
-
Panel
-
제목 표시줄, 테두리 또는 메뉴 표시줄을 포함하지 않는다.
-
Button, TextField 등의 다른 구성 요소들을 배치하기 위한 용도로 사용된다.
-
-
Frame
-
제목 표시줄, 테두리 또는 메뉴 표시줄을 포함하지 않는다.
-
Button, TextField 등의 다른 구성 요소들을 배치하기 위한 용도로 사용된다.
-
-
Dialog
AWT에서는 컨테이너 안에서 구성 요소들의 위치를 자동으로 지정해 주는 다양한 layout을 지원한다.
Layout의 종류는 다음과 같다.
-
BorderLayout
-
FlowLayout
-
GridLayout
-
GridBagLayout
-
BoxLayout
-
CardLayout
-
GroupLayout
-
SpringLayout
몇 가지 layout에 대해 알아보자.
다음 코드는 버튼을 Frame의 동쪽에 붙이는 코드이다.
결과는 다음과 같다.
각 버튼의 속성을 보면 아래와 같다.
Button |
가로 위치 |
세로 위치 |
폭 가중치 |
가로 격자수 |
높이 |
1 |
0 |
0 |
|||
2 |
1 |
0 |
0.5 |
||
3 |
2 |
0 |
0.5 |
||
4 |
3 |
0 |
0.5 |
||
5 |
1 |
1 |
0.5 |
2 |
40 |
예제 코드
import java.awt.*;
import javax.swing.*;
public class Exam_11_3_1_2_1 {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
// 제목 설정
frame.setTitle("GridBagLayout");
//// 크기 설정
frame.setSize(400, 130);
//
frame.setLayout(new GridBagLayout());
JButton button = new JButton("Button 1");
constraints.gridx = 0;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 2");
constraints.weightx = 0.5;
constraints.gridx = 1;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 3");
constraints.weightx = 0.5;
constraints.gridx = 2;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 4");
constraints.weightx = 0.5;
constraints.gridx = 3;
constraints.gridy = 0;
frame.add(button, constraints);
button = new JButton("Button 5");
constraints.weightx = 0.5;
constraints.gridx = 1;
constraints.gridwidth = 2;
constraints.gridy = 1;
constraints.ipady = 40;
frame.add(button, constraints);
frame.setVisible(true);
}
}
-
아래 그림과 같은 계산기를 구성한다.
-
버튼
-
0~9 숫자(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
-
사칙 연산자(+, -, *, /)
-
초기화(AC)
-
계산(=)
-
-
절대 좌표를 이용한 구성요소 배치
-
SetBounds
-
자동 배치가 되지 않아 원하는 구성이 나오지 않을 수 있음
-
-
LayoutManager 없음
-
setLayout(null)
-