java网页浏览器设计 第2页

java网页浏览器设计 第2页
        //后退
        else if (e.getSource() == backItem ||e.getSource() == picBack){
            historyIndex--;
            if(historyIndex < 0)
                historyIndex = 0;
            url = jurl.getText();
            try{
                //获得history对象中本地址之前访问的地址
                url = (String)history.get(historyIndex);
                jEditorPane1.setPage(url);
                jurl.setText(url.toString());
  jEditorPane1.setEditable(false); //add by copy editor :)
                jEditorPane1.revalidate ();
            }
            catch(Exception ex){
            }
        }
        //前进
        else if (e.getSource() == forwardItem ||e.getSource() == picForward){
            historyIndex++;
            if(historyIndex >= history.size())
                    historyIndex = history.size()-1;
            url = jurl.getText();
            try{
                //获得history对象中本地址之后访问的地址
                url = (String)history.get(historyIndex);
                jEditorPane1.setPage(url);
                jurl.setText(url.toString());
  jEditorPane1.setEditable(false); //add by copy editor :)
                jEditorPane1.revalidate ();
            }
            catch(Exception ex){
            }
        }
        //全屏
        else if (e.getSource() == fullscreenItem){
            boolean add_button2=true;
            //获得屏幕大小
            Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

            Container content = window.getContentPane();
            content.add (bar,"North");
            content.add (scrollPane,"Center");
               
            //button2为点击“全屏”后的还原按钮
            if(add_button2==true) {
                bar.add (button2);
            }
            //为button2添加事件
            button2.addActionListener (new ActionListener () {
                public void actionPerformed (ActionEvent evt) {
                    WebBrowser.this.setEnabled (true);
                    window.remove (bar);
                    window.remove (toolBar);
                    window.remove (scrollPane);
                    window.setVisible (false);
                   
                    scrollPane.setPreferredSize (new Dimension (100,500));
                    getContentPane ().add (scrollPane,BorderLayout.SOUTH);
                    getContentPane ().add (bar,BorderLayout.CENTER);
                    getContentPane ().add (toolBar,BorderLayout.NORTH);
                    bar.remove (button2);
                    pack();
                }
            });
            window.setSize (size);
            window.setVisible (true);
        }
        //查看源文件
        else if (e.getSource() == sourceItem ||e.getSource() == picView){
            url = jurl.getText ().toString ().trim ();
                if(url.length ()>0&&!url.startsWith ("http://")) {
                    url="http://"+url;
                }
                if( !url.equals ("")) {
                    //根据url,获得源代码
                    getHtmlSource (url);
                    //生成显示源代码的框架对象
                    ViewSourceFrame vsframe = new ViewSourceFrame (htmlSource);
                    vsframe.setBounds (0,0,800,500);
                    vsframe.setVisible(true);
                }
                else {
                    JOptionPane.showMessageDialog (WebBrowser.this,"请输入链接地址","网页浏览器",JOptionPane.ERROR_MESSAGE);
                }
        }
        //刷新
        else if (e.getSource() == reloadItem){
            url=jurl.getText ();
            if(url.length ()>0&&url.startsWith ("http://")) {
                try {
                    jEditorPane1.setPage (url);
      jEditorPane1.setEditable(false); //add by copy editor :)
                    jEditorPane1.revalidate ();
                }
                catch(Exception ex) {
                }
            }
            else if(url.length ()>0&&!url.startsWith ("http://")) {
                url="http://"+url;
                try {
                    jEditorPane1.setPage (url );
      jEditorPane1.setEditable(false); //add by copy editor :)
                    jEditorPane1.revalidate ();
                }
                catch(Exception ex) {
                }
            }
        }
    }
   
    /*
    **保存文件
    */
    void saveFile (final String url) {
        final String linesep = System.getProperty ("line.separator");
        chooser1.setCurrentDirectory (new File ("."));
        chooser1.setDialogType (JFileChooser.SAVE_DIALOG);
        chooser1.setDialogTitle ("另存为...");
        if(chooser1.showSaveDialog (this) != JFileChooser.APPROVE_OPTION)
            return;
        this.repaint ();
        Thread thread = new Thread () {
            public void run () {
                try {
                    java.net.URL source = new URL (url);
                    InputStream in = new BufferedInputStream (source.openStream ());
                    BufferedReader br=new BufferedReader (new InputStreamReader (in));
                    File fileName = chooser1.getSelectedFile ();
                    FileWriter out = new FileWriter (fileName);
                    BufferedWriter bw = new BufferedWriter (out);
                    String line;
                    while((line = br.readLine ()) != null) {
                        bw.write (line);
                        bw.newLine ();
                    }
                    bw.flush ();
                    bw.close ();
                    out.close ();
                    String dMessage = url + " 已经被保存至"+ linesep +fileName.getAbsolutePath ();
                    String dTitle = "另存为";
                    int dType = JOptionPane.INFORMATION_MESSAGE;
                    JOptionPane.showMessageDialog ((Component) null,dMessage,dTitle,dType);
                }
                catch(java.net.MalformedURLException muex) {
                    JOptionPane.showMessageDialog ((Component)null,muex.toString (),"网页浏览器",JOptionPane.ERROR_MESSAGE);
                }
                catch(Exception ex) {
                    JOptionPane.showMessageDialog ((Component) null,ex.toString (),"网页浏览器",JOptionPane.ERROR_MESSAGE);
                }
            }
        };
        thread.start ();
    }

    /*
    **获得源代码
    */
    void getHtmlSource (String url) {
        String linesep,htmlLine;
        linesep = System.getProperty ("line.separator");
        htmlSource ="";
        try {
            java.net.URL source = new URL (url);
            InputStream in = new BufferedInputStream (source.openStream ());
            BufferedReader br = new BufferedReader ( new InputStreamReader (in));
            while((htmlLine = br.readLine ())!=null) {
                htmlSource = htmlSource +htmlLine+linesep;
            }
        }
        catch(java.net.MalformedURLException muex) {
            JOptionPane.showMessageDialog (WebBrowser.this,muex.toString (),"网页浏览器",JOptionPane.ERROR_MESSAGE);
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog (WebBrowser.this,e.toString (),"网页浏览器",JOptionPane.ERROR_MESSAGE);
        }
    }
   
    /**
    **实现监X听*C器接口的hyperlinkUpdate函数
    */
    public void hyperlinkUpdate (HyperlinkEvent e) {
    /*    if(e.getEventType () == HyperlinkEvent.EventType.ACTIVATED) {
            String url = jurl.getText();
            if(url.length ()>0&&url.startsWith ("http://")) {
                try {
                    jEditorPane1.setPage (url);
                    jEditorPane1.revalidate ();
                }
                catch(Exception ex) {
                }
            }
            else if(url.length ()>0&&!url.startsWith ("http://")) {
                url="http://www.751com.cn"+url;
                try {
                    jEditorPane1.setPage (url );
                    jEditorPane1.revalidate ();
                }
                catch(Exception ex) {
                }
            }
        }
    */ 751com.cn
    // Revised by copy editor :) Now with new code to help hyperlink
 try {
   if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    jEditorPane1.setPage(e.getURL());
   } catch (Exception ex) {
    ex.printStackTrace(System.err);
   }

        }
        catch(Exception e){
        }

        WebBrowser webBrowser = new WebBrowser ();
        webBrowser.pack();
        webBrowser.setVisible(true);
    }
}/*
**源代码框架
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import java.io.*;
import java.util.*;

class ViewSourceFrame extends JFrame implements ActionListener{
    JPanel contentPane;
    JPanel panel1 = new JPanel ();
    JPanel panel2 = new JPanel ();
    Border border1;

    JButton closebutton = new JButton ();
    JButton savebutton = new JButton ();
    JScrollPane jScrollPanel = new JScrollPane ();
    JTextArea jTextArea1 = new JTextArea ();

    String htmlSource;

    /**
 **构造函数,初始化图形用户界面
 */
 public ViewSourceFrame (String htmlSource) {
       
        this.htmlSource = htmlSource;
        enableEvents (AWTEvent.WINDOW_EVENT_MASK);
        setSize (new Dimension (600,500));
        setTitle ("源代码");
        setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
       
        contentPane = (JPanel)getContentPane ();
        contentPane.setLayout (new BorderLayout());
       
        panel2.setLayout (new FlowLayout());
       
        savebutton.setText ("保存");
        closebutton.setText ("退出");
       
  closebutton.addActionListener(this);
        savebutton.addActionListener(this);
       
        jScrollPanel.getViewport ().add (jTextArea1,null);
        border1 = BorderFactory.createEmptyBorder (4,4,4,4);
        panel1.setLayout (new BorderLayout());
        panel1.setBorder (border1);
        panel1.add (jScrollPanel,BorderLayout.CENTER);
        contentPane.add (panel1,BorderLayout.CENTER);
       
        panel2.add (savebutton);
        panel2.add (closebutton);
       
  contentPane.add (panel2,BorderLayout.SOUTH);
        this.jTextArea1.setEditable (true);
        this.jTextArea1.setText (this.htmlSource);
        //设置光标的位置,将其移动到文本区第0个字符
  this.jTextArea1.setCaretPosition (0);
    }
   
    /**
 **实现监X听*X器接口的actionPerformed函数
 */
 public void actionPerformed(ActionEvent e) {
        String url = "";
        if (e.getSource() == closebutton){
            dispose();
        }
        else if(e.getSource() == savebutton){
            JFileChooser fc=new JFileChooser();
            int returnVal=fc.showSaveDialog(ViewSourceFrame.this);
            File saveFile=fc.getSelectedFile();
            try {
                FileWriter writeOut = new FileWriter(saveFile);
                writeOut.write(jTextArea1.getText());
                writeOut.close();
            }
            catch (IOException ex) {
                System.out.println("保存失败");
            }
        }
    }
}

上一页  [1] [2] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有