java简单浏览器

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();
//		}
	}

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值