package Browser;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class Browsertest extends JFrame {
private JTextField enterfield;
private JEditorPane contentsArea;
public Browsertest()
{
super("Web浏览器");
Container container = getContentPane();
enterfield = new JTextField("http://www.google.com");
enterfield.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event)
{
getThePage(event.getActionCommand());
}
} );
container.add(enterfield,BorderLayout.NORTH);
contentsArea = new JEditorPane();
contentsArea.setEditable(false);
contentsArea.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent event)
{
if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
getThePage(event.getURL().toString());
}
});
container.add(new JScrollPane(contentsArea),BorderLayout.CENTER);
setSize(1000,800);
setVisible(true);
}
private void getThePage(String location) {
try {
contentsArea.setPage(location);
enterfield.setText(location);
}catch(IOException ioException) {
System.out.println("Bad URL");
}
}
public static void main(String args[]) {
Browsertest app = new Browsertest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}