Java学习笔记(AWT 之 对话框)

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("\n你单击了对话框的yes按钮");
			}else if(dialog.getMessage() == MyDialogs.NO){
				text.append("\n你单击了对话框的NO按钮");
			}
		}
	}	
}
public class MyDialog{
	public static void main(String args[]){
		new Dwindow("带对话框的窗口");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值