-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebBrowser2.java
130 lines (114 loc) · 2.77 KB
/
WebBrowser2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class WebBrowser2
{
public static void main(String [] args)
{
JFrame frame = new EditorPaneFrame();
frame.show();
}
}
class EditorPaneFrame extends JFrame
{
private JTextField url;
private JCheckBox editable;
private JButton loadButton;
private JButton backButton;
private JEditorPane editorPane;
private Stack urlStack = new Stack();
public EditorPaneFrame()
{
setTitle("Java Web Browser");
setSize(600,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
// set up text field and load button for typing in URL
url = new JTextField(30);
loadButton = new JButton("Load");
loadButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
// remember URL for back button
urlStack.push(url.getText());
editorPane.setPage(url.getText());
}
catch(Exception e)
{
editorPane.setText("Error: " +e);
}
}
});
// set up back button and button action
backButton = new JButton("Back");
backButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(urlStack.size()<=1) return;
try
{
urlStack.pop();
String urlString = (String)urlStack.peek();
url.setText(urlString);
editorPane.setPage(urlString);
}
catch(IOException e)
{
editorPane.setText("Error : " +e);
}
}
});
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
{
try
{
urlStack.push(event.getURL().toString());
url.setText(event.getURL().toString());
editorPane.setPage(event.getURL());
}
catch(IOException e)
{
editorPane.setText("Error: " + e);
}
}
}
});
editable = new JCheckBox();
editable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
editorPane.setEditable(editable.isSelected());
}
});
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(editorPane), "Center");
JPanel panel = new JPanel();
panel.add(new JLabel("URL"));
panel.add(url);
panel.add(loadButton);
panel.add(backButton);
panel.add(new JLabel("Editable"));
panel.add(editable);
contentPane.add(panel,"South");
}
}