Java 학습노트(AWT 대화 상자)

1910 단어
Dialog 클래스는 weinow의 하위 클래스이지만 다른 창에 의존해야 합니다.
package dialog;
import java.awt.*;
import java.awt.event.*;
class MyDialogs extends Dialog implements ActionListener {
	
	static final int YES = 1,NO = 0;
	int message = -1;
	Button yes,no;
	MyDialogs(Frame f,String s,boolean b){
		super(f,s,b);
		yes = new Button("YES");
		no = new Button("NO");
		no.addActionListener(this);
		yes.addActionListener(this);
		setLayout(new FlowLayout());
		add(yes);
		add(no);
		setBounds(60, 60, 100, 100);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				message = -1;
				setVisible(false);
			}
		});
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == yes){
			System.out.println("click yes");
			message = YES;
			setVisible(false);
		} else if(e.getSource() == no){
			message = NO;
			setVisible(false);
		}
	}
	public int getMessage(){
		return message;
	}
}
class Dwindow extends Frame implements ActionListener{
	TextArea text;
	Button button;
	MyDialogs  dialog;
	Dwindow(String s){
		super(s);
		text = new TextArea(10,22);
		button = new Button("open dialog");
		button.addActionListener(this);
		setLayout(new FlowLayout());
		add(button);
		add(text);
		dialog = new MyDialogs(this,"my have mode", true);
		setBounds(60, 60, 300,300);
		setVisible(true);
		validate();
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == button){
			dialog.setVisible(true);
			if(dialog.getMessage() == MyDialogs.YES){
				text.append("
yes "); }else if(dialog.getMessage() == MyDialogs.NO){ text.append("
NO "); } } } } public class MyDialog{ public static void main(String args[]){ new Dwindow(" "); } }

좋은 웹페이지 즐겨찾기