书上的一个例子
import javax.swing.text.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.event.*;public class useSimpleWebBrowser
{
public static void main(String[] args)
{
// get the first URL
String initialPage = "http://www.sohu.com";
if (args.length > 0) initialPage = args[0];
// set up the editor pane
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
jep.addHyperlinkListener(new LinkFollower(jep));
try
{
jep.setPage(initialPage);
}
catch (IOException e)
{
System.err.println("Usage: java SimpleWebBrowser url");
System.err.println(e);
System.exit(-1);
}
// set up the window
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Simple Web Browser");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.show();
}
}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)
{ }
}
}
}
import javax.swing.text.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.event.*;public class useSimpleWebBrowser
{
public static void main(String[] args)
{
// get the first URL
String initialPage = "http://www.sohu.com";
if (args.length > 0) initialPage = args[0];
// set up the editor pane
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
jep.addHyperlinkListener(new LinkFollower(jep));
try
{
jep.setPage(initialPage);
}
catch (IOException e)
{
System.err.println("Usage: java SimpleWebBrowser url");
System.err.println(e);
System.exit(-1);
}
// set up the window
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Simple Web Browser");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.show();
}
}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)
{ }
}
}
}
解决方案 »
- 关于RMI的问题~求助 急死人啊~~ (ConnectIOException~ConnectException)
- 关于struts2下载的那点奇怪事
- IntelliJ IDEA 汉化版
- 关于xml文件的解析
- 请高手看看,为什么代码执行后异常,请帮忙修改。
- 各位兄弟们,路过的都进来看一下啊
- 高手请进:一个关于线程同步的问题,是java线程笔记中作者提出的一个问题
- 在象数据库插入一条纪录后怎样得到此纪录中自动编号字段的内容?
- 在java applet中如何关闭IE窗口呢?
- main方法怎么可以访问私有方法和字段,求解
- 正则式的中文问题!!
- 请教各位,一个小迷惑,为什么main函数也需要throws Throwable?
import java.awt.event.*;
import java.net.*;
import java.io.*;
// Java extension packages
import javax.swing.*;
import javax.swing.event.*;public class ReadServerFile extends JFrame {
private JTextField enterField;
private JEditorPane contentsArea; // set up GUI
public ReadServerFile()
{
super( "Simple Web Browser" ); Container container = getContentPane(); // create enterField and register its listener
enterField = new JTextField( "Enter file URL here" ); enterField.addActionListener( new ActionListener() { // get document specified by user
public void actionPerformed( ActionEvent event )
{
getThePage( event.getActionCommand() );
} } // end anonymous inner class ); // end call to addActionListener container.add( enterField, BorderLayout.NORTH ); // create contentsArea and register HyperlinkEvent listener
contentsArea = new JEditorPane();
contentsArea.setEditable( false ); contentsArea.addHyperlinkListener( new HyperlinkListener() { // if user clicked hyperlink, go to specified page
public void hyperlinkUpdate( HyperlinkEvent event )
{
if ( event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED )
getThePage( event.getURL().toString() );
} } // end anonymous inner class ); // end call to addHyperlinkListener container.add( new JScrollPane( contentsArea ),
BorderLayout.CENTER ); setSize( 400, 300 );
setVisible( true );
} // load document; change mouse cursor to indicate status
private void getThePage( String location )
{
// change mouse cursor to WAIT_CURSOR
setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR ) ); // load document into contentsArea and display location in
// enterField
try {
contentsArea.setPage( location );
enterField.setText( location );
} // process problems loading document
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error retrieving specified URL",
"Bad URL", JOptionPane.ERROR_MESSAGE );
} setCursor( Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR ) );
} // begin application execution
public static void main( String args[] )
{
ReadServerFile application = new ReadServerFile(); application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}} // end class ReadServerFile
http://java-fan.vicp.net:10000/cgi-bin/topic.cgi?forum=23&topic=195&show=0
mymoto(忽忽) 你的程序好象不能运行啊,输入网址后没有用啊