Example 5.6 is an improved version of the
URLViewer
you first encountered in Chapter 2. This is a
simple application that provides a window in which you can view the
contents of a URL. It assumes that those contents are more or less
ASCII text. (In future chapters, I’ll remove that restriction.)
Figure 5.1 shows the result. Our application has a
text area in which the user can type a URL, a Load button that the
user uses to load the specified URL, and a
StreamedTextArea
component that displays the text
from the URL. Each of these corresponds to a field in the
URLViewer
class.
Example 5-6. The URLViewer Program
import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import com.macfaq.awt.*; import com.macfaq.io.*; public class URLViewer extends Frame implements WindowListener, ActionListener { TextField theURL = new TextField(); Button loadButton = new Button("Load"); StreamedTextArea theDisplay = new StreamedTextArea(); public URLViewer() { super("URL Viewer"); } public void init() { this.add("North", theURL); this.add("Center", theDisplay); Panel south = new Panel(); south.add(loadButton); this.add("South", south); theURL.addActionListener(this); loadButton.addActionListener(this); this.addWindowListener(this); this.setLocation(50, 50); this.pack(); this.show(); } public void actionPerformed(ActionEvent evt) { try { URL u = new URL(theURL.getText()); InputStream in = u.openStream(); OutputStream out = theDisplay.getOutputStream(); StreamCopier.copy(in, out); in.close(); out.close(); } catch (MalformedURLException ex) {theDisplay.setText("Invalid URL");} catch (IOException ex) {theDisplay.setText("Invalid URL");} } public void windowClosing(WindowEvent e) { this.setVisible(false); this.dispose(); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public static void main(String args[]) { URLViewer me = new URLViewer(); me.init(); } }
The URLViewer
class itself extends
Frame
. An alternative would have been to extend
Panel
, which would have allowed
URLViewer
objects to be embedded in other
containers. However, this application seemed big enough to justify
exclusive use of a window. URLViewer
implements
the WindowListener
interface to enable the user to
close the window by clicking in the close box. Only the
windowClosing()
method is nontrivial. The other
six window methods are do-nothing methods required to satisfy the
contract of the WindowListener
interface.
The init()
method builds the interface and
displays the window. This is invoked by the main()
method, which constructs a new URLViewer
object.
The constructor is quite simple, merely passing the title of the
frame to the superclass constructor.
The streamed text area is filled when the user clicks the Load button
or hits Return inside the URL text field. The
URLViewer
object listens to both of these
components. The URLViewer
’s
actionPerformed()
method constructs a URL from the
text in the text field, then opens an input stream from the URL in
the text field. Next, StreamCopier
from Chapter 3 pours the data from the URL’s input
stream into the text area’s output stream. When that’s
finished, both streams are closed.
18.191.253.62