(转载)
该程序让客户在文本域输入一个HTML文件的URL,按回车键之后,在编辑窗格内显示该HTML文件.
程序代码如下:// WebBrowser.java: Display HTML file in JEditorPaneimport java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import javax.swing.event.*;
import java.io.*; public class WebBrowser extends JApplet
implements ActionListener,HyperlinkListener{
// JEditor pane to view HTML files
private JEditorPane jep=new JEditorPane();
// Label for URL
private JLabel jlblURL=new JLabel("URL");
// Text field for entering URL
private JTextField jtfURL=new JTextField();
// Initialize the applet
public void init(){
// Create a panel jpURL to hold the label and text field
JPanel jpURL=new JPanel();
jpURL.setLayout(new BorderLayout());
jpURL.add(jlblURL,BorderLayout.WEST);
jpURL.add(jtfURL,BorderLayout.CENTER);
// Create a scroll pane to hold JEditorPane
JScrollPane jspViewer=new JScrollPane();
jspViewer.getViewport().add(jep,null);
// Place jpURL and jspViewer in the applet
this.getContentPane().add(jspViewer,BorderLayout.CENTER);
this.getContentPane().add(jpURL,BorderLayout.NORTH); // Set jep noneditable
jep.setEditable(false);
// Register listener
jep.addHyperlinkListener(this);
jtfURL.addActionListener(this);
} public void actionPerformed(ActionEvent e){
// TODO: Implement this java.awt.event.ActionListener method
try{
// Get the URL from text field
URL url=new URL(jtfURL.getText().trim());
// Display the HTML file
jep.setPage(url);
}catch(IOException ex){
System.out.println(ex);
}
} public void hyperlinkUpdate(HyperlinkEvent e){
// TODO: Implement HyperlinkListener method
try{
// Get the URL from text field
jep.setPage(e.getURL());
}catch(IOException ex){
System.out.println(ex);
}
}
// Main method
public static void main(String[] args){
// Create a frame
JFrame frame=new JFrame("Web Browser");
// Create an instance of the applet
WebBrowser applet=new WebBrowser();
// Add the applet instance to the frame
frame.getContentPane().add(applet,BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.setSize(600,600);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}不足:该程序不能浏览本地的HTML文件.只能浏览远程的HTML文件,且必须输入以http://开头的URL.