7.5 KiB
7.5 KiB
1. 주요 Swing 컴포넌트 메서드 정리
(1) JLabel 메서드
| 메서드 | 설명 |
|---|---|
setText(String text) |
레이블의 텍스트를 설정 |
getText() |
레이블의 현재 텍스트 반환 |
setIcon(Icon icon) |
아이콘을 설정 |
getIcon() |
현재 설정된 아이콘 반환 |
setHorizontalAlignment(int alignment) |
텍스트/아이콘의 수평 정렬 설정 (SwingConstants.LEFT, CENTER, RIGHT) |
setVerticalAlignment(int alignment) |
텍스트/아이콘의 수직 정렬 설정 (TOP, CENTER, BOTTOM) |
(2) JButton 메서드
| 메서드 | 설명 |
|---|---|
setText(String text) |
버튼의 텍스트 설정 |
getText() |
버튼의 현재 텍스트 반환 |
setEnabled(boolean b) |
버튼 활성화/비활성화 |
setIcon(Icon icon) |
버튼의 아이콘 설정 |
addActionListener(ActionListener l) |
클릭 이벤트 리스너 추가 |
(3) JToggleButton 메서드
| 메서드 | 설명 |
|---|---|
setSelected(boolean b) |
토글 버튼의 선택 상태 설정 |
isSelected() |
현재 선택 상태 반환 |
addItemListener(ItemListener l) |
상태 변경 이벤트 리스너 추가 |
(4) JCheckBox 메서드
| 메서드 | 설명 |
|---|---|
setSelected(boolean b) |
체크박스의 선택 여부 설정 |
isSelected() |
현재 선택 상태 반환 |
addItemListener(ItemListener l) |
체크박스 상태 변경 이벤트 리스너 추가 |
(5) JRadioButton 메서드
| 메서드 | 설명 |
|---|---|
setSelected(boolean b) |
라디오 버튼의 선택 상태 설정 |
isSelected() |
현재 선택 상태 반환 |
setText(String text) |
버튼의 텍스트 설정 |
addItemListener(ItemListener l) |
상태 변경 이벤트 리스너 추가 |
(6) JComponent 메서드 (모든 Swing 컴포넌트의 공통 메서드)
| 메서드 | 설명 |
|---|---|
setBackground(Color c) |
배경색 설정 |
setForeground(Color c) |
전경색(글자색) 설정 |
setFont(Font f) |
폰트 설정 |
setBounds(int x, int y, int width, int height) |
컴포넌트의 위치와 크기 설정 |
setToolTipText(String text) |
툴팁(마우스 오버 시 표시되는 설명) 설정 |
(7) AbstractButton 메서드 (JButton, JToggleButton, JCheckBox, JRadioButton의 공통 메서드)
| 메서드 | 설명 |
|---|---|
setText(String text) |
버튼의 텍스트 설정 |
getText() |
버튼의 현재 텍스트 반환 |
setIcon(Icon icon) |
버튼의 아이콘 설정 |
setSelected(boolean b) |
선택 여부 설정 (토글 버튼 및 체크박스에서 사용) |
isSelected() |
현재 선택 여부 반환 |
addActionListener(ActionListener l) |
액션 이벤트 추가 |
2. 관련 이벤트 정리
| 이벤트 리스너 | 관련 컴포넌트 | 설명 |
|---|---|---|
ActionListener |
JButton, JToggleButton, JCheckBox, JRadioButton |
버튼 클릭 시 동작 |
ItemListener |
JToggleButton, JCheckBox, JRadioButton |
선택 상태가 변경될 때 동작 |
MouseListener |
JLabel, JButton, JToggleButton, JCheckBox, JRadioButton |
마우스 클릭, 이동 등 감지 |
3. Swing 컴포넌트 예제 코드와 설명
(1) JLabel과 JButton을 이용한 간단한 GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("버튼을 눌러보세요!");
JButton button = new JButton("클릭");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("버튼이 클릭됨!");
}
});
frame.add(label);
frame.add(button);
frame.setVisible(true);
}
}
설명:
JLabel을 사용해 텍스트를 출력하고,JButton을 클릭하면JLabel의 텍스트가 변경된다.ActionListener를 사용하여 버튼 이벤트를 감지한다.
(2) JToggleButton과 JCheckBox 사용 예제
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ToggleExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Toggle Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JToggleButton toggleButton = new JToggleButton("OFF");
JCheckBox checkBox = new JCheckBox("체크박스");
toggleButton.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (toggleButton.isSelected()) {
toggleButton.setText("ON");
} else {
toggleButton.setText("OFF");
}
}
});
frame.add(toggleButton);
frame.add(checkBox);
frame.setVisible(true);
}
}
설명:
JToggleButton은 클릭할 때마다 "ON"/"OFF"로 상태가 바뀜.ItemListener를 사용하여 상태 변경을 감지.JCheckBox는 별도의 기능 없이 화면에 추가됨.
(3) JRadioButton을 사용한 단일 선택 예제
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RadioButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio Button Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JRadioButton option1 = new JRadioButton("옵션 1");
JRadioButton option2 = new JRadioButton("옵션 2");
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
JButton button = new JButton("선택 확인");
JLabel label = new JLabel("선택된 옵션: 없음");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (option1.isSelected()) {
label.setText("선택된 옵션: 옵션 1");
} else if (option2.isSelected()) {
label.setText("선택된 옵션: 옵션 2");
}
}
});
frame.add(option1);
frame.add(option2);
frame.add(button);
frame.add(label);
frame.setVisible(true);
}
}
설명:
JRadioButton을 사용해 하나의 옵션만 선택 가능하도록ButtonGroup을 사용.JButton을 클릭하면 선택된 옵션이JLabel에 표시됨.
이제 Swing의 주요 컴포넌트, 이벤트, 예제 코드까지 완벽하게 정리되었다.