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();
}
}