
如果不想出錯請用eclipse進行編譯。至于什么原因,下個文章解釋;
對瀏覽器感興趣的朋友,這是firefox各個版本的程序和源代碼下載FTP地址
ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/?
代碼:
??1
??2
//?????WebReader.java???
??3
??//???This???program???uses???a???JEditorPane???to???display???the???
??4
??//???contents???of???a???file???on???a???Web???server.???
??5
????
??6
??//???Java???core???packages???
??7
??import???java.awt.*;???
??8
??import???java.awt.event.*;???
??9
??import???java.net.*;???
?10
??import???java.io.*;???
?11
??import???java.util.*;???
?12
????
?13
??//???Java???extension???packages???
?14
??import???javax.swing.*;???
?15
??import???javax.swing.event.*;???
?16
????
?17
??public???class???WebReader???extends???JFrame???
{???
?18
??private???JTextField???enterField;???
?19
??private???JEditorPane???contentsArea;???
?20
??private???JButton???backButton,fwdButton;???
?21
??private???JPanel???contralPanel;???
?22
??private???URLList???urllist;???
?23
????????//???set???up???GUI???
?24
????????public???WebReader()???
?25
????????
{???
?26
??????????????super(???"My???Web???Browser"???);???
?27
????
?28
????
?29
??????????????JMenu???fileMenu???=???new???JMenu("File");//set???up???File???menu???ane???its???menu???item???
?30
??????????????fileMenu.setMnemonic('F');???
?31
????
?32
??????????????JMenuItem???exitItem???=???new???JMenuItem("Exit");???
?33
??????????????exitItem.setMnemonic('X');???
?34
??????????????exitItem.addActionListener(???
?35
??????????????new???ActionListener()
{???
?36
??????????????public???void???actionPerformed(ActionEvent???event)
{???
?37
??????????????System.exit(0);???
?38
??????????????}???
?39
??????????????}???
?40
??????????????);???
?41
??????????????fileMenu.add(???exitItem???);???
?42
????
?43
??????????????JMenuBar???bar???=???new???JMenuBar();???
?44
??????????????setJMenuBar(???bar???);???
?45
??????????????bar.add(???fileMenu???);???
?46
????
?47
??????????????JMenu???editMenu???=???new???JMenu("Edit");???
?48
??????????????editMenu.setMnemonic('E');???
?49
????
?50
??????????????JMenuItem???nextItem???=???new???JMenuItem(???"Next"???);???
?51
??????????????nextItem.setMnemonic(???'N'???);???
?52
??????????????nextItem.addActionListener(???
?53
??????????????new???ActionListener()
{???
?54
??????????????public???void???actionPerformed(ActionEvent???event)
{???
?55
??????????????System.exit(0);???
?56
??????????????}???
?57
??????????????}???
?58
??????????????);???
?59
??????????????editMenu.add(???nextItem???);???
?60
??????????????bar.add(???editMenu???);???
?61
????
?62
??????????????Container???container???=???getContentPane();???
?63
????
?64
??????????????FlowLayout???lay=new???FlowLayout();???
?65
??????????????lay.setAlignment(???FlowLayout.LEFT???);???
?66
????
?67
??????????????contralPanel???=???new???JPanel();???
?68
??????????????contralPanel.setLayout(???lay???);???
?69
????
?70
??????????????urllist???=???new???URLList();???
?71
????
?72
??????????????Icon???back???=???new???ImageIcon(???"back.gif"???);???
?73
??????????????backButton???=???new???JButton(???"back",back???);???
?74
??????????????backButton.addActionListener(???
?75
??????????????new???ActionListener()???
?76
??????????????
{???
?77
??????????????public???void???actionPerformed(???ActionEvent???event???)???
?78
??????????????????????????
{???
?79
??????????????????????????String???strback=new???String(???urllist.back()???);???
?80
??????????????????????????System.out.println(???strback???);???
?81
??????????????????????????if(???!strback.equals(???"Noop"???)???)???
?82
??????????????????????????getThePage(???strback???);???
?83
????????????????????????????
?84
??????????????????????????}???
?85
??????????????????}???
?86
??????????????);???
?87
??????contralPanel.add(???backButton???);???
?88
????
?89
??????????????Icon???fwd???=???new???ImageIcon(???"fwd.gif"???);???
?90
??????????????fwdButton???=???new???JButton(???"fwd",fwd???);???
?91
??????????????fwdButton.addActionListener(???
?92
??????????????new???ActionListener()???
?93
??????????????
{???
?94
??????????????public???void???actionPerformed(???ActionEvent???event???)???
?95
??????????????????????????
{???
?96
??????????????????????????String???strfwd=new???String(???urllist.forward()???);???
?97
??????????????????????????System.out.println(???strfwd???);???
?98
??????????????????????????if(???!strfwd.equals(???"Noop"???)???)???
?99
??????????????????????????getThePage(???strfwd???);???
100
????????????????????????????
101
??????????????????????????}???
102
??????????????????}???
103
??????????????);???
104
??????????????contralPanel.add(???fwdButton???);???
105
????
106
??????????????//???create???enterField???and???register???its???listener???
107
??????????????enterField???=???new???JTextField(???"Enter???file???URL???here",24???);???
108
????
109
??????????????enterField.addActionListener(???
110
????
111
????????????????????new???ActionListener()???
{???
112
????
113
??????????????????????????//???get???document???specified???by???user???
114
??????????????????????????public???void???actionPerformed(???ActionEvent???event???)???
115
??????????????????????????
{???
116
??????????????????????????urllist.push(???event.getActionCommand()???);???
117
??????????????????????????getThePage(???event.getActionCommand()???);???
118
??????????????????????????}???
119
????
120
????????????????????}?????//???end???anonymous???inner???class???
121
????
122
??????????????);???//???end???call???to???addActionListener???
123
????
124
??????????????enterField.addMouseListener(???
125
??????????????new???MouseListener()
{???
126
??????????????public???void???mouseClicked(???MouseEvent???e???)???
127
??????????????
{???
128
??????????????enterField.selectAll();???
129
??????????????}???
130
????
131
??????????????public???void???mousePressed(???MouseEvent???e???)???
132
??????????????
{???
133
??????????????}???
134
????
135
??????????????public???void???mouseReleased(???MouseEvent???e???)???
136
??????????????
{???
137
??????????????}???
138
??????????????public???void???mouseEntered(???MouseEvent???e???)???
139
??????????????
{???
140
??????????????}???
141
????????????????
142
????????????????
143
??????????????public???void???mouseExited(???MouseEvent???e???)???
144
??????????????
{???
145
??????????????}???
146
??????????????}???
147
??????????????);???
148
????
149
??????????????contralPanel.add(???enterField???);???
150
????
151
??????????????container.add(???contralPanel,???BorderLayout.NORTH???);???
152
??????????????//???create???contentsArea???and???register???HyperlinkEvent???listener???
153
??????????????contentsArea???=???new???JEditorPane();???
154
??????????????contentsArea.setEditable(???false???);???
155
????
156
??????????????contentsArea.addHyperlinkListener(???
157
????
158
????????????????????new???HyperlinkListener()???
{???
159
????
160
??????????????????????????//???if???user???clicked???hyperlink,???go???to???specified???page???
161
??????????????????????????public???void???hyperlinkUpdate(???HyperlinkEvent???event???)???
162
??????????????????????????
{???
163
????????????????????????????????if???(???event.getEventType()???==???
164
??????????????????????????????????????????HyperlinkEvent.EventType.ACTIVATED???)???
165
??????????????????????????????????????????
{???
166
??????????????????????????????????????????urllist.push(???event.getURL().toString()???);???
167
??????????????????????????????????????????getThePage(???event.getURL().toString()???);???
168
??????????????????????????????????????????}???
169
??????????????????????????}???
170
????
171
????????????????????}?????//???end???anonymous???inner???class???
172
????
173
??????????????);???//???end???call???to???addHyperlinkListener???
174
????
175
??????????????container.add(?????new???JScrollPane(???contentsArea???)???,???
176
????????????????????BorderLayout.CENTER???);???
177
????
178
??????????????setSize(???1000,???700???);???
179
??????????????setVisible(???true???);???
180
????????}???
181
????
182
????????//???load???document;???change???mouse???cursor???to???indicate???status???
183
????????private???void???getThePage(???String???location???)???
184
????????
{???
185
??????????????//???change???mouse???cursor???to???WAIT_CURSOR???
186
??????????????setCursor(???Cursor.getPredefinedCursor(???
187
????????????????????Cursor.WAIT_CURSOR???)???);???
188
????
189
??????????????if(!location.startsWith(???"http://"???))???
190
??????????????location="http://"???+???location;???
191
????
192
??????????????//???load???document???into???contentsArea???and???display???location???in???
193
??????????????//???enterField???
194
??????????????try???
{???
195
????????????????????contentsArea.setPage(???location???);???
196
????????????????????enterField.setText(???location???);???
197
??????????????}???
198
????
199
??????????????//???process???problems???loading???document???
200
??????????????catch???(???IOException???ioException???)???
{???
201
????????????????????JOptionPane.showMessageDialog(???this,???
202
??????????????????????????"Error???retrieving???specified???URL",???
203
??????????????????????????"Bad???URL",???JOptionPane.ERROR_MESSAGE???);???
204
??????????????}???
205
????
206
??????????????setCursor(???Cursor.getPredefinedCursor(???
207
????????????????????Cursor.DEFAULT_CURSOR???)???);???
208
????????}???
209
????
210
????????//???begin???application???execution???
211
????????public???static???void???main(???String???args[]???)???
212
????????
{???
213
??????????????WebReader???application???=???new???WebReader();???
214
????
215
??????????????application.setDefaultCloseOperation(???
216
????????????????????JFrame.EXIT_ON_CLOSE???);???
217
????
218
????????}???
219
????
220
??}?????//???end???class???WebReader???
221
??class???URLList???
222
??
{???
223
??private???int???now???=???0;???
224
??private???Vector???vector;???
225
????
226
??URLList()???
227
??
{???
228
??vector???=???new???Vector(???1???);???
229
??}???
230
??public???void???push(String???url)???
231
??
{???
232
??vector.add(url);???
233
??now???=???vector.size()-1;???
234
??}???
235
????
236
??public???String???back()???
237
??
{???
238
??System.out.println(???"back?????"???+???now???);???
239
??if(???now???==???0???)???
240
??return(???"Noop"???);???
241
??else???
242
??
{???
243
??now--;???
244
??System.out.println(???"back----"???+???now???);???
245
??return(???(String)vector.get(???now???));???
246
??}???
247
??}???
248
????
249
??public???String???forward()???
250
??
{???
251
??System.out.println(???"fwd"???+???now???+???vector.size()???);???
252
??if(???(???vector.size()???==???0???)???||???(???now???==???(???vector.size()-1???)???)???)???
253
??return(???"Noop"???);???
254
??else???
255
??
{???
256
??now++;???
257
??System.out.println(???"fwd+++"???+???now???);???
258
??return(???(String)vector.get(???now???));???
259
??}???
260
??}???
261
??public???int???getnow()???
262
??
{???
263
??return???now;???
264
??}???
265
??}?

??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

260

261

262



263

264

265

地震讓大伙知道:居安思危,才是生存之道。
