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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…