是什么导致我的java awt和swing浏览器的后退按钮无法正常工作?

问题描述 投票:0回答:1

actionForward和actionBack函数都抛出IndexOutofBounds异常,我不知道为什么吗?我的任务是制作一个非常简单的Web浏览器,该浏览器具有后退和前进按钮功能以及url地址栏。当ArrayList pageList的大小为2时,按钮将按预期工作。但是,一旦pageList的大小为3或更大,它们就会中断。

class Proj03RunnerHtmlHandler extends JFrame implements HyperlinkListener{
    JEditorPane html;
    JButton backButton, forwardButton;
    JTextField urlTextField;
    ArrayList<String> pageList = new ArrayList<String>();

    public Proj03RunnerHtmlHandler(String website) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Asg03");
        pageList.add(website);

        try{
            if(website != null){
                html = new JEditorPane(website);
                html.setEditable(false);
                html.addHyperlinkListener(this);

                JPanel buttonPanel = new JPanel();
                backButton = new JButton("Back");
                backButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        actionBack();
                    }
                });
                buttonPanel.add(backButton);

                urlTextField = new JTextField("http://www.somesite.com");
                urlTextField.addKeyListener(new KeyAdapter() {
                    public void keyReleased(KeyEvent e){
                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                            try {
                                showPage(new URL(urlTextField.getText()), true);
                            } catch(Exception ey){
                                ey.printStackTrace();
                            }
                        }
                    }
                });
                buttonPanel.add(urlTextField);

                forwardButton = new JButton("Forward");
                forwardButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        actionForward();
                    }
                });
                buttonPanel.add(forwardButton);

                JScrollPane scroller = new JScrollPane();
                JViewport vp = scroller.getViewport();
                vp.add(html);

                this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
                this.getContentPane().add(scroller, BorderLayout.CENTER);
                this.setSize(669,669);
                this.setVisible(true);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public void hyperlinkUpdate(HyperlinkEvent e){
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
            if (!(e instanceof HTMLFrameHyperlinkEvent)) {
                try {
                    showPage(e.getURL(), true);
                } catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }

    public void actionBack() {
        try {
            String currentUrl = html.getPage().toString();
            int currentIndex = pageList.indexOf(currentUrl);
            showPage(new URL(pageList.get(currentIndex - 1)), false);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void actionForward() {
        try {
            String currentUrl = html.getPage().toString();
            int currentIndex = pageList.indexOf(currentUrl);
            showPage(new URL(pageList.get(currentIndex + 1)), false);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void showPage(URL pageUrl, boolean addToList){
        try {
            URL currentUrl = html.getPage();

            html.setPage(pageUrl);

            if (addToList) {
                int listSize = pageList.size();
                if (listSize > 0) {
                    int pageIndex =
                            pageList.indexOf(currentUrl.toString());
                    if (pageIndex < listSize - 1) {
                        for (int i = listSize - 1; i > pageIndex; i--) {
                            pageList.remove(i);
                        }
                    }
                }
                pageList.add(pageUrl.toString());
            }
            urlTextField.setText(pageUrl.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
java swing awt
1个回答
0
投票
                if (pageIndex < listSize - 1) {
                    for (int i = listSize - 1; i > pageIndex; i--) {
                        pageList.remove(i);
                    }
                }

有两个明显的问题。

  • 您应该只从历史记录中删除一页。
  • 如果页面位于末尾(pageIndex == listSize - 1),则您不会删除页面,但会添加重复的页面。
© www.soinside.com 2019 - 2024. All rights reserved.