AWT 模态与非模态对话框

import java.awt.*;
import java.awt.event.*;

public class TestDialog extends Frame {
 private TextField tfd = new TextField(10);
 private MyDialog myd = null;
 public static final String MODE = "模态";
 public static final String MODELESS = "非模态";
 
 
 public TestDialog()
 {
  Button bt1 = new Button("打开模态对话框");
  Button bt2 = new Button("打开非模态对话框");
  add(tfd,"North");
  add(bt1,"Center");
  add(bt2,"East");
  
  
  bt1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    getMyDialog(MODE);
   }
   
  });
  
  bt2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    getMyDialog(MODELESS);
   }
   
  });  
  
  addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
   } 
  });
   
  
 }
 
 public MyDialog getMyDialog(String mode)
 {
  if(MODE.equals(mode))
  {
   myd = new MyDialog(this,"模态对话框",true);
   myd.setInfo(tfd.getText());
   myd.setVisible(true);
   tfd.setText(myd.getInfo());
  
  }else if(MODELESS.equals(mode))
  {
   myd = new MyDialog(this,"非模态对话框",false);
   myd.setInfo(tfd.getText());
   myd.setVisible(true);
   //tfd.setText(myd.getInfo());
  
  }
   return myd;
 }
 
 public void setInfo(String textArea)
 {
  tfd.setText(textArea);
 }
 /**
  * Method main
  *
  *
  * @param args
  *
  */
 public static void main(String[] args) {
  // TODO: Add your code here
  TestDialog tdg = new TestDialog();
  tdg.setSize(300,300);
  tdg.setVisible(true);
 }
}

 

class MyDialog extends Dialog {
 
 private TextField tfd = new TextField(10);
 private String strInfo = "";
 
 public MyDialog(Frame owner, String title, boolean modal){
  super(owner,title,modal);
  setBounds(0,0,200,150);
  
  Button bt1 = new Button("应用");
  Button bt2 = new Button("确定");
  
  add(tfd,"North");
  add(bt1,"Center");
  add(bt2,"East");
  
  if(this.isModal()==true){
   bt1.setEnabled(false);
  }
  
  bt1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ((TestDialog)MyDialog.this.getOwner()).setInfo(tfd.getText());
   }
   
  });
  
  bt2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    if(isModal()==true){
     strInfo = new String(tfd.getText());
    }else{
     ((TestDialog)MyDialog.this.getOwner()).setInfo(tfd.getText());
    }
    dispose();    
   }
   
  });
  
  addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e){
    dispose();
   } 
  });
 }
 
 
 public void setInfo(String textArea)
 {
  tfd.setText(textArea);
 }
 
 public String getInfo(){
  return tfd.getText();
 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值