Package Ex30;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*; public class Viewremotefile extends japplet{//Button to view the fileprivate JButton Jbtview = new JButton ("View");//Tex T field to receive file nameprivate JTextField jtfurl = new JTextField;//Text area to store fileprivate JTextArea JTA File = new JTextArea ();//Label to display statusprivate JLabel jlblstatus = new JLabel ();/** Initialize the applet */publ IC void init () {//Create a panel to hold a label, a text field, and a buttonjpanel p1 = new JPanel ();p 1.setLayout (New Bord Erlayout ());p 1.add (New JLabel ("Filename"), borderlayout.west);p 1.add (Jtfurl, Borderlayout.center);p 1.add (Jbtview, Borderlayout.east);//Place text area and panel p to the Appletsetlayout (new BorderLayout ()); Add (new JScrollPane (Jtafile) , Borderlayout.center), add (P1, Borderlayout.north), add (Jlblstatus, Borderlayout.south);//Register listener to handle The "View" Buttonjbtview.addactionlistener (New ACtionlistener () {public void actionperformed (ActionEvent e) {showfile ();}});} private void Showfile () {Java.util.Scanner input = null; Use Scanner for text inputurl url = null;try{//Obtain URL from the text fieldurl = new URL (jtfurl.gettext (). Trim ());// Create a Scanner for input streaminput = new Java.util.Scanner (Url.openstream ());//Read a line and append the line to th E Text Areawhile (Input.hasnext ()) {Jtafile.append (Input.nextline () + "\ n");} Jlblstatus.settext ("File loaded Successfully");} catch (Malformedurlexception ex) {jlblstatus.settext ("url" + URL + "not Found");} catch (IOException e) {jlblstatus.settext (E.getmessage ());} Finally{if (input! = null) Input.close ();}}}
Transfer from http://bbs.csdn.net/topics/390040684
Java get Web page source code