add user agent parser and update build configuration
This commit is contained in:
190
docs/swing/ActionListener.md
Normal file
190
docs/swing/ActionListener.md
Normal file
@@ -0,0 +1,190 @@
|
||||
# **ActionListener와 ActionCommand 사용법**
|
||||
|
||||
`ActionListener`는 버튼 클릭, 메뉴 항목 선택 등 사용자 입력에 대한 동작을 처리하는 인터페이스입니다.
|
||||
`ActionCommand`는 특정 이벤트를 식별하는 문자열로, `ActionListener`가 어떤 동작을 수행해야 하는지 구분하는 데 사용됩니다.
|
||||
|
||||
---
|
||||
|
||||
## **1. ActionListener 기본 사용법**
|
||||
`ActionListener`를 사용하려면 `ActionListener` 인터페이스를 구현한 후 `actionPerformed()` 메서드를 오버라이드해야 합니다.
|
||||
|
||||
### **(1) 버튼 클릭 이벤트 처리 예제**
|
||||
```java
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ActionListenerExample {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("ActionListener Example");
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
JPanel panel = new JPanel();
|
||||
|
||||
JButton button = new JButton("클릭하세요");
|
||||
|
||||
// ActionListener 추가
|
||||
button.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JOptionPane.showMessageDialog(null, "버튼이 클릭되었습니다!");
|
||||
}
|
||||
});
|
||||
|
||||
panel.add(button);
|
||||
frame.add(panel);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
```
|
||||
### **설명**
|
||||
1. `JButton`을 생성하고 `"클릭하세요"` 라벨을 설정.
|
||||
2. `button.addActionListener()`를 사용하여 `ActionListener` 추가.
|
||||
3. 버튼을 클릭하면 `actionPerformed()`가 호출되어 메시지 창이 표시됨.
|
||||
|
||||
---
|
||||
|
||||
## **2. ActionCommand 활용하기**
|
||||
기본적으로 버튼의 텍스트가 `ActionCommand`로 설정되지만, 직접 지정할 수도 있습니다.
|
||||
|
||||
### **(1) 여러 버튼을 구분하는 예제**
|
||||
```java
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ActionCommandExample {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("ActionCommand Example");
|
||||
frame.setSize(400, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLayout(new FlowLayout());
|
||||
|
||||
JButton btn1 = new JButton("버튼 1");
|
||||
JButton btn2 = new JButton("버튼 2");
|
||||
|
||||
// ActionCommand 설정
|
||||
btn1.setActionCommand("BUTTON_ONE");
|
||||
btn2.setActionCommand("BUTTON_TWO");
|
||||
|
||||
// 공통 ActionListener
|
||||
ActionListener listener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String command = e.getActionCommand();
|
||||
if ("BUTTON_ONE".equals(command)) {
|
||||
JOptionPane.showMessageDialog(null, "버튼 1이 클릭됨!");
|
||||
} else if ("BUTTON_TWO".equals(command)) {
|
||||
JOptionPane.showMessageDialog(null, "버튼 2가 클릭됨!");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
btn1.addActionListener(listener);
|
||||
btn2.addActionListener(listener);
|
||||
|
||||
frame.add(btn1);
|
||||
frame.add(btn2);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **설명**
|
||||
1. 두 개의 버튼(`btn1`, `btn2`)을 생성.
|
||||
2. `setActionCommand("BUTTON_ONE")` 및 `setActionCommand("BUTTON_TWO")`로 명령어 설정.
|
||||
3. **하나의 `ActionListener`** 에서 `e.getActionCommand()`를 사용하여 어떤 버튼이 눌렸는지 판별.
|
||||
|
||||
---
|
||||
|
||||
## **3. 별도의 ActionListener 클래스로 분리하기**
|
||||
코드를 깔끔하게 유지하려면 `ActionListener`를 별도의 클래스로 분리하는 것이 좋습니다.
|
||||
|
||||
```java
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
class MyActionListener implements ActionListener {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JOptionPane.showMessageDialog(null, "이벤트가 발생했습니다! (" + e.getActionCommand() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
public class SeparateActionListenerExample {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("Separate ActionListener Example");
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
JPanel panel = new JPanel();
|
||||
|
||||
JButton button = new JButton("클릭");
|
||||
button.setActionCommand("CLICK_BUTTON");
|
||||
|
||||
// 별도의 클래스 사용
|
||||
button.addActionListener(new MyActionListener());
|
||||
|
||||
panel.add(button);
|
||||
frame.add(panel);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **설명**
|
||||
- `MyActionListener` 클래스를 만들어 `ActionListener`를 구현.
|
||||
- `JButton`에 `MyActionListener`를 추가.
|
||||
- 클릭하면 `"이벤트가 발생했습니다! (CLICK_BUTTON)"` 메시지를 표시.
|
||||
|
||||
---
|
||||
|
||||
## **4. Lambda 표현식 사용**
|
||||
Java 8 이상에서는 람다를 사용하여 `ActionListener`를 더 간결하게 작성할 수 있습니다.
|
||||
|
||||
```java
|
||||
import javax.swing.*;
|
||||
|
||||
public class LambdaActionListenerExample {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("Lambda ActionListener");
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
JPanel panel = new JPanel();
|
||||
|
||||
JButton button = new JButton("눌러보세요");
|
||||
|
||||
// Lambda 표현식 사용
|
||||
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "람다식으로 처리된 이벤트!"));
|
||||
|
||||
panel.add(button);
|
||||
frame.add(panel);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **설명**
|
||||
- `ActionListener`를 람다 표현식(`e -> { }`)으로 간단하게 구현.
|
||||
- 코드가 더 짧고 가독성이 좋아짐.
|
||||
|
||||
---
|
||||
|
||||
## **5. 총정리**
|
||||
| 개념 | 설명 |
|
||||
|------|------------------------------------------------|
|
||||
| `ActionListener` | 버튼, 메뉴 항목 등의 액션 이벤트를 처리하는 인터페이스 |
|
||||
| `actionPerformed(ActionEvent e)` | 버튼 클릭 등 액션이 발생하면 호출됨 |
|
||||
| `setActionCommand(String command)` | 특정 동작을 식별하기 위한 명령어 설정 |
|
||||
| `getActionCommand()` | 이벤트 발생 시 설정된 `ActionCommand` 값 가져오기 |
|
||||
| **구현 방식** | - **익명 클래스** (`new ActionListener() {}`) 사용<br>- **별도 클래스** 구현<br>- **람다 표현식** (`e -> { }`) 사용 |
|
||||
|
||||
---
|
||||
|
||||
## **6. 어떤 방식을 사용할까?**
|
||||
- **버튼 수가 적다면?** → `익명 클래스` 또는 `람다 표현식`
|
||||
- **여러 개의 버튼을 한 곳에서 처리해야 한다면?** → `ActionCommand` 활용
|
||||
- **큰 프로젝트에서 여러 UI 요소가 같은 이벤트를 공유해야 한다면?** → `별도 클래스`로 `ActionListener` 분리
|
||||
|
||||
위 내용을 이해하면 버튼 클릭뿐만 아니라 메뉴 항목 선택, 키보드 단축키 처리 등 다양한 Swing 이벤트 처리에 활용할 수 있습니다.
|
||||
Reference in New Issue
Block a user