欢迎访问我的个人网站==》 jiashubing.cn

带有对话框的父窗口

 

程序功能:创建一个带有文本区及“对话框”按钮的父窗口,单击“对话框”按钮可打开一 个自定义对话框,从中可以定义行和列的数值,关闭对话框其设置的设置会显示在父窗口的文本区中。产生界面如下图所示:

 

 1 import javax.swing.*;
 2 
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 
 6 
 7 public class LX9_20 extends JFrame implements ActionListener
 8 {
 9     int row =10,col =40;
10     JPanel p1 = new JPanel(), p2=new JPanel();
11     JTextArea ta = new JTextArea("文本区行数:"+row+"列数:"+col,row,col);
12     JScrollPane scrollPane = new JScrollPane(ta);
13     Button exit = new Button("关闭");
14     Button dialog = new Button("对话框");
15     JPanel panel = new JPanel();
16     LX9_20()
17     {
18         setContentPane(panel);
19         setTitle("带有对话框的父窗口");
20         panel.setPreferredSize(new Dimension(200, 200));
21         panel.setLayout(new BorderLayout());
22         panel.add("Center",p1);
23         panel.add("South",p2);
24         p1.add(scrollPane);
25         p2.add(exit);
26         p2.add(dialog);
27         exit.addActionListener(this);
28         dialog.addActionListener(this);
29         pack();
30         show();
31     }
32     public static void main(String args[])
33     {
34         new LX9_20();
35     }
36 
37     @Override
38     public void actionPerformed(ActionEvent e) {
39         if(e.getSource()==exit)
40             System.exit(0);
41         else
42         {
43             MyDialog dlg = new MyDialog(this,true);
44             dlg.show();
45         }
46     }
47 
48     class MyDialog extends Dialog implements ActionListener
49     {
50         Label label1 = new Label("请输入行数");
51         Label label2 = new Label("请输入列数");
52         TextField rows = new TextField(50);
53         TextField columns = new TextField(50);
54         Button OK = new Button("确定");
55         Button Cancel = new Button("取消");
56         MyDialog(LX9_20 parent,boolean modal)
57         {
58             super(parent,modal);
59             setTitle("自定义对话框");
60             setSize(260,140);
61             setResizable(false);
62             setLayout(null);
63             add(label1);
64             add(label2);
65             label1.setBounds(50,30,65,20);
66             label2.setBounds(50,60,65,20);
67             add(rows);
68             add(columns);
69             rows.setText(Integer.toString(ta.getRows()));
70             columns.setText(Integer.toString(ta.getColumns()));
71             rows.setBounds(120,30,90,20);
72             columns.setBounds(120,60,90,20);
73             add(OK);
74             add(Cancel);
75             OK.setBounds(60,100,60,25);
76             OK.addActionListener(this);
77             Cancel.addActionListener(this);
78         }
79         @Override
80         public void actionPerformed(ActionEvent e) {
81             if(e.getSource()==OK)
82             {
83                 int row = Integer.parseInt(rows.getText());
84                 int col = Integer.parseInt(columns.getText());
85                 ta.setRows(row);
86                 ta.setColumns(col);
87                 ta.setText("文本区行数:"+row+"列数:"+col);
88                 show();
89             }
90             dispose();
91         }
92     }
93 }

 

 

posted @ 2013-12-31 13:10  贾树丙  阅读(481)  评论(0编辑  收藏  举报