import java.awt.event.*
import java.io.IOException
import java.net.*
import javax.swing.*
import javax.swing.event.HyperlinkEvent
import javax.swing.event.HyperlinkListener
public class HTTPBrowserDemo extends JFrame {
private JLabel jlAddress,jlInfo
private JTextField jtfAddress
private JEditorPane jtpShow
private JPanel panel
private JButton btnGO
public static void main(String[] args) {
HTTPBrowserDemo hbd=new HTTPBrowserDemo()
}
HTTPBrowserDemo(){
jlAddress=new JLabel("地址")
jlInfo=new JLabel()
jtpShow=new JEditorPane()
panel=new JPanel()
jtfAddress=new JTextField(20)
btnGO=new JButton("转到")
add(panel,BorderLayout.NORTH)
add(jtpShow,BorderLayout.CENTER)
add(jlInfo,BorderLayout.SOUTH)
panel.add(jlAddress)
panel.add(jtfAddress)
panel.add(btnGO)
setVisible(true)
setSize(350, 280)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
btnGO.addActionListener(new ShowHTMLListener())
jtpShow.setEditable(false)
jtpShow.addHyperlinkListener(new MyHyperlinkListener())
}
class ShowHTMLListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String str=jtfAddress.getText()
try {
if (!str.startsWith("http://")){
str="http://"+str
}
jlInfo.setText("连接中...")
URL address=new URL(str)
jtpShow.setPage(address)
jlInfo.setText("完成")
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
} catch (IOException e2) {
// TODO: handle exception
}
}
}
class MyHyperlinkListener implements HyperlinkListener{
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
JEditorPane pane=(JEditorPane)e.getSource()
try {
pane.setPage(e.getURL())
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
}
}
}
}
import javax.swing.*import java.awt.*
import java.awt.event.*
import java.net.*
import java.io.*
import javax.swing.event.*
@SuppressWarnings("serial")
class Win3 extends JFrame implements ActionListener,Runnable
{
JButton button
URL url
JTextField text
JEditorPane editPane
byte b[]=new byte[118]
Thread thread
public Win3()
{
text=new JTextField(20)
editPane=new JEditorPane()
editPane.setEditable(false)
button=new JButton("确定")
button.addActionListener(this)
thread=new Thread(this)
JPanel p=new JPanel()
p.add(new JLabel("输入网址:"))
p.add(text)
p.add(button)
Container con=getContentPane()
con.add(new JScrollPane(editPane),BorderLayout.CENTER)
con.add(p,BorderLayout.NORTH)
setBounds(60,60,400,300)
setVisible(true)
validate()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
editPane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==
HyperlinkEvent.EventType.ACTIVATED)
{
try{
editPane.setPage(e.getURL())
}
catch(IOException e1)
{
editPane.setText(""+e1)
}
}
}
}
)
}
public void actionPerformed(ActionEvent e)
{
if(!(thread.isAlive()))
thread=new Thread(this)
try{
thread.start()
}
catch(Exception ee)
{
text.setText("我正在读取"+url)
}
}
public void run()
{
try {
int n=-1
editPane.setText(null)
url=new URL(text.getText().trim())
editPane.setPage(url)
}
catch(MalformedURLException e1)
{
text.setText(""+e1)
return
}
catch(IOException e1)
{
text.setText(""+e1)
return
}
}
}
public class Example3
{
public static void main(String args[])
{
new Win3()
}
}