520kobe 2013-12-30 09:17
浏览 2678

用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();

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler