[英]Display clickable HTML links in JEditorPane
I have done some Googling, and all the methods do achieving clickable links in html didn't have much explanation on how they worked(at least not enough for me to understand). 我已经做了一些谷歌搜索,所有的方法确实实现了html中的可点击链接没有太多解释他们如何工作(至少不足以让我理解)。 Anyone willing to elaborate a bit more? 有谁愿意再详细说明一下?
My code: 我的代码:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class WebBrowser extends JFrame {
public JPanel
address_panel, window_panel;
public JLabel
address_label;
public JTextField
address_tf;
public JEditorPane
window_pane;
public JScrollPane
window_scroll;
public JButton
address_b;
private Go go = new Go();
public WebBrowser() throws IOException {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
Image image = null;
try {
image = ImageIO.read(new File("images/icon.gif"));
} catch (IOException e) {
e.printStackTrace();
}
address_label = new JLabel(" address: ", SwingConstants.CENTER);
address_tf = new JTextField("");
address_tf.addActionListener(go);
address_b = new JButton("Go");
address_b.addActionListener(go);
window_pane = new JEditorPane("http://(server):(port)/");
window_pane.setContentType("text/html");
window_pane.setEditable(false);
address_panel = new JPanel(new BorderLayout());
window_panel = new JPanel(new BorderLayout());
address_panel.add(address_label, BorderLayout.WEST);
address_panel.add(address_tf, BorderLayout.CENTER);
address_panel.add(address_b, BorderLayout.EAST);
window_scroll = new JScrollPane(window_pane);
window_panel.add(window_scroll);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(address_panel, BorderLayout.NORTH);
pane.add(window_panel, BorderLayout.CENTER);
setIconImage(image);
setTitle("web browser");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class Go implements ActionListener{
public void actionPerformed(ActionEvent ae){
try {
window_pane.setPage("http://(server):(port)/"+address_tf.getText());
} catch (MalformedURLException e) {
window_pane.setText("MalformedURLException: " + e);
} catch (IOException e) {
window_pane.setText("IOException: " + e);
}
}
}
}
Thanks in advance(Feel free to point out anything that can be better in my code) 提前致谢(请随意指出我的代码中可能更好的任何内容)
Airis AIRIS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.