Dialog 프로그래밍
- Frame객체나 다른 Dialog는 항상 Dialog객체를 가지고 있다.
- 다이얼로그가 닫힐때까지 다른 처리를 잠그는 modal이거나
다른 윈도우에서 작업하는 동안 열린 상태로 있게 해주는 modaless일수도 있다.
- Dialog객체는 윈도우 상태가 변할때 윈도우 이벤트가 발생한다.
*
import java.awt.*;
import java.awt.event.*;
public class TestDialog extends Frame implements ActionListener{
Button btnExit;
Button btnYes;
Button btnNo;
Dialog dlgConfirm;
//TestDialog생성
public TestDialog() {
btnExit = new Button("Exit"); //Exit버튼 생성
btnExit.addActionListener(this);
add(btnExit);
this.setLayout(new FlowLayout()); //TestDialog를 위한 레이아웃관리자 설정
dlgConfirm = new Dialog(this); //다이얼로그를 생성하고 클래스에 부착
dlgConfirm.setResizable(false);
btnYes = new Button("Yes"); //다이얼로그에 버튼 추가
btnYes.addActionListener(this);
btnNo = new Button("No");
btnNo.addActionListener(this);
dlgConfirm.add(btnYes);
dlgConfirm.add(btnNo);
dlgConfirm.setTitle("Are you sure?"); //다이얼로그의 제목과 크기 설정
dlgConfirm.setSize(200,100);
//디폴트 BorderLayout()은 서도 다른 버튼을 덮어쓸수 있기 때문에 쓰지 않았다.
dlgConfirm.setLayout(new FlowLayout());
addWindowListener(new WinCloser());
setTitle("Using a Dialog");
setBounds(100,100,300,300);
setVisible(true);
}
//actionPerformed는 버튼클릭과 같은 동작 action이벤트가 발생하면 자동으로 호출
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Exit"))
dlgConfirm.show(); //Dialog객체를 가져와서 출력한다.
if(ae.getActionCommand().equals("Yes"))
System.exit(0);
if(ae.getActionCommand().equals("No"))
dlgConfirm.setVisible(false); //Dialog 사라짐
}
public static void main(String[] args) {
TestDialog td = new TestDialog();
}
}
class WinCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}*