我如何将JTextField输入存储到arraylist中

时间:2017-04-04 10:06:48

标签: java arrays swing browser jtextfield

我正在尝试在java中创建一个Web浏览器,并且我正在尝试查看如何将JTextField输入存储到arraylist中,然后我可以访问该arraylist以显示Web历史记录并使用后向和后向迭代以进行前进和后退导航按钮请有人帮助我这么卡住

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

public class Browser_main {

public static void main(String[] args) {

    final JFrame frame = new JFrame("Quest");

    JTextField user_home_page = new JTextField("");

    String initialURL = "http://www.google.com/";

    final JEditorPane editor_pane_browser;

    ImageIcon url_Icon = new     ImageIcon("/Users/GeorgeBradley/Desktop/url-icon.png");
    JLabel url_label = new JLabel(url_Icon);

    final JTextField txtURL = new JTextField(initialURL, 60);

    ImageIcon search_Icon = new ImageIcon("/Users/GeorgeBradley/Desktop/Search-icon.png");
    JButton browse_button = new JButton("Search", search_Icon);

    ImageIcon refresh_Icon = new ImageIcon("/Users/GeorgeBradley/Desktop/refresh-icon.png");
    JButton refresh_button = new JButton(refresh_Icon);

    ImageIcon forward_Icon = new ImageIcon("/Users/GeorgeBradley/Desktop/Actions-go-next-icon.png");
    JButton forward_button = new JButton(forward_Icon);

    ImageIcon backward_Icon = new ImageIcon("/Users/GeorgeBradley/Desktop/Actions-go-previous-icon.png");
    JButton backward_button = new JButton(backward_Icon);

    ImageIcon home_Icon = new ImageIcon("/Users/GeorgeBradley/Desktop/home-icon.png");
    JButton home_button = new JButton(home_Icon);


    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(home_button);
    panel.add(backward_button);
    panel.add(forward_button);
    panel.add(refresh_button);
    panel.add(url_label);
    panel.add(txtURL);
    panel.add(browse_button);

    //making tabbed pane
    JTabbedPane tabbedPane = new JTabbedPane();
    ImageIcon tabbed_Icon =new ImageIcon("/Users/GeorgeBradley/Desktop/Hat-icon.png");

    //JComponent panel1 = makeTextField("Panel #1");
    //tabbedPane.addTab("Tab 1");
    //tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);



    try {
        editor_pane_browser = new JEditorPane(initialURL);
        editor_pane_browser.setEditable(false);

        editor_pane_browser.addHyperlinkListener(new HyperlinkListener() {

            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    JEditorPane pane = (JEditorPane) e.getSource();
                    if (e instanceof HTMLFrameHyperlinkEvent) {
                        HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                        HTMLDocument doc = (HTMLDocument) pane.getDocument();
                        doc.processHTMLFrameHyperlinkEvent(evt);
                    } else {
                        try {
                            pane.setPage(e.getURL());
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                }
            }
        });

        home_button.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                     try {
                         editor_pane_browser.setPage(initialURL);
                     } catch (IOException ex){
                         ex.printStackTrace();
                     }
                    }
                }
                );

        browse_button.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            editor_pane_browser.setPage(txtURL.getText().trim());
                        } catch (IOException ex) {
                            JOptionPane.showMessageDialog(txtURL,"INVALID URL");
                        }
                    }
                });

        refresh_button.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        try {
                            editor_pane_browser.setPage(txtURL.getText().trim());
                        } catch (IOException error_refreshing) {
                            JOptionPane.showMessageDialog(refresh_button, "ERROR REFRESHING PAGE");;
                        }
                    }
                });

        JScrollPane sp = new JScrollPane(editor_pane_browser);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel, BorderLayout.NORTH);
        frame.getContentPane().add(sp, BorderLayout.CENTER);

        frame.setSize(500, 350);
        frame.setVisible(true);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

}

0 个答案:

没有答案