Java Reference
In-Depth Information
elements which are arranged in a panel called ToolPanel . There is a text field for
entering a URL and two buttons labelled 'GO!' and 'Back'. When pressing the first
one, the browser loads the web page specified in the text field. One can navigate
through the web following links. The browser keeps a record of the previous pages.
Pressing the 'Back'-button makes the browser return to the web page from which
the currently displayed page had been reached. Going back stops when we get
back to the page whose URL had been directly entered into the text field.
The history of recently visited web pages is stored in a stack ,adata structure
which stores data like a stack of paper. New data can be placed on the top of the
stack and only the topmost data can be taken from the stack. Assume we first look
at web page W 1 which contains a link to page W 2 .Weuse this link to get to W 2 .
Then the address (URL) of W 1 is put on top of the stack. If W 2 contains a link to
page W 3 and we use this link, then the address W 2 is put on the stack. Using the
'Back'-button of our browser will cause the application to remove the top-most
address ( W 2 ) from the stack and display the corresponding page. After that, W 1
is the top-most address on the stack. Using the 'Back'-button again will therefore
cause the application to remove the address of W 1 from the stack and display it.
The 'GO!'- and 'Back'-buttons of our application are monitored by a Button-
Listener which implements the interface ActionListener and performs the de-
sired actions if one of the buttons is pressed. The hyperlinks are monitored by class
LinkLis which implements a HyperlinkListener like in the previous section.
The application is listed below. It can be tested by using the following URL:
http://www.imm.dtu.dk/swingbook/HTMLTest/test1.html
File: its/HTML/Browser.java
1. package its.HTML;
2.
3. import its.SimpleFrame.SimpleFrame;
4. import java.awt.*;
5. import java.awt.event.ActionEvent;
6. import java.awt.event.ActionListener;
7. import java.net.URL;
8. import java.util.Stack;
9. import javax.swing.*;
10. import javax.swing.event.HyperlinkEvent;
11. import javax.swing.event.HyperlinkListener;
12.
13. public class Browser extends SimpleFrame
14. {
15.
private JEditorPane ediPane;
16.
private static String startURL =
"http://www.imm.dtu.dk/swingbook/HTMLTest/test1.html";
17.
private ToolPanel tools;
18.
private Stack urlStack;
19.
Search WWH ::




Custom Search