piaoling
2010/2/7 20:34:56
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.net.*;
class LinkFollower implements HyperlinkListener
{
private JEditorPane pane;
public LinkFollower(JEditorPane pane)
{
this.pane=pane;
}
public void hyperlinkUpdate(HyperlinkEvent evt)
{
if (evt.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try
{
pane.setPage(evt.getURL());
}
catch(Exception e){}
}
}
}
public class simple
{
public static void main(String[] args)
{
String initialPage="http://www.oreilly.com";
if (args.length>0) initialPage=args[0];
JEditorPane jep=new JEditorPane();
jep.setEditable(false);
jep.addHyperlinkListener(new LinkFollower(jep));
try{
jep.setPage(initialPage);
}
catch(IOException e){
System.err.println("java simple url");
System.err.println(e);
System.exit(-1);
}
JScrollPane scrollPane=new JScrollPane(jep);
JFrame f=new JFrame("simple web browser");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setContentPane(scrollPane);
f.setSize(512,342);
f.show();
}
}