-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleWebBrowser.java
88 lines (79 loc) · 2.4 KB
/
SimpleWebBrowser.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
package java_web_programming;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
/**
* A simple Web Browser
*
* @author ÌÆÁú
*
*/
public class SimpleWebBrowser extends JFrame{
public static void main(String[] args) {
final int WIDTH = 512;
final int HEIGHT = 350;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SimpleWebBrowser swb = new SimpleWebBrowser("Simple Web Browers");
swb.setSize(WIDTH,HEIGHT);
swb.setVisible(true);
swb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
private static final long serialVersionUID = 1L;
private JTextField urlField = new JTextField();
private JButton goButton = new JButton("תµ½");
private JEditorPane editorPane = new JEditorPane();
public SimpleWebBrowser(String title){
super(title);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(new Color(255, 255, 255));
panel.add(new JLabel("URLµØÖ·"),BorderLayout.WEST);
panel.add(urlField, BorderLayout.CENTER);
panel.add(goButton, BorderLayout.EAST);
add(panel,BorderLayout.NORTH);
add(new JScrollPane(editorPane),BorderLayout.CENTER);
editorPane.setEditable(false);
ActionListener actionListener = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
URL url = new URL(urlField.getText().trim());
editorPane.setPage(url);
}catch(IOException ioex){
ioex.printStackTrace();
}
}
};//actionListener
urlField.addActionListener(actionListener);
goButton.addActionListener(actionListener);
editorPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
try{
URL url = e.getURL();
urlField.setText(url.toString());
editorPane.setPage(url);
}catch(IOException ioex){
ioex.printStackTrace();
}
}
});//addHyperlinkListener
}
}