【例 11-16】小应用程序声明一个用户窗口类和对话框类,用户窗口有两个按钮和两个文本框,当点击某个按钮时,对应的对话框被激活。在对话框中输入相应信息,按对话框的确定按钮。确定按钮的监视方法,将对话框中输入的信息传送给用户窗口,并在用户窗口的相应文本框中显示选择信息。 import java.applet.* import javax.swing.*; import java.awt.*; import java.awt.event.*; class MyWindow extends JFrame implements ActionListener{ private JButton button1,button2; private static int flg=0; private static JTextField text1,text2; Mywindow(String s){ super(s); Container con = this.getContentPane(); con.setLayout(new GridLayout(2,2)); this.setSize(200,100); this setLocation(100,100); button1 = new JButton("选择水果"); button2 = new JButton("选择食品"); button1.addActionListener(this); button2.addActionListener(this); text1 = new JTextField(20); text2 = new JTextField(20); con.add(button1); con.add(button2); con.add(text1); con.add(text2); this.setVisible(true); this.pack(); } public static void returnName(String s){ if(flg ==1) text1.setText("选择的水果是:"+s); else if(flg == 2) text2.setText("选择的食品是:"+s); } public void actionPerformed(ActionEvent e){ MyDialog dialog; if(e.getSource()==button1){ dialog = new MyDialog(this,"水果"); dialog.setVisible(true); flg =1; } else if(e.getSource()==button2){ dialog =new MyDialog(this,"食品"); dialog.setVisible(true); flg=2; } } } class MyDialog extends JDialog implements ActionListener{ JLabel title; JTextField text; JButton done; Mydialog(JFrame F,String s){ super(F,s,true);//模态 Container con = this.getContentPane(); title = new JLabel("输入"+s+"名称"); text = new JTextField(10); text.setEditable(true); con.setLayout(new FlowLayout()); con.setSize(200,100); setModal(false); done = new JButton("确定"); done.addActionListener(this); con.setVisible(true); this.pack(); } public void actionPerformed(ActionEvent e){ MyWindow.returnName(text.getText()); setVisible(false); dispose(); } } public class Example6_6 extends Applet{ MyWindow window; MyDialog dialog; public void init(){ window = new MyWindow("带对话框窗口"); } }