Dialog組件
/**?* @(#)TestDialog2.java
?*
?* TestDialog2 application
?*
?* @author
?* @version 1.00 2007/1/20
?*/
import java.awt.*;
import java.awt.event.*;
public class TestDialog2 extends Frame
{
?private TextField tf=new TextField(10);
?
?? public TestDialog2()
?? {
??? ?Button bt1=new Button("打開模態(tài)窗口");
??? ?Button bt2=new Button("打開非模態(tài)窗口");
?? ??add(tf,"North");
?? ??add(bt1,"Center");
?? ??add(bt2,"East");
?? ?
?? ?bt1.addActionListener(new ActionListener()
?? ?{
?? ??public void actionPerformed(ActionEvent e)
?? ??{
?? ???MyDialog dlg=new MyDialog(TestDialog2.this,"模態(tài)窗口",true);
?? ???dlg.setTF(tf.getText());
?? ???dlg.setVisible(true);
?? ??}
?? ?});
?? ?
?? ?bt2.addActionListener(new ActionListener()
?? ?{
?? ??public void actionPerformed(ActionEvent e)
?? ??{
?? ???MyDialog dlg=new MyDialog(TestDialog2.this,"非模態(tài)窗口",false);
?? ???dlg.setTF(tf.getText());
?? ???dlg.setVisible(true);
?? ??}
?? ?});
?? ??? ?
?? ?addWindowListener(new WindowAdapter()
?? ?{
?? ??public void windowClosing(WindowEvent e)
?? ??{
?? ???e.getWindow().dispose();
?? ??}
?? ?});
?? }
??
?? public void setTF(String setInfo)
?? {
?? ??tf.setText(setInfo);
?? }
??
??
??? public static void main(String[] args)
??? {
??? ?
??? ?// TODO, add your application code
??? ?System.out.println("Hello World!");
??? ?TestDialog2 mainFram=new TestDialog2();
??? ?mainFram.setTitle("hello");
??? ?//mainFram.setBackground(Color.lightGray);
??? ?mainFram.setBounds(300,200,400,400);
??? ?mainFram.setVisible(true);
??? }
}
//**************************************************************
import java.awt.*;
import java.awt.event.*;
import java.awt.Dialog;
public class MyDialog extends Dialog
{
?private TextField tf=new TextField(10);
?
?public MyDialog(Frame owner,String title,boolean modal)
?{
??super(owner,title,modal);
??Button b1=new Button("應(yīng)用");
??Button b2=new Button("確定");
??
??add(tf,"North");
??add(b1,"Center");
??add(b2,"East");
??setBounds(0,0,200,200);
?
??if(this.isModal()==true)
??{
???b1.setEnabled(false);
??}
??
??b1.addActionListener(new ActionListener()
??{
???public void actionPerformed(ActionEvent e)
???{
????((TestDialog2)MyDialog.this.getOwner()).setTF(tf.getText());?
???}
??});
??
??b2.addActionListener(new ActionListener()
??{
???public void actionPerformed(ActionEvent e)
???{
????((TestDialog2)MyDialog.this.getOwner()).setTF(tf.getText());
????dispose();
???}
??});
??
??
??addWindowListener(new WindowAdapter()
??{
???public void windowClosing(WindowEvent e)
???{
????e.getWindow().dispose();
???}
??});
?}
?public void setTF(String setInfo)
?{
??tf.setText(setInfo);
?}
}
posted on 2007-01-20 14:18 大頭劍客 閱讀(104) 評(píng)論(0) 編輯 收藏 所屬分類: 學(xué)習(xí)筆記