littlefermat

用netbean开发简单的网页浏览器

参考《java课程设计—案例精编》清华大学出版社
1.WebBrowser.java主类

  1
  2import java.io.BufferedInputStream;
  3import java.io.BufferedReader;
  4import java.io.BufferedWriter;
  5import java.io.File;
  6import java.io.FileWriter;
  7import java.io.IOException;
  8import java.io.InputStream;
  9import java.io.InputStreamReader;
 10import java.net.URL;
 11import java.util.ArrayList;
 12import java.util.logging.Level;
 13import java.util.logging.Logger;
 14import javax.swing.JFileChooser;
 15import javax.swing.JFrame;
 16import javax.swing.JOptionPane;
 17
 18/*
 19 * WebBrowser.java
 20 *
 21 * Created on 2008年4月12日, 下午3:47
 22 */

 23import javax.swing.event.HyperlinkEvent;
 24
 25
 26
 27/**
 28 *
 29 * @author  wangliang
 30 */

 31public class WebBrowser extends javax.swing.JFrame {
 32    private ArrayList history;
 33    private int historyIndex;
 34    String htmlSource;
 35
 36    
 37    /** Creates new form WebBrowser */
 38    public WebBrowser() {
 39        setTitle("网页浏览器");
 40        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 41        initComponents();
 42        history=new ArrayList();
 43        htmlSource="";
 44        
 45    }

 46    
 47    /** This method is called from within the constructor to
 48     * initialize the form.
 49     * WARNING: Do NOT modify this code. The content of this method is
 50     * always regenerated by the Form Editor.
 51     */

 52    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 53    private void initComponents() {
 54
 55        jToolBar1 = new javax.swing.JToolBar();
 56        jButton1 = new javax.swing.JButton();
 57        jButton2 = new javax.swing.JButton();
 58        jButton3 = new javax.swing.JButton();
 59        jButton4 = new javax.swing.JButton();
 60        jButton5 = new javax.swing.JButton();
 61        jLabel1 = new javax.swing.JLabel();
 62        jTextField1 = new javax.swing.JTextField();
 63        jButton6 = new javax.swing.JButton();
 64        jScrollPane1 = new javax.swing.JScrollPane();
 65        jEditorPane1 = new javax.swing.JEditorPane();
 66        jMenuBar1 = new javax.swing.JMenuBar();
 67        jMenu1 = new javax.swing.JMenu();
 68        jMenuItem1 = new javax.swing.JMenuItem();
 69        jMenuItem7 = new javax.swing.JMenuItem();
 70        jMenu2 = new javax.swing.JMenu();
 71        jMenuItem2 = new javax.swing.JMenuItem();
 72        jMenuItem3 = new javax.swing.JMenuItem();
 73        jMenu3 = new javax.swing.JMenu();
 74        jMenuItem5 = new javax.swing.JMenuItem();
 75        jMenuItem6 = new javax.swing.JMenuItem();
 76
 77        jToolBar1.setRollover(true);
 78
 79        jButton1.setText("另存为");
 80        jButton1.setFocusable(false);
 81        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
 82        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
 83        jButton1.addActionListener(new java.awt.event.ActionListener() {
 84            public void actionPerformed(java.awt.event.ActionEvent evt) {
 85                jButton1ActionPerformed(evt);
 86            }

 87        }
);
 88        jToolBar1.add(jButton1);
 89
 90        jButton2.setText("后退");
 91        jButton2.setFocusable(false);
 92        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
 93        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
 94        jButton2.addActionListener(new java.awt.event.ActionListener() {
 95            public void actionPerformed(java.awt.event.ActionEvent evt) {
 96                jButton2ActionPerformed(evt);
 97            }

 98        }
);
 99        jToolBar1.add(jButton2);
100
101        jButton3.setText("前进");
102        jButton3.setFocusable(false);
103        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
104        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
105        jButton3.addActionListener(new java.awt.event.ActionListener() {
106            public void actionPerformed(java.awt.event.ActionEvent evt) {
107                jButton3ActionPerformed(evt);
108            }

109        }
);
110        jToolBar1.add(jButton3);
111
112        jButton4.setText("查看源代码");
113        jButton4.setFocusable(false);
114        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
115        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
116        jButton4.addActionListener(new java.awt.event.ActionListener() {
117            public void actionPerformed(java.awt.event.ActionEvent evt) {
118                jButton4ActionPerformed(evt);
119            }

120        }
);
121        jToolBar1.add(jButton4);
122
123        jButton5.setText("退出");
124        jButton5.setFocusable(false);
125        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
126        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
127        jButton5.addActionListener(new java.awt.event.ActionListener() {
128            public void actionPerformed(java.awt.event.ActionEvent evt) {
129                jButton5ActionPerformed(evt);
130            }

131        }
);
132        jToolBar1.add(jButton5);
133
134        jLabel1.setText("地址");
135
136        jTextField1.addActionListener(new java.awt.event.ActionListener() {
137            public void actionPerformed(java.awt.event.ActionEvent evt) {
138                jTextField1ActionPerformed(evt);
139            }

140        }
);
141
142        jButton6.setText("转到");
143        jButton6.addActionListener(new java.awt.event.ActionListener() {
144            public void actionPerformed(java.awt.event.ActionEvent evt) {
145                jButton6ActionPerformed(evt);
146            }

147        }
);
148
149        jEditorPane1.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
150            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
151                jEditorPane1HyperlinkUpdate(evt);
152            }

153        }
);
154        jScrollPane1.setViewportView(jEditorPane1);
155
156        jMenu1.setText("文件(F)");
157        jMenu1.addActionListener(new java.awt.event.ActionListener() {
158            public void actionPerformed(java.awt.event.ActionEvent evt) {
159                jMenu1ActionPerformed(evt);
160            }

161        }
);
162
163        jMenuItem1.setText("另存为");
164        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
165            public void actionPerformed(java.awt.event.ActionEvent evt) {
166                jMenuItem1ActionPerformed(evt);
167            }

168        }
);
169        jMenu1.add(jMenuItem1);
170
171        jMenuItem7.setText("退出");
172        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
173            public void actionPerformed(java.awt.event.ActionEvent evt) {
174                jMenuItem7ActionPerformed(evt);
175            }

176        }
);
177        jMenu1.add(jMenuItem7);
178
179        jMenuBar1.add(jMenu1);
180
181        jMenu2.setText("编辑(E)");
182
183        jMenuItem2.setText("后退");
184        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
185            public void actionPerformed(java.awt.event.ActionEvent evt) {
186                jMenuItem2ActionPerformed(evt);
187            }

188        }
);
189        jMenu2.add(jMenuItem2);
190
191        jMenuItem3.setText("前进");
192        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
193            public void actionPerformed(java.awt.event.ActionEvent evt) {
194                jMenuItem3ActionPerformed(evt);
195            }

196        }
);
197        jMenu2.add(jMenuItem3);
198
199        jMenuBar1.add(jMenu2);
200
201        jMenu3.setText("视图(V)");
202
203        jMenuItem5.setText("查看源码");
204        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
205            public void actionPerformed(java.awt.event.ActionEvent evt) {
206                jMenuItem5ActionPerformed(evt);
207            }

208        }
);
209        jMenu3.add(jMenuItem5);
210
211        jMenuItem6.setText("刷新");
212        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
213            public void actionPerformed(java.awt.event.ActionEvent evt) {
214                jMenuItem6ActionPerformed(evt);
215            }

216        }
);
217        jMenu3.add(jMenuItem6);
218
219        jMenuBar1.add(jMenu3);
220
221        setJMenuBar(jMenuBar1);
222
223        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
224        getContentPane().setLayout(layout);
225        layout.setHorizontalGroup(
226            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
227            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
228            .addGroup(layout.createSequentialGroup()
229                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
230                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
231                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 537, javax.swing.GroupLayout.PREFERRED_SIZE)
232                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
233                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
234                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
235            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
236        );
237        layout.setVerticalGroup(
238            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239            .addGroup(layout.createSequentialGroup()
240                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
241                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
242                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
243                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
244                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
245                        .addComponent(jButton6))
246                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
247                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
248                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
249        );
250
251        pack();
252    }
// </editor-fold>//GEN-END:initComponents
253
254    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
255        String url=jTextField1.getText();
256        if(url.length()>0&&!url.startsWith("http://")){
257            url="http://"+url;
258        }

259        if(!url.equals("")){
260            saveFile(url);
261        }

262        else {
263            JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
264        }

265    }
//GEN-LAST:event_jMenuItem1ActionPerformed
266
267    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
268        // TODO add your handling code here:
269    }
//GEN-LAST:event_jMenu1ActionPerformed
270
271    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
272        System.exit(0);
273    }
//GEN-LAST:event_jMenuItem7ActionPerformed
274
275    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
276        historyIndex--;
277        if(historyIndex<0){
278            historyIndex=0;
279        }

280        String url=jTextField1.getText();
281        try{
282            url=(String)history.get(historyIndex);
283            jEditorPane1.setPage(url);
284            jTextField1.setText(url.toString());
285            jEditorPane1.revalidate();          
286        }

287        catch(Exception ex){
288            
289        }

290    }
//GEN-LAST:event_jMenuItem2ActionPerformed
291
292    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
293        historyIndex++;
294        if(historyIndex>=history.size()){
295            historyIndex=history.size()-1;
296        }

297        String url=jTextField1.getText();
298        try{
299            url=(String)history.get(historyIndex);
300            jEditorPane1.setPage(url);
301            jTextField1.setText(url.toString());
302            jEditorPane1.revalidate();          
303        }

304        catch(Exception ex){
305            
306        }

307    }
//GEN-LAST:event_jMenuItem3ActionPerformed
308
309    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
310        // TODO add your handling code here:
311        String url="";
312        url=jTextField1.getText().trim();
313        if(url.length()>0&&!url.startsWith("http://")){
314            url="http://"+url;
315        }

316        if(url!=""){
317            getHtmlSource(url);
318            ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
319            vsframe.setBounds(00800500);
320            vsframe.setVisible(true);
321        }

322        else {
323            JOptionPane.showMessageDialog(WebBrowser.this"请输入网址","网页浏览器",JOptionPane.ERROR_MESSAGE);
324        }

325    }
//GEN-LAST:event_jMenuItem5ActionPerformed
326
327    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
328        // TODO add your handling code here:
329        String url="";
330        url=jTextField1.getText().trim();
331        if(url.length()>0&&!url.startsWith("http://")){
332            url="http://"+url;
333        }

334        if(url!=""){
335            try {
336                jEditorPane1.setPage(url);
337                jEditorPane1.setEditable(false);
338                jEditorPane1.revalidate();
339            }
 catch (IOException ex) {
340                Logger.getLogger(WebBrowser.class.getName()).log(Level.SEVERE, null, ex);
341            }

342            
343        }

344    }
//GEN-LAST:event_jMenuItem6ActionPerformed
345
346    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
347        String url=jTextField1.getText();
348        if(url.length()>0&&!url.startsWith("http://")){
349            url="http://"+url;
350        }

351        if(!url.equals("")){
352            saveFile(url);
353        }

354        else {
355            JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
356        }

357        
358    }
//GEN-LAST:event_jButton1ActionPerformed
359
360    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
361        historyIndex--;
362        if(historyIndex<0){
363            historyIndex=0;
364        }

365        String url=jTextField1.getText();
366        try{
367            url=(String)history.get(historyIndex);
368            jEditorPane1.setPage(url);
369            jTextField1.setText(url.toString());
370            jEditorPane1.revalidate();          
371        }

372        catch(Exception ex){
373            
374        }

375    }
//GEN-LAST:event_jButton2ActionPerformed
376
377    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
378        historyIndex++;
379        if(historyIndex>=history.size()){
380            historyIndex=history.size()-1;
381        }

382        String url=jTextField1.getText();
383        try{
384            url=(String)history.get(historyIndex);
385            jEditorPane1.setPage(url);
386            jTextField1.setText(url.toString());
387            jEditorPane1.revalidate();          
388        }

389        catch(Exception ex){
390            
391        }

392    }
//GEN-LAST:event_jButton3ActionPerformed
393
394    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
395        // TODO add your handling code here:
396        String url="";
397        url=jTextField1.getText().trim();
398        if(url.length()>0&&!url.startsWith("http://")){
399            url="http://"+url;
400        }

401        if(url!=""){
402            getHtmlSource(url);
403            ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
404            vsframe.setBounds(00800500);
405            vsframe.setVisible(true);
406        }

407        else {
408            JOptionPane.showMessageDialog(WebBrowser.this"请输入网址","网页浏览器",JOptionPane.ERROR_MESSAGE);
409        }

410    }
//GEN-LAST:event_jButton4ActionPerformed
411
412    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
413        System.exit(0);
414    }
//GEN-LAST:event_jButton5ActionPerformed
415
416    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
417        String url="";
418        url=jTextField1.getText().trim();
419        if(url.length()>0&&url.startsWith("http://")){
420            try{
421                jEditorPane1.setPage(url);
422                history.add(url);
423                historyIndex=history.size()-1;
424                jEditorPane1.setEditable(false);
425                jEditorPane1.revalidate();
426            }

427            catch(Exception ex){
428                JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
429            }

430        }

431        else if(url.length()>0&&!url.startsWith("http://")){
432            url="http://"+url;
433            try{
434                jEditorPane1.setPage(url);
435                history.add(url);
436                historyIndex=history.size()-1;
437                jEditorPane1.setEditable(false);
438                jEditorPane1.revalidate();
439            }

440            catch(Exception ex){
441                JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
442            }

443        }

444        else{
445             JOptionPane.showMessageDialog(WebBrowser.this"请输入网址","网页浏览器",JOptionPane.ERROR_MESSAGE);
446        }

447
448    }
//GEN-LAST:event_jTextField1ActionPerformed
449
450    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
451        String url="";
452        url=jTextField1.getText();
453        if(url.length()>0&&url.startsWith("http://")){
454            
455            try{
456                jEditorPane1.setPage(url);
457                history.add(url);
458                historyIndex=history.size()-1;
459                jEditorPane1.setEditable(false);
460                jEditorPane1.revalidate();
461            }

462            catch(Exception ex){
463                JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
464            }

465        }

466        else if(url.length()>0&&!url.startsWith("http://")){
467            url="http://"+url;
468            try{
469                jEditorPane1.setPage(url);
470                history.add(url);
471                historyIndex=history.size()-1;
472                jEditorPane1.setEditable(false);
473                jEditorPane1.revalidate();
474            }

475            catch(Exception ex){
476                JOptionPane.showMessageDialog(WebBrowser.this"无法打开该搜索网页","网页浏览器",JOptionPane.ERROR_MESSAGE);
477            }

478        }

479        else{
480             JOptionPane.showMessageDialog(WebBrowser.this"请输入网址","网页浏览器",JOptionPane.ERROR_MESSAGE);
481        }

482    }
//GEN-LAST:event_jButton6ActionPerformed
483
484    private void jEditorPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPane1HyperlinkUpdate
485        try {
486
487            if(evt.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
488                String url=evt.getURL().toString();
489                jEditorPane1.setPage(url);
490                jTextField1.setText(url);
491                history.add(url);
492                historyIndex=history.size()-1;
493                jEditorPane1.setEditable(false);
494                jEditorPane1.revalidate();
495                
496            }

497            
498        }
 catch (IOException ex) {
499            Logger.getLogger(WebBrowser.class.getName()).log(Level.SEVERE, null, ex);
500        }

501 
502    }
//GEN-LAST:event_jEditorPane1HyperlinkUpdate
503    
504    void getHtmlSource(String url){
505        String linesep,htmlLine;
506        linesep=System.getProperty("line.separator");
507        try{
508            URL source=new URL(url);
509            InputStream in=new BufferedInputStream(source.openStream());
510            BufferedReader br=new BufferedReader(new InputStreamReader(in));
511            while((htmlLine=br.readLine())!=null){
512                htmlSource=htmlSource+htmlLine+linesep;
513            }

514        }

515        catch(java.net.MalformedURLException muex){
516                JOptionPane.showMessageDialog(WebBrowser.this, muex.toString(),"网页浏览器",JOptionPane.INFORMATION_MESSAGE);
517            }

518        catch(Exception ex){
519                JOptionPane.showMessageDialog(WebBrowser.this, ex.toString(),"网页浏览器",JOptionPane.INFORMATION_MESSAGE);
520            }

521    }

522    
523    void saveFile(final String url){
524        final String linesep=System.getProperty("line.separator");
525        final JFileChooser chooser=new JFileChooser();
526        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
527        chooser.setDialogTitle("另存为");
528        if(chooser.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
529            return;
530        this.repaint();
531        Thread thread=new Thread(){
532            public void run(){
533                 try{
534                java.net.URL source=new URL(url);
535                InputStream in=new BufferedInputStream(source.openStream());
536                BufferedReader br=new BufferedReader(new InputStreamReader(in));
537                File fileName=chooser.getSelectedFile();
538                FileWriter out=new FileWriter(fileName);
539                BufferedWriter bw=new BufferedWriter(out);
540                String line;
541                while((line=br.readLine())!=null){
542                    bw.write(line);
543                    bw.newLine();
544                }

545                bw.flush();
546                bw.close();
547                String dMessage=url+"已经被保存"+linesep+fileName.getAbsolutePath();
548                String dTitle="另存为";
549                JOptionPane.showMessageDialog(WebBrowser.this, dMessage,dTitle,JOptionPane.INFORMATION_MESSAGE);
550            }

551            catch(java.net.MalformedURLException muex){
552                JOptionPane.showMessageDialog(WebBrowser.this, muex.toString(),"网页浏览器",JOptionPane.INFORMATION_MESSAGE);
553            }

554            catch(Exception ex){
555                JOptionPane.showMessageDialog(WebBrowser.this, ex.toString(),"网页浏览器",JOptionPane.INFORMATION_MESSAGE);
556            }

557            }

558           
559        }
;
560        thread.start();
561    }

562   
563    
564    /**
565     * @param args the command line arguments
566     */

567    public static void main(String args[]) {
568        java.awt.EventQueue.invokeLater(new Runnable() {
569            public void run() {
570                new WebBrowser().setVisible(true);
571            }

572        }
);
573    }

574    
575    // Variables declaration - do not modify//GEN-BEGIN:variables
576    private javax.swing.JButton jButton1;
577    private javax.swing.JButton jButton2;
578    private javax.swing.JButton jButton3;
579    private javax.swing.JButton jButton4;
580    private javax.swing.JButton jButton5;
581    private javax.swing.JButton jButton6;
582    private javax.swing.JEditorPane jEditorPane1;
583    private javax.swing.JLabel jLabel1;
584    private javax.swing.JMenu jMenu1;
585    private javax.swing.JMenu jMenu2;
586    private javax.swing.JMenu jMenu3;
587    private javax.swing.JMenuBar jMenuBar1;
588    private javax.swing.JMenuItem jMenuItem1;
589    private javax.swing.JMenuItem jMenuItem2;
590    private javax.swing.JMenuItem jMenuItem3;
591    private javax.swing.JMenuItem jMenuItem5;
592    private javax.swing.JMenuItem jMenuItem6;
593    private javax.swing.JMenuItem jMenuItem7;
594    private javax.swing.JScrollPane jScrollPane1;
595    private javax.swing.JTextField jTextField1;
596    private javax.swing.JToolBar jToolBar1;
597    // End of variables declaration//GEN-END:variables
598    
599}

600
2.ViewSourceFrame.java显示源代码

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;

/*
 * ViewSourceFrame.java
 *
 * Created on 2008年4月12日, 下午10:25
 
*/

import javax.swing.JOptionPane;



/**
 *
 * 
@author  wangliang
 
*/

public class ViewSourceFrame extends javax.swing.JFrame {
    String htmlSource;
    
/** Creates new form ViewSourceFrame */
    
public ViewSourceFrame(String htmlSource) {
        setTitle(
"源代码");
        
this.htmlSource=htmlSource;
        initComponents();
        jTextArea1.setEditable(
true);
        jTextArea1.setText(htmlSource);
        jTextArea1.setCaretPosition(
0);
    }

    
    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     
*/

    
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 
= new javax.swing.JScrollPane();
        jTextArea1 
= new javax.swing.JTextArea();
        jButton1 
= new javax.swing.JButton();
        jButton2 
= new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(
20);
        jTextArea1.setRows(
5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText(
"保存");
        jButton1.addActionListener(
new java.awt.event.ActionListener() {
            
public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }

        }
);

        jButton2.setText(
"退出");
        jButton2.addActionListener(
new java.awt.event.ActionListener() {
            
public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }

        }
);

        javax.swing.GroupLayout layout 
= new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 
713, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(
265265265)
                .addComponent(jButton1)
                .addGap(
585858)
                .addComponent(jButton2)
                .addContainerGap(
272, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 
421, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)))
        );

        pack();
    }
// </editor-fold>//GEN-END:initComponents

    
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }
//GEN-LAST:event_jButton2ActionPerformed

    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fc=new JFileChooser();
        
int returnVal=fc.showSaveDialog(ViewSourceFrame.this);
        
if(fc.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
            
return;
        File saveFile
=fc.getSelectedFile();
        
try{
            FileWriter writeOut
=new FileWriter(saveFile);
            writeOut.write(jTextArea1.getText());
            writeOut.close();
        }

        
catch(IOException ex){
            JOptionPane.showMessageDialog(
this, ex.toString(),"网页代码",JOptionPane.INFORMATION_MESSAGE);
        }

    }
//GEN-LAST:event_jButton1ActionPerformed
    

    
    
// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    
private javax.swing.JButton jButton2;
    
private javax.swing.JScrollPane jScrollPane1;
    
private javax.swing.JTextArea jTextArea1;
    
// End of variables declaration//GEN-END:variables
    
}

可运行的jar文件/Files/littlefermat/webBrowser.rar

posted on 2008-04-13 00:43 littlefermat 阅读(771) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: