A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨宗彬 中级黑马   /  2012-8-4 17:29  /  1124 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.yzb.GUI;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestDialog extends Frame {
private TextField tf = new TextField("www.baidu.com");

public TestDialog(){
  Button b1 = new Button("打开模态窗口");
  Button b2 = new Button("打开非模态窗口");
  add(tf,"North");
  add(b1,"West");
  add(b2,"East");
  
  b1.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    MyDialog md = new MyDialog(TestDialog.this, "模态窗口", true);
    md.setInfo(tf.getText());
    md.setVisible(true);
    tf.setText(md.getInfo());
   
   }
  });
  
  b2.addActionListener(new ActionListener() {
   
    @Override
    public void actionPerformed(ActionEvent e) {
     // TODO Auto-generated method stub
     MyDialog md = new MyDialog(TestDialog.this, "非模态窗口", false);
     md.setInfo(tf.getText());
     md.setVisible(true);
    }
   });
  
  addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    super.windowClosing(e);
    dispose();
    System.exit(0);
   }
  });
}

public static void main(String[] args) {
  // TODO Auto-generated method stub
  TestDialog td = new TestDialog();
  td.setSize(500, 500);
  td.setTitle("TestDialog");
  td.setVisible(true);
}
/*public void setInfo(String s) {
  // TODO Auto-generated method stub
  tf.setText(s);
}*/
public void setInfo(String text) {
  // TODO Auto-generated method stub
  tf.setText(text);
}

}


package com.yzb.GUI;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyDialog extends Dialog {

private TextField tf = new TextField();
private String s = null;
Button b1 = new Button("应用");
Button b2 = new Button("确定");

MyDialog(Frame owner,String title,boolean modal){
  super(owner,title,modal);
  add(tf,"North");
  add(b1,"West");
  add(b2,"East");
  setBounds(100,100,300, 300);
  
  if(modal == true){
   b1.setEnabled(false);
  }
  
  b1.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    ((TestDialog)getOwner()).setInfo(tf.getText());
   }
  });
  
  b2.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
   if(isModal() == true){
     s = new String(tf.getText());
    }else{
     ((TestDialog)getOwner()).setInfo(tf.getText());
    }

    dispose();
   }
  });
  addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    // TODO Auto-generated method stub
    super.windowClosing(e);
    dispose();
   
   }
  });
}
public void setInfo(String s) {
  // TODO Auto-generated method stub
  tf.setText(s);
}
public String getInfo() {
  // TODO Auto-generated method stub
  return s;
}

}

程序运行没问题。但是为搞不明白加个if判断是为什么,我们要那个String s 是干什么用的,getText()的返回值是个String 类型我们为什么要输入一个text,还有为什么要在我们Frame里设置Dialog的显示,如果在Dialog里面自己写setVisable()就不能关闭dialog,难道是我的MyEclipse有问题?

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马