第三个:TOOLBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ToolBar extends JToolBar implements ActionListener{
private JButton jbBack = new JButton(new ImageIcon("./Back16.gif"));
private JButton jbForward = new JButton(new ImageIcon("./Forward16.gif"));
private JButton jbRefresh = new JButton(new ImageIcon("./Refresh16.gif"));
private JButton jbGo = new JButton("Go");
private JButton jbShogun = new JButton(new ImageIcon("./s.gif"));
private JTextField jtfLocation = new JTextField(20);
private BrowserFrame bfRef;
public ToolBar() {
     this(new BrowserFrame());
}
public ToolBar(BrowserFrame bf) {
     bfRef = bf;
     setName("Toolbar");
     setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
     jbBack.setToolTipText("Back");
     jbForward.setToolTipText("Forward");
     jbRefresh.setToolTipText("Refresh");
     jbGo.setToolTipText("Go");
     jbShogun.setToolTipText("Homepage");
     this.add(jbBack);
     this.add(jbForward);
     this.add(jbRefresh);
     this.add(new JLabel("Location:"));
     this.add(jtfLocation);
     this.add(jbGo);
     this.add(new JLabel("   "));
     this.add(jbShogun);
     jbBack.addActionListener(this);
     jbForward.addActionListener(this);
     jbRefresh.addActionListener(this);
     jtfLocation.addActionListener(this);
     jbGo.addActionListener(this);
     jbShogun.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
     if ((e.getSource() == jbGo) || (e.getSource() == jtfLocation))
           bfRef.setCurrentURL(jtfLocation.getText());
     else if(e.getSource() == jbRefresh)
           bfRef.refreshURL();
     else if(e.getSource() == jbBack)
           bfRef.goBack();
     else if (e.getSource() == jbForward)
           bfRef.goForward();
     else if (e.getSource() == jbShogun)
           bfRef.setCurrentURL("http://www.163.com");
}
public void setTextField(String s) {
     jtfLocation.setText(s);
}
}
第四个:MenuBar.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuBar extends JMenuBar implements ActionListener, ItemListener{
     private JMenu jmFile = new JMenu("文件");
     private JMenu jmEdit = new JMenu("编辑");
     private JMenu jmSet = new JMenu("设置");
     private JMenu jmBooks = new JMenu("标签");
     private JMenu jmHelp = new JMenu("帮助");
     private JMenuItem jmiNew =new JMenuItem("新建");
     private JMenuItem jmiOpen =new JMenuItem("打开");
     private JMenuItem jmiSaveas =new JMenuItem("另存为");
     private JMenuItem jmiExit = new JMenuItem("退出");
     private JMenuItem jmiCopy = new JMenuItem("复制");
     private JMenuItem jmiCut = new JMenuItem("剪切");
     private JMenuItem jmiPaste = new JMenuItem("粘贴");
     private JMenuItem jmiFcolor = new JMenuItem("文字颜色");
     private JMenuItem jmiBcolor = new JMenuItem("背景颜色");
     private JMenuItem jmiAddBook = new JMenuItem("加入标签");
     private JCheckBoxMenuItem jmiRemoveBook = new JCheckBoxMenuItem("移除标签");
     private JMenuItem jmiAbout = new JMenuItem("帮助");
     private boolean bRemove = false;
     private BrowserFrame bfRef;
     private BookList bl = new BookList();
public MenuBar() {
     this(new BrowserFrame());
}
public MenuBar(BrowserFrame bf) {
     bfRef = bf;
     jmFile.add(jmiNew);
     jmFile.add(jmiOpen);
     jmFile.add(jmiSaveas);
     jmFile.add(jmiExit);
     jmEdit.add(jmiCopy);
     jmEdit.add(jmiCut);
     jmEdit.add(jmiPaste);
     jmSet.add(jmiFcolor);
     jmSet.add(jmiBcolor);
     jmBooks.add(jmiAddBook);
     jmBooks.add(jmiRemoveBook);
     jmHelp.add(jmiAbout);
     jmBooks.addSeparator();
     add(jmFile);
     add(jmEdit);
     add(jmSet);
     add(jmBooks);
     add(jmHelp);
     jmiNew.addActionListener(this);
     jmiOpen.addActionListener(this);
     jmiSaveas.addActionListener(this);
     jmiExit.addActionListener(this); 
     jmiCopy.addActionListener(this);
     jmiCut.addActionListener(this);
     jmiPaste.addActionListener(this);
     jmiFcolor.addActionListener(this);
     jmiBcolor.addActionListener(this);
     jmiAbout.addActionListener(this);
     jmiAddBook.addActionListener(this);
     jmiRemoveBook.addItemListener(this);
     addBooks();
}
public void addBooks() {
     for(int x = 0; x < bl.getSize(); x++) {
       JMenuItem jmiBM = new JMenuItem(bl.returnURL(x));
       jmBooks.add(jmiBM);
       jmiBM.addActionListener(this);}
}
public void actionPerformed(ActionEvent e) {
     if (e.getSource() == jmiExit)
     System.exit(0);
     else if (e.getSource() == jmiAbout)
       new PopupDialog("Help?", "Help? 自己动手,丰衣足食!!^_^");
     else if (e.getSource() == jmiAddBook) {
       bl.add(bfRef.getCurrentURL());
       addBookItem(bfRef.getCurrentURL());
      }
     else {
       if (!bRemove)
          bfRef.setCurrentURL(e.getActionCommand());
       else {
          jmBooks.remove((JMenuItem) e.getSource());
          try {
              bl.remove(e.getActionCommand());
             }catch(Exception ex) {new PopupDialog("错误", "移除标签出错!");}
            }
          }
}
public void itemStateChanged(ItemEvent e) {
   if(e.getSource() == jmiRemoveBook)
       bRemove = !bRemove;
}
public void addBookItem(String s) {
     JMenuItem newItem = new JMenuItem(s);
     newItem.addActionListener(this);
     jmBooks.add(newItem);
  }
}

解决方案 »

  1.   

    第五个WebNode.java
    public class WebNode {
         private String site;
         private WebNode next;
         private WebNode prev;
    public WebNode() {
         site = null;
         next = null;
         prev = null;
    }
    public WebNode(String addr) {
         site = addr;
         next = null;
         prev = null;
    }
    public void setURL(String url) {
        site = url;}
    public String getURL() {
        return site;
    }
    public void setNext(WebNode node) {
        next = node;
    }
    public WebNode getNext() {
         return next;
    }
    public void setPrev(WebNode node) {
         prev = node;
    }
    public WebNode getPrev() {
         return prev;
    }
    }
    第六个PopuPDialog.java
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class PopupDialog extends JDialog implements ActionListener{
          public PopupDialog(String title, String text) {
               super( new Frame(""), title, true );
               setSize(300,100);
               setResizable( false );
               setLocation (240, 240);
               JLabel message = new JLabel(text, JLabel.CENTER);
               getContentPane().add( message, BorderLayout.CENTER );
               JButton close = new JButton( " 关闭 " );
               JPanel p = new JPanel();
               close.addActionListener( this );
               p.add(close);
               getContentPane().add( p, BorderLayout.SOUTH );
               setVisible(true);}
           public void actionPerformed( ActionEvent e ){
               setVisible( false );}
    }
    第七个List.java
    public abstract class List {
         protected WebNode head;
         protected int size = 0;
         public List() {
             head = null;}
         public void remove(String s) throws Exception {}
         public void add(String url) {
             WebNode placeHolder = new WebNode();
             placeHolder.setURL(url);
             placeHolder.setNext(head);
             head=placeHolder;
             size++;}
        public String getName(int i) throws Exception{
            WebNode wn = head;
            if (isEmpty())
                throw new Exception("记录为空");
            for (int x = 0; x < i; x++) {
                 wn = wn.getNext();
             }
            return wn.getURL();
             }
            public int getSize() {return size;}
             public boolean isEmpty() { return (head == null); }
    }
    第八个WebWindow.java
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    import java.awt.*;
    import java.io.*;
    public class WebWindow extends JScrollPane implements HyperlinkListener{
          private JEditorPane je = new JEditorPane();
          private BrowserFrame bfRef;
          public WebWindow() { 
               this(new BrowserFrame());}
          public WebWindow(BrowserFrame bf) {
               bfRef = bf;
               je.setEditable(false);
               setViewportView(je);
               setAutoscrolls(false);}
    public void setCurrentURL(String sURL) throws Exception {
          try {
                  je = new JEditorPane(sURL);
                  je.setEditable(false);
                  setViewportView(je);
                  je.addHyperlinkListener(this);
              }
          catch(Exception e) {throw new Exception("有些小错误!去不了你想去的地方!^_^");
               }
    }
    public void hyperlinkUpdate(HyperlinkEvent evt) {
         if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                 bfRef.setCurrentURL(evt.getURL().toString());
          }
      }
    }
    第九个 HistoryList.java
    public class HistoryList extends List{
         private WebNode tail;
         private WebNode current;
         public HistoryList() {
            super();
            tail = null;
             }
         public void add(String url) {
            super.add(url);
            if (size == 1) {
                  tail = head;
                  current = head;
               }
            else {
                  head.setPrev(current);
                  current.setNext(head);
                  current = head;
                  head.setNext(null);
                 }
       }
    public String getLast() throws Exception{
         if(current.getPrev() != null)
            current = current.getPrev();
         else
             throw new Exception("无法后退!");
          return current.getURL();
         }
    public String getNext() throws Exception{
         if(current.getNext() != null)
             current = current.getNext();
         else
              throw new Exception("不能前进!");
          return current.getURL();
    }
    }
    第十个BookList.java
    import java.util.*;
    import java.io.*;
    public class BookList extends List {
         public static final String BFILE = "book.lis";
         public BookList() {
              super();
              readIntoList();
          }
    public void add(String s) {
             super.add(s);
             writeToFile();}
    public void startupAdd(String s){
        super.add(s);
    }
    public void remove(String s) throws Exception {
         WebNode holder = head;
         boolean isFound = false;
         if(isEmpty())
            throw new Exception("记录为空");
         else if ((holder.getURL()).equals(s)){
             head = holder.getNext();
             size--;
             writeToFile();
             isFound = true;
         }
         else {
             while ((holder != null) && (!isFound)){
                 if ((holder.getNext().getURL()).equals(s)) {
                      holder.setNext(holder.getNext().getNext());
                       isFound = true;
                        size--;
                      writeToFile();
                     }
                  else{
                      holder = holder.getNext();
                        }
                    }
                }
              if (!isFound) {
                throw new Exception("No match found!");
                  }
             }
    private void writeToFile() {
       try {
               FileOutputStream fOut = new FileOutputStream(BFILE);
               for (WebNode x = head; x != null; x = x.getNext()) {
                    fOut.write(x.getURL().getBytes());
                    fOut.write('\n');
                  }
              fOut.close();
            }catch (Exception e) {
                     new PopupDialog("文件错误", "不能写入文件");
               }
          }
    private void readIntoList() {
          try {
                  FileInputStream fIn = new FileInputStream(new File(BFILE));
                  byte bt[] = new byte[(int) (new File(BFILE)).length()];
                  fIn.read(bt);
                  String s = new String(bt);
                  StringTokenizer st = new StringTokenizer(s, "\n");
                  while(st.hasMoreTokens())
                  startupAdd(st.nextToken());
                  fIn.close();}catch(Exception e) { }
     }
    public String returnURL(int loc) {
          WebNode wn = head;
          for (int x = 0; x < loc; x++)
          wn = wn.getNext();
          return wn.getURL();
       }
    }
      

  2.   

    如果是省事,用JEditorPane 来作浏览器的话,JEditorPane 只支持html3.2,并且不支持javascript、FLASH等,对现在的大多数网页使用的是html4.0+javascript来说,很多地方不能被解析,看上去乱七八糟也是理所当然的。乱码的问题是编码的问题,有的使用的是utf-8来制作页面,有的人使用gb2312,有的使用unicode,所以,你可以截取http头来判断当前页面使用的编码方式,并且在正确转换后才可以避免乱码问题。所以,如果如果做用java做一个兼容ie4.0以上的浏览器的话,除了使用JEditorPane 外,你必须花大量的时间做其它工作,至少你得写出javascript的解释器