Java2实用教程(第二版)程序代码——第十六章 建立对话框
1
//例子1
2
import java.awt.event.*; import java.awt.*;
3
class MyDialog extends Dialog implements ActionListener //建立对话框类。
4
{ static final int YES=1,NO=0;
5
int message=-1; Button yes,no;
6
MyDialog(Frame f,String s,boolean b) //构造方法。
7
{ super(f,s,b);
8
yes=new Button("Yes"); yes.addActionListener(this);
9
no=new Button("No"); no.addActionListener(this);
10
setLayout(new FlowLayout());
11
add(yes); add(no);
12
setBounds(60,60,100,100);
13
addWindowListener(new WindowAdapter()
14
{ public void windowClosing(WindowEvent e)
15
{ message=-1;setVisible(false);
16
}
17
}
18
);
19
}
20
public void actionPerformed(ActionEvent e)
21
{ if(e.getSource()==yes)
22
{ message=YES;setVisible(false);
23
}
24
else if(e.getSource()==no)
25
{ message=NO;setVisible(false);
26
}
27
}
28
public int getMessage()
29
{ return message;
30
}
31
}
32
class Dwindow extends Frame implements ActionListener
33
{ TextArea text; Button button; MyDialog dialog;
34
Dwindow(String s)
35
{ super(s);
36
text=new TextArea(5,22); button=new Button("打开对话框");
37
button.addActionListener(this);
38
setLayout(new FlowLayout());
39
add(button); add(text);
40
dialog=new MyDialog(this,"我有模式",true);
41
setBounds(60,60,300,300); setVisible(true);
42
validate();
43
addWindowListener(new WindowAdapter()
44
{ public void windowClosing(WindowEvent e)
45
{ System.exit(0);
46
}
47
}
48
);
49
}
50
public void actionPerformed(ActionEvent e)
51
{ if(e.getSource()==button)
52
{ dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句。
53
//对话框消失后下面的语句继续执行:
54
if(dialog.getMessage()==MyDialog.YES) //如果单击了对话框的"yes"按钮。
55
{ text.append("\n你单击了对话框的yes按钮");
56
}
57
else if(dialog.getMessage()==MyDialog.NO) //如果单击了对话框的"no"按钮。
58
{ text.append("\n你单击了对话框的No按钮");
59
}
60
}
61
}
62
}
63
public class Example16_1
64
{ public static void main(String args[])
65
{ new Dwindow("带对话框的窗口");
66
}
67
}
68![]()
69
//例子2
70
import java.awt.*;import java.awt.event.*;
71
public class Example16_2
72
{ public static void main(String args[])
73
{ FWindow f=new FWindow("窗口");
74
}
75
}
76
class FWindow extends Frame implements ActionListener
77
{ FileDialog filedialog_save,
78
filedialog_load;//声明2个文件对话筐
79
MenuBar menubar;
80
Menu menu;
81
MenuItem itemSave,itemLoad;
82
TextArea text;
83
FWindow(String s)
84
{ super(s);
85
setSize(300,400);setVisible(true);
86
text=new TextArea(10,10);
87
add(text,"Center"); validate();
88
menubar=new MenuBar();menu=new Menu("文件");
89
itemSave=new MenuItem("保存文件"); itemLoad=new MenuItem("打开文件");
90
itemSave.addActionListener(this); itemLoad.addActionListener(this);
91
menu.add(itemSave); menu.add(itemLoad);
92
menubar.add(menu);
93
setMenuBar(menubar);
94
filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
95
filedialog_save.setVisible(false);
96
filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);
97
filedialog_load.setVisible(false);
98
filedialog_save.addWindowListener(new WindowAdapter()//对话框增加适配器。
99
{ public void windowClosing(WindowEvent e)
100
{ filedialog_save.setVisible(false);
101
}
102
});
103
filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器。
104
{public void windowClosing(WindowEvent e)
105
{ filedialog_load.setVisible(false);
106
}
107
});
108
addWindowListener(new WindowAdapter() //窗口增加适配器。
109
{public void windowClosing(WindowEvent e)
110
{ setVisible(false);System.exit(0);
111
}
112
});
113
}
114
public void actionPerformed(ActionEvent e) //实现接口中的方法。
115
{ if(e.getSource()==itemSave)
116
{ filedialog_save.setVisible(true);
117
String name=filedialog_save.getFile();
118
if(name!=null)
119
{ text.setText("你选择了保存文件,名字是"+name);
120
}
121
else
122
{ text.setText("没有保存文件");
123
}
124
}
125
else if(e.getSource()==itemLoad)
126
{ filedialog_load.setVisible(true);
127
String name=filedialog_load.getFile();
128
if(name!=null)
129
{ text.setText("你选择了打开文件,名字是"+name);
130
}
131
else
132
{ text.setText("没有打开文件");
133
}
134
}
135
}
136
}
137![]()
138
//例子3
139
import java.awt.event.*;import java.awt.*;
140
import javax.swing.JOptionPane;
141
class Dwindow extends Frame implements ActionListener
142
{ TextField inputNumber;
143
TextArea show;
144
Dwindow(String s)
145
{ super(s);
146
inputNumber=new TextField(22); inputNumber.addActionListener(this);
147
show=new TextArea();
148
add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER);
149
setBounds(60,60,300,300); setVisible(true);
150
validate();
151
addWindowListener(new WindowAdapter()
152
{ public void windowClosing(WindowEvent e)
153
{ System.exit(0);
154
}
155
}
156
);
157
}
158
public void actionPerformed(ActionEvent e)
159
{ boolean boo=false;
160
if(e.getSource()==inputNumber)
161
{ String s=inputNumber.getText();
162
char a[]=s.toCharArray();
163
for(int i=0;i<a.length;i++)
164
{ if(!(Character.isDigit(a[i])))
165
boo=true;
166
}
167
if(boo==true) //弹出"警告"消息对话框。
168
{ JOptionPane.showMessageDialog(this,"您输入了非法字符","警告对话框",
169
JOptionPane.WARNING_MESSAGE);
170
inputNumber.setText(null);
171
}
172
else if(boo==false)
173
{ int number=Integer.parseInt(s);
174
show.append("\n"+number+"平方:"+(number*number));
175
}
176
}
177
}
178
}
179
public class Example16_3
180
{ public static void main(String args[])
181
{ new Dwindow("带对话框的窗口");
182
}
183
}
184![]()
185
//例子4
186
import java.awt.event.*;import java.awt.*;
187
import javax.swing.JOptionPane;
188
class Dwindow extends Frame implements ActionListener
189
{ TextField inputName;
190
TextArea save;
191
Dwindow(String s)
192
{ super(s);
193
inputName=new TextField(22);inputName.addActionListener(this);
194
save=new TextArea();
195
add(inputName,BorderLayout.NORTH); add(save,BorderLayout.CENTER);
196
setBounds(60,60,300,300); setVisible(true);
197
validate();
198
addWindowListener(new WindowAdapter()
199
{ public void windowClosing(WindowEvent e)
200
{ System.exit(0);
201
}
202
}
203
);
204
}
205
public void actionPerformed(ActionEvent e)
206
{ String s=inputName.getText();
207
int n=JOptionPane.showConfirmDialog(this,"确认正确吗?","确认对话框",
208
JOptionPane.YES_NO_OPTION );
209
if(n==JOptionPane.YES_OPTION)
210
{ save.append("\n"+s);
211
}
212
else if(n==JOptionPane.NO_OPTION)
213
{ inputName.setText(null);
214
}
215
}
216
}
217
public class Example16_4
218
{ public static void main(String args[])
219
{ new Dwindow("带对话框的窗口");
220
}
221
}
222![]()
223
//例子5
224
import java.awt.event.*;
225
import java.awt.*;
226
import javax.swing.JColorChooser;
227
class Dwindow extends Frame implements ActionListener
228
{ Button button;
229
Dwindow(String s)
230
{ super(s);
231
button=new Button("打开颜色对话框");
232
button.addActionListener(this);
233
setLayout(new FlowLayout());
234
add(button);
235
setBounds(60,60,300,300);
236
setVisible(true);
237
validate();
238
addWindowListener(new WindowAdapter()
239
{ public void windowClosing(WindowEvent e)
240
{
241
System.exit(0);
242
}
243
}
244
);
245![]()
246
}
247
public void actionPerformed(ActionEvent e)
248
{
249
Color newColor=JColorChooser.showDialog(this,"调色板",button.getBackground());
250
button.setBackground(newColor);
251
}
252
}
253
public class Example16_5
254
{ public static void main(String args[])
255
{
256
new Dwindow("带颜色对话框的窗口");
257
}
258
}
259![]()
//例子12
import java.awt.event.*; import java.awt.*;3
class MyDialog extends Dialog implements ActionListener //建立对话框类。4
{ static final int YES=1,NO=0;5
int message=-1; Button yes,no;6
MyDialog(Frame f,String s,boolean b) //构造方法。7
{ super(f,s,b);8
yes=new Button("Yes"); yes.addActionListener(this);9
no=new Button("No"); no.addActionListener(this);10
setLayout(new FlowLayout());11
add(yes); add(no);12
setBounds(60,60,100,100);13
addWindowListener(new WindowAdapter()14
{ public void windowClosing(WindowEvent e)15
{ message=-1;setVisible(false);16
}17
}18
);19
}20
public void actionPerformed(ActionEvent e)21
{ if(e.getSource()==yes) 22
{ message=YES;setVisible(false);23
}24
else if(e.getSource()==no)25
{ message=NO;setVisible(false);26
}27
}28
public int getMessage()29
{ return message;30
}31
}32
class Dwindow extends Frame implements ActionListener 33
{ TextArea text; Button button; MyDialog dialog;34
Dwindow(String s)35
{ super(s);36
text=new TextArea(5,22); button=new Button("打开对话框"); 37
button.addActionListener(this);38
setLayout(new FlowLayout());39
add(button); add(text); 40
dialog=new MyDialog(this,"我有模式",true);41
setBounds(60,60,300,300); setVisible(true);42
validate();43
addWindowListener(new WindowAdapter()44
{ public void windowClosing(WindowEvent e)45
{ System.exit(0);46
}47
}48
);49
}50
public void actionPerformed(ActionEvent e)51
{ if(e.getSource()==button)52
{ dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句。53
//对话框消失后下面的语句继续执行:54
if(dialog.getMessage()==MyDialog.YES) //如果单击了对话框的"yes"按钮。55
{ text.append("\n你单击了对话框的yes按钮");56
}57
else if(dialog.getMessage()==MyDialog.NO) //如果单击了对话框的"no"按钮。58
{ text.append("\n你单击了对话框的No按钮");59
}60
}61
}62
}63
public class Example16_164
{ public static void main(String args[])65
{ new Dwindow("带对话框的窗口");66
}67
}68

69
//例子270
import java.awt.*;import java.awt.event.*;71
public class Example16_272
{ public static void main(String args[])73
{ FWindow f=new FWindow("窗口"); 74
}75
}76
class FWindow extends Frame implements ActionListener77
{ FileDialog filedialog_save,78
filedialog_load;//声明2个文件对话筐79
MenuBar menubar;80
Menu menu;81
MenuItem itemSave,itemLoad;82
TextArea text;83
FWindow(String s)84
{ super(s); 85
setSize(300,400);setVisible(true); 86
text=new TextArea(10,10);87
add(text,"Center"); validate(); 88
menubar=new MenuBar();menu=new Menu("文件"); 89
itemSave=new MenuItem("保存文件"); itemLoad=new MenuItem("打开文件"); 90
itemSave.addActionListener(this); itemLoad.addActionListener(this);91
menu.add(itemSave); menu.add(itemLoad); 92
menubar.add(menu);93
setMenuBar(menubar);94
filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);95
filedialog_save.setVisible(false);96
filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);97
filedialog_load.setVisible(false);98
filedialog_save.addWindowListener(new WindowAdapter()//对话框增加适配器。99
{ public void windowClosing(WindowEvent e)100
{ filedialog_save.setVisible(false);101
}102
});103
filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器。104
{public void windowClosing(WindowEvent e)105
{ filedialog_load.setVisible(false);106
}107
});108
addWindowListener(new WindowAdapter() //窗口增加适配器。109
{public void windowClosing(WindowEvent e)110
{ setVisible(false);System.exit(0);111
}112
});113
}114
public void actionPerformed(ActionEvent e) //实现接口中的方法。115
{ if(e.getSource()==itemSave)116
{ filedialog_save.setVisible(true);117
String name=filedialog_save.getFile();118
if(name!=null)119
{ text.setText("你选择了保存文件,名字是"+name);120
} 121
else122
{ text.setText("没有保存文件");123
}124
}125
else if(e.getSource()==itemLoad)126
{ filedialog_load.setVisible(true);127
String name=filedialog_load.getFile();128
if(name!=null)129
{ text.setText("你选择了打开文件,名字是"+name);130
} 131
else132
{ text.setText("没有打开文件");133
}134
}135
}136
}137

138
//例子3139
import java.awt.event.*;import java.awt.*;140
import javax.swing.JOptionPane;141
class Dwindow extends Frame implements ActionListener142
{ TextField inputNumber;143
TextArea show;144
Dwindow(String s)145
{ super(s);146
inputNumber=new TextField(22); inputNumber.addActionListener(this);147
show=new TextArea(); 148
add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER); 149
setBounds(60,60,300,300); setVisible(true);150
validate();151
addWindowListener(new WindowAdapter()152
{ public void windowClosing(WindowEvent e)153
{ System.exit(0);154
}155
}156
);157
}158
public void actionPerformed(ActionEvent e) 159
{ boolean boo=false;160
if(e.getSource()==inputNumber)161
{ String s=inputNumber.getText();162
char a[]=s.toCharArray();163
for(int i=0;i<a.length;i++)164
{ if(!(Character.isDigit(a[i])))165
boo=true; 166
} 167
if(boo==true) //弹出"警告"消息对话框。168
{ JOptionPane.showMessageDialog(this,"您输入了非法字符","警告对话框",169
JOptionPane.WARNING_MESSAGE);170
inputNumber.setText(null); 171
} 172
else if(boo==false)173
{ int number=Integer.parseInt(s);174
show.append("\n"+number+"平方:"+(number*number));175
}176
}177
}178
}179
public class Example16_3180
{ public static void main(String args[]) 181
{ new Dwindow("带对话框的窗口");182
}183
}184

185
//例子4186
import java.awt.event.*;import java.awt.*;187
import javax.swing.JOptionPane;188
class Dwindow extends Frame implements ActionListener189
{ TextField inputName;190
TextArea save;191
Dwindow(String s)192
{ super(s);193
inputName=new TextField(22);inputName.addActionListener(this);194
save=new TextArea();195
add(inputName,BorderLayout.NORTH); add(save,BorderLayout.CENTER); 196
setBounds(60,60,300,300); setVisible(true);197
validate();198
addWindowListener(new WindowAdapter()199
{ public void windowClosing(WindowEvent e)200
{ System.exit(0);201
}202
}203
);204
}205
public void actionPerformed(ActionEvent e) 206
{ String s=inputName.getText();207
int n=JOptionPane.showConfirmDialog(this,"确认正确吗?","确认对话框",208
JOptionPane.YES_NO_OPTION );209
if(n==JOptionPane.YES_OPTION) 210
{ save.append("\n"+s);211
} 212
else if(n==JOptionPane.NO_OPTION) 213
{ inputName.setText(null);214
} 215
}216
}217
public class Example16_4218
{ public static void main(String args[]) 219
{ new Dwindow("带对话框的窗口");220
}221
}222

223
//例子5224
import java.awt.event.*;225
import java.awt.*;226
import javax.swing.JColorChooser;227
class Dwindow extends Frame implements ActionListener 228
{ Button button;229
Dwindow(String s)230
{ super(s);231
button=new Button("打开颜色对话框"); 232
button.addActionListener(this);233
setLayout(new FlowLayout());234
add(button);235
setBounds(60,60,300,300);236
setVisible(true);237
validate();238
addWindowListener(new WindowAdapter()239
{ public void windowClosing(WindowEvent e)240
{241
System.exit(0);242
}243
}244
);245

246
}247
public void actionPerformed(ActionEvent e)248
{ 249
Color newColor=JColorChooser.showDialog(this,"调色板",button.getBackground());250
button.setBackground(newColor); 251
}252
}253
public class Example16_5254
{ public static void main(String args[])255
{ 256
new Dwindow("带颜色对话框的窗口");257
}258
}259





浙公网安备 33010602011771号