package wo;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
public class Ex9 implements ActionListener,HyperlinkListener{
/**
* @param args
*/
JLabel lab;
JTextField te;
JEditorPane content;
JScrollPane pa;
JPanel pal;
Container con;
JFrame frame;
public Ex9(){
frame=new JFrame("我的浏览器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con=frame.getContentPane();
lab=new JLabel("输入地址:");
te=new JTextField();
te.setColumns(20);
te.addActionListener(this);
pal=new JPanel();
pal.setLayout(new FlowLayout());
pal.add(lab);
pal.add(te);
content=new JEditorPane();
content.setEditable(false);
content.addHyperlinkListener(this);
pa=new JScrollPane(content);
con.add(pal,BorderLayout.NORTH);
con.add(pa,BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
try{
URL url=new URL(te.getText());
content.setPage(url);
}catch(MalformedURLException el){
System.out.println(e.toString());
}catch(IOException el){
JOptionPane.showMessageDialog(frame, "连接错误");
}
}
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try{
URL url=e.getURL();
content.setPage(url);
te.setText(e.getURL().toString());
}catch(MalformedURLException el){
System.out.println(e.toString());
}catch(IOException el){
JOptionPane.showMessageDialog(frame, "连接错误");
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Ex9();
// try{
// InetAddress ad=InetAddress.getByName("www.baidu.com");
// System.out.println("IP地址为:"+ad.getHostAddress());
// System.out.println("本机IP为:"+InetAddress.getLocalHost());
// InetAddress ad1=InetAddress.getByName("172.16.210.28");
// System.out.println("主机名为:"+ad1.getHostName());
// }catch(UnknownHostException el){
// el.printStackTrace();
// }
}
}