* 다이얼로그(JDialog)란 사용자에게 보여주고자 하는 내용을 출력하고 사용자로부터 입력을 받는 대화 상자이다.
스윙에서는 JDialog를 상속받아 자신만의 다이얼로그를 만들 수 있다.
JDialog는 JFrame처럼 다른 컨테이너에 속할 필요 없이 화면에 출력 가능한 최상위 컨테이너(Top Level Container) 중 하나이다.
JDialog는 기본적으로 BorderLayout 배치관리자를 사용하기 때문에 버튼이 다이얼로그의 중심(CENTER)에 출력된다.
JDialog dialog = new JDialgo(); // 다이얼로그 생성
dialog.setTitle("나의 다이얼로그");
dialog.add(new JButton("clock!");
dialog.setSize(200,200);
dialog.setVisible(true);
* JDialog 클래스의 주요 멤버
메소드 | 내용 |
JDialog() JDialog(Frame owner) JDialog(Frame owner, String Title) JDialog(Frame owner, String Title, boolean modal) |
다이얼로그를 생성하는 생성자이다. owner는 다이얼로그의 주인, title은 타이틀, modal은 다이얼로그의 종류를 뜻한다. modal 값이 true이면 모달 다이얼로그를, false이면 모달리스 다이얼로그를 생성한다. 디폴트는 모달리스 타입이다. |
void setVisible(boolean b) | b가 true이면 다이얼로그를 화면에 출력하고 false이면 보이지 않게 숨긴다. |
void setTitle(String title) | title 문자열을 다이얼로그 타이틀로 설정한다. |
* JDialog를 상속받아 다이얼로그를 만드는 방법
텍스트필드 창 하나와 OK 버튼을 하나 가진 다이얼로그를 만들어보자.
OK버튼에는 Action 리스너를 달아보자
1) 다이얼로그 클래스 정의
새로운 다이얼로그 클래스의 이름을 MyDialog로 하고 JDialog를 상속받는다.
class MyDialog extends JDialog{
}
2) 다이얼로그 생성자 정의
다이어로그의 생성자를 정의한다.
JDialog 클래스의 생성자는 여러가지가 있다.
기본적으로 다이얼로그는 자신을 생성한 주인(owner)에 관한 정보를 주어야 하며 타이틀 역시 대체로 주는 것이 좋다.
class MyDialog extends JDialog{
public MyDialog(JFrame frame, String title){
super(frame, title); // frame은 다이얼로그의 주인이며 title은 다이얼로그의 타이틀이다.
}
}
3) 다이얼로그에 삽입할 컴포넌트 생성
다이얼로그에 삽입할 텍스트필드와 OK 버튼 컴포넌트를 생성한다.
class MyDialog extends JDialog{
JTextField tf = new JTextField(10); // 10칸 크기의 텍스트필드 컴포넌트 생성
JButton okButton = new JButton("OK"); // OK 버튼 생성
public MyDialog(JFrame frame, String title){
super(frame, title);
}
}
4) 다이얼로그에 컴포넌트 부착
다이얼로그의 생성자에 컴포넌트를 부착하는 코드를 작성한다.
다이얼로그의 배치간리자를 FlowLayout으로 설정하고 두 개의 컴포넌트를 삽입해보자.
class MyDialog extends JDialog{
JTextField tf = new JTextField(10);
JButton okButton = new JButton("OK");
public MyDialog(JFrame frame, String title){
super(frame, title);
setLayout(new FlowLayout()); // FlowLayout 배치관리자로 변경
add(tf);
add(okButton);
}
}
5) OK 버튼에 Action 리스너 달기
OK 버튼이 선택되면 다이얼로그가 화면에서 사라지도록 해보자.
다이얼로그는 보이지 않게 되는 것이지 완전히 없어지는 것이 아니다.
class MyDialog extends JDialog{
JTextField tf = new JTextField(10);
JButton OKButton = new JButton("OK");
public MyDialog(JFrame frame, String title){
super(frame, title);
setLayout(new FlowLayout());
add(tf);
add(OKButton);
setSize(200, 100);
OKButton.setActionListener(new ActionListener(){ // Action 리스너 등록
public void actionPerformed(ActionEvent e){
setVisible(false); // OK 버튼이 선택되면 다이얼로그를 화면에서 숨김
}
});
}
}
6) MyDialog 다이얼로그를 생성하는 프레임 작성
마지막으로 MyDialog를 사용하는 프레임과 main() 메소드를 작성한다.
public class DialogEx extends JFrame{
MyDialog dialog;
public DialogEx(){
super("DialogEx 예제 프레임");
dialog = new MyDialog(this, "Test Dialog"); // 다이얼로그 생성, 이때 화면에 보이는 것은 아니다.
JButton btn = new JButton("Show Dialog"); // 프레임에 버튼 삽입
// 버튼이 선택되면 다이얼로그를 작동시킨다.
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.setVisible(true); // 다이얼로그를 출력하고 작동시킨다. 버튼이 선택되면 화면에 보이도록 한다.
}
});
getContentPane().add(btn);
setSize(250, 200);
setVisible(true);
}
public static void main(String[] args){
new DialogEx();
}
}
* 프레임의 생성자에서 MyDialog의 다이얼로그를 생성한다.
이때 다이얼로그가 화면에 보이는 것은 아니다.
MyDialog dialog = new MyDialog(this, "Text Dialog");
그리고 나서 "Show Dialog" 버튼이 선택될 때마다 Action 리스너에서 다음과 같이 다이얼로그가 화면에 보이도록 한다.
dialog.setVisible(true);
* 지금까지 설명한 소스를 바탕으로 만든 Example
package Ch14Ex;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyDialog extends JDialog{
JTextField tf = new JTextField(10);
JButton okButton = new JButton("OK");
public MyDialog(JFrame frame, String title) {
super(frame, title);
setLayout(new FlowLayout());
add(tf);
add(okButton);
setSize(200, 100);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
}
public class DialogEx extends JFrame{
MyDialog dialog;
public DialogEx() {
super("DialogEx 예제 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton("Show Dialog");
dialog = new MyDialog(this, "Test Dialog");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
});
getContentPane().add(btn);
setSize(250,200);
setVisible(true);
}
public static void main(String[] args) {
new DialogEx();
}
}
'IT > 자바공부' 카테고리의 다른 글
명품 자바 프로그래밍 14장, 고급 스윙 컴포넌트 요약 (0) | 2022.08.11 |
---|---|
자바 고급 스윙 컴포넌트, 모달 다이얼로그 & 모달리스 다이얼로그 (0) | 2022.08.11 |
자바 고급 스윙컴포넌트 툴팁, ToolTip (0) | 2022.08.10 |
자바 고급 스윙 컴포넌트, 툴바 ToolBar (0) | 2022.08.10 |
자바 고급 스윙 컴포넌트, 메뉴 만들기 (0) | 2022.08.10 |