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

2

3

4



5

6

7



8

9

10

11

12

13

14



15



16

17

18

19

20

21



22



23

24

25



26

27

28

29



30

31

32

33



34

35



36

37

38

39

40

41

42

43

44



45



46

47

48

49

50

51



52



53

54

55



56

57

58



59

60

61

62

63

64



65



66

67

68

69

70

71

72



73



74

75

76

77



78

79

80

81

82

83

84



85

86

87

88

89

90

91

92

93

94

95

96

97

98

99



100



101

102

103

104



105



106

107

108

109



110



111

112

113

114

115



116



117

118

119



120

121

122



123

124

125

126



127

128

129



130

131

132



133

134

135

136

137

138

139

140

141

142



143

144

145



146

147

148

149

150

151

152



153



154

155

156

157

158

159



160

161



162

163

164



165

166

167

168



169

170

171

172

173



174

175

176

177

178

179

180



181



182

183

184

185

186

187

188

189



190

191

192



193

194

195

196

197

198

199



200



201

202

203

204

205

206



207

208

209

210



211

212

213



214

215

216

217

218



219



220

221

222

223

224

225

226

227

228



229

230



231

232

233

234

235

236

237

238

239



240



241

242

243

244

245

246

247

248



249

250

251

252

253

254



255



256

257

258

259
