Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

swing - Java - JEditorPane - hyperlink problem

I have to write WebBrowser application based on JEditorPane - to school.

I have a little trouble with it, hyperlinks don't work.

Here's my code:

public class BrowserFrame extends javax.swing.JFrame  implements HyperlinkListener {

    /** Creates new form BrowserFrame */
    public BrowserFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        btnBack = new javax.swing.JButton();
        btnForward = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        tfURL = new javax.swing.JTextField();
        btnGo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        epBrowser = new javax.swing.JEditorPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Java Browser");
        setMinimumSize(new java.awt.Dimension(300, 300));
        setName("fMain"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(100, 25));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnBack.setText("<");
        jPanel1.add(btnBack, new java.awt.GridBagConstraints());

        btnForward.setText(">");
        jPanel1.add(btnForward, new java.awt.GridBagConstraints());

        btnRefresh.setText("Od?wie?");
        jPanel1.add(btnRefresh, new java.awt.GridBagConstraints());

        tfURL.setText("http://www.google.com");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(tfURL, gridBagConstraints);

        btnGo.setText("Id?");
        btnGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoActionPerformed(evt);
            }
        });
        jPanel1.add(btnGo, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jPanel1, gridBagConstraints);

        epBrowser.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(epBrowser);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jMenu1.setText("Plik");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Historia");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>


    private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {
        String url;
        epBrowser.setContentType("text/html");
        try {
            url = tfURL.getText();
            if (url.startsWith("http://"))
            url = url.substring(7);
            epBrowser.setPage("http://"+url);

        } catch (IOException ex) {
            Logger.getLogger(BrowserFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

   public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                epBrowser.setPage(event.getURL());
                tfURL.setText(event.getURL().toExternalForm());
            } catch (IOException ex) {
                Logger.getLogger(BrowserFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
  }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BrowserFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnForward;
    private javax.swing.JButton btnGo;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JEditorPane epBrowser;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tfURL;
    // End of variables declaration

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the 2 line change.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event .*;
import java.io.IOException;
import java.util.logging.*;

public class BrowserFrame extends JFrame  implements HyperlinkListener {

    /** Creates new form BrowserFrame */
    public BrowserFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form. */
    private void initComponents() {
        GridBagConstraints gridBagConstraints;

        jPanel1 = new JPanel();
        btnBack = new JButton();
        btnForward = new JButton();
        btnRefresh = new JButton();
        tfURL = new JTextField();
        btnGo = new JButton();
        jScrollPane1 = new JScrollPane();
        epBrowser = new JEditorPane();

        // this is the trick!
        epBrowser.setEditable(false);
        epBrowser.addHyperlinkListener(this);    

        jMenuBar1 = new JMenuBar();
        jMenu1 = new JMenu();
        jMenu2 = new JMenu();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Java Browser");
        setMinimumSize(new Dimension(300, 300));
        setName("fMain"); // NOI18N
        getContentPane().setLayout(new GridBagLayout());

        jPanel1.setPreferredSize(new Dimension(100, 25));
        jPanel1.setLayout(new GridBagLayout());

        btnBack.setText("<");
        jPanel1.add(btnBack, new GridBagConstraints());

        btnForward.setText(">");
        jPanel1.add(btnForward, new GridBagConstraints());

        btnRefresh.setText("Odswiez");
        jPanel1.add(btnRefresh, new GridBagConstraints());

        tfURL.setText("http://www.google.com");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(tfURL, gridBagConstraints);

        btnGo.setText("Idz");
        btnGo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnGoActionPerformed(evt);
            }
        });
        jPanel1.add(btnGo, new GridBagConstraints());

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        getContentPane().add(jPanel1, gridBagConstraints);

        epBrowser.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(epBrowser);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jMenu1.setText("Plik");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Historia");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>


    private void btnGoActionPerformed(ActionEvent evt) {
        String url;
        epBrowser.setContentType("text/html");
        try {
            url = tfURL.getText();
            if (url.startsWith("http://"))
            url = url.substring(7);
            epBrowser.setPage("http://"+url);

        } catch (IOException ex) {
            Logger.getLogger(BrowserFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

   public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                epBrowser.setPage(event.getURL());
                tfURL.setText(event.getURL().toExternalForm());
            } catch (IOException ex) {
                Logger.getLogger(BrowserFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
  }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BrowserFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private JButton btnBack;
    private JButton btnForward;
    private JButton btnGo;
    private JButton btnRefresh;
    private JEditorPane epBrowser;
    private JMenu jMenu1;
    private JMenu jMenu2;
    private JMenuBar jMenuBar1;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTextField tfURL;
    // End of variables declaration

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...