2 u010686558 u010686558 于 2013.12.30 17:17 提问

用netbeans做一个网页浏览器,

要实现的功能很简单,要有历史记录显示和数钱的添加,可是一直出现问题,求各位大神帮帮忙。
private void File_exitActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0); // TODO add your handling code here:
}

private void HTMLActionPerformed(java.awt.event.ActionEvent evt) {                                     

InputStream ins = null;
BufferedInputStream bins = null;
try {
URL u = new URL(URLDecoder.decode(URL.getText(), "utf-8"));

        ins = u.openStream();
        bins = new BufferedInputStream(ins);
        byte[] b = new byte[32];
        int count;

        StringBuffer sb = new StringBuffer();
        while ((count = bins.read(b, 0, b.length)) != -1) {
            sb.append(new String(b, 0, count));
        }
        try {
            File file = new File("src/WebBrowser/源文件.txt ");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(sb.toString());
            writer.close();
        } catch (Exception e) {
            System.out.println(e);
        }
 JOptionPane.showMessageDialog(this, "源文件已下载到源文件.txt,请自行查看!");
    } catch (MalformedURLException me) {
        System.out.println(me);
    } catch (IOException ie) {
        System.out.println(ie);
    } finally {
        if (bins != null) {
            try {
                bins.close();
            } catch (IOException ioe) {
                System.out.println("缓冲输入流关闭异常");
            }
        }
        if (ins != null) {
            try {
                ins.close();
            } catch (IOException ioe) {
                System.out.println("输入流关闭异常");
            }
        }
    }
    // TODO add your handling code here:
}                                    

private void AboutActionPerformed(java.awt.event.ActionEvent evt) {                                      
     JOptionPane.showMessageDialog(this, "这是一个简单的浏览器" + '\n' + "姓名:梁丽丽"
            + '\n' + "学号:110604404152" + '\n' + "班级:计科1141" + '\n'
           + "时间:2013年12月15日" + '\n' + "O(∩_∩)O~", "关于浏览器", WIDTH);        // TODO add your handling code here:
}                                     

private void GroundColorActionPerformed(java.awt.event.ActionEvent evt) {                                            
     Color newColor = JColorChooser.showDialog(this, "选择背景颜色", Windows.getBackground());
    if (newColor != null) {
        Windows.setBackground(newColor); }       // TODO add your handling code here:
}                                           

private void BackActionPerformed(java.awt.event.ActionEvent evt) {                                     

URL currentUrl = Windows.getPage();
int pageIndex = historyList.indexOf(currentUrl.toString());

    try {
        display(new URL((String) historyList.get(pageIndex -1)), false);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "错误!" + e.getMessage());
    }



    // TODO add your handling code here:
}                                    

private void ForwardActionPerformed(java.awt.event.ActionEvent evt) {                                        

URL currentUrl = Windows.getPage();
int pageIndex = historyList.indexOf(currentUrl.toString());
try {
display(new URL((String) historyList.get(pageIndex +1)), false);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "错误!" + e.getMessage());
}
// TODO add your handling code here:
}

private void RefreshActionPerformed(java.awt.event.ActionEvent evt) {                                        

try {
display(Windows.getPage(), false);//刷新按钮不会增加历史记录!
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "错误!" + e.getMessage());
}
// TODO add your handling code here:
}

private void HomePageActionPerformed(java.awt.event.ActionEvent evt) {                                         

try {
URL url = new URL("http://www.baidu.com");
display(url, true);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "错误" + e.getMessage());
}
// TODO add your handling code here:
}

private void GoActionPerformed(java.awt.event.ActionEvent evt) {                                   
 go();        // TODO add your handling code here:
}                                  

private void History_DelActionPerformed(java.awt.event.ActionEvent evt) {                                            

historyListDel = !historyListDel; // TODO add your handling code here:
}

private void BookMark_DelActionPerformed(java.awt.event.ActionEvent evt) {                                             
   bookmarkListDel = !bookmarkListDel;   // TODO add your handling code here:

}                                            

private void History_ShowActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

private void BookMark_AddActionPerformed(java.awt.event.ActionEvent evt) {                                             

boolean isIn = bookmarkList.contains(URL.getText());
if (!isIn) {
bookmarkList.add(URL.getText());
JMenuItem newItem = new JMenuItem(URL.getText());

        BookMark.add(newItem);
        JOptionPane.showMessageDialog(this, "书签添加成功!(*^__^*) ");
    } else {
       JOptionPane.showMessageDialog(this, "书签已存在,无需重复加入!⊙﹏⊙");
    }
    // TODO add your handling code here:
}                                            

private void BookMarkActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void URLKeyPressed(java.awt.event.KeyEvent evt) {                               

if ((evt.getKeyCode()) == (KeyEvent.VK_ENTER)) {
go();
}
// TODO add your handling code here:
}

private void WindowsHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {                                        

if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
display(evt.getURL(), true);
}
// TODO add your handling code here:
}

private void WindowsMousePressed(java.awt.event.MouseEvent evt) {                                     

if (isVisible) {
JPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
} else {
JPopupMenu1.setVisible(false);
}
isVisible = !isVisible;
// TODO add your handling code here:
}

private void BookMarkAddActionPerformed(java.awt.event.ActionEvent evt) {                                            

JPopupMenu1.setVisible(false);
BookMark_AddActionPerformed(evt);
// TODO add your handling code here:
}

private void HTML2ActionPerformed(java.awt.event.ActionEvent evt) {                                      

JPopupMenu1.setVisible(false);
HomePageActionPerformed(evt);
// TODO add your handling code here:
}

private void ReloadActionPerformed(java.awt.event.ActionEvent evt) {                                       

JPopupMenu1.setVisible(false);
RefreshActionPerformed(evt);
// TODO add your handling code here:
}

private void ExitActionPerformed(java.awt.event.ActionEvent evt) {                                     

System.exit(0); // TODO add your handling code here:
}

private void go() {
try {
String url = URLDecoder.decode(URL.getText(), "utf-8");
if (!url.toLowerCase().startsWith("http://")) {
url = "http://" + url;//确保url格式正确
}
URL currentURL = new URL(url);
if (currentURL!=null) {
display(currentURL, true);
} else {
JOptionPane.showMessageDialog(this, "有些小错误,去不了你想要去的地方%>_<%");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "错误!非法的URL");
}
}

private ArrayList historyList = new ArrayList();

private void display(URL pageUrl, boolean addToList) {

    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {

        Windows.setPage(pageUrl);

        URL newUrl = Windows.getPage();
        URL.setText(newUrl.toString());
        Label.setText(newUrl.getHost());
        if (addToList) {

            boolean isIn = historyList.contains(newUrl.toString());
            if (isIn) {
                historyList.remove(newUrl.toString());
            }
            historyList.add(newUrl.toString());

            if (isIn) {
                History.removeAll();
                History.add("历史记录[10]");
                History.insertSeparator(1);
                if (historyList.size() > 10) {
                    for (int i = historyList.size() - 10; i < historyList.size() - 1; i++) {
                     JMenuItem historyItemR = new JMenuItem(historyList.get(i));
                        historyItemR.addActionListener((ActionListener) this);
                        History.add(historyItemR);
                    }
                } 
                else {
                    for (int i = 0; i < historyList.size(); i++) {
                     JMenuItem historyItem = new JMenuItem(historyList.get(i));
                        historyItem.addActionListener((ActionListener) this);
                        History.add(historyItem);
                    }
                }
            }
            else {
                JMenuItem historyItem = new JMenuItem(URL.getText());
                historyItem.addActionListener((ActionListener) this);
                History.add(historyItem);
            }

            if (historyList.size() > 10) {
                History.remove(2);
            }
        }
        updateButtons();
    } catch (Exception e) {
     JOptionPane.showMessageDialog(this, "不能载入该页面!" + e.getMessage());
    } finally {
        setCursor(Cursor.getDefaultCursor());
    }
}

private void updateButtons() {

    if (historyList.size() < 2) {
        Back.setEnabled(false);
        Forward.setEnabled(false);
    } else {
        String currentUrl = Windows.getPage().toString();

        int pageIndex = historyList.indexOf(currentUrl.toString());
        Back.setEnabled(pageIndex > 0);
        Forward.setEnabled(pageIndex < (historyList.size() - 1));
    }
}

private boolean historyListDel = false;
private boolean bookmarkListDel = false;
private boolean bookmarkListUp = false;
private boolean bookmarkListDown = false;
public void actionPerformed(ActionEvent e) {

    if ((bookmarkListDel == true) && (historyListDel == false)) {
        BookMark.remove((JMenuItem) e.getSource());
        bookmarkList.remove(e.getActionCommand());
        bookmarkListDel = false;
    } 
    else if ((bookmarkListDel == false) && (historyListDel == true) ) {
        History.remove((JMenuItem) e.getSource());
        historyList.remove(e.getActionCommand());
        historyListDel = false;
    }

    else {
        JMenuItem newItem = (JMenuItem) (e.getSource());
        try {
            URL itemURL = new URL(newItem.getText());
            display(itemURL, true);
        } catch (MalformedURLException ex) {
         Logger.getLogger(Browser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
private ArrayList bookmarkList = new ArrayList();

Csdn user default icon
上传中...
上传图片
插入图片