day24/MyIE2.java

/*
客户端:自定义
服务端:tomcat服务器

需求:
自定义一个客户端,向tomcat服务器发请求,要求浏览demo.html页面。
自定义客户端用到了图形化界面。


URL类封装了Socket类。
传输层--->应用层


*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIE2 
{
	private Frame f;
	private Button but;
	private TextField tf;
	private TextArea ta;
	MyIE2()
	{
		init();
	}
	public void init()
	{
		f=new Frame("IE浏览器");
		f.setBounds(300,150,600,500);//坐标x y x y
		f.setLayout(new FlowLayout());

		tf=new TextField(50);
		f.add(tf);

		but=new Button("转到");
		f.add(but);

		ta=new TextArea(25,60);//25行,60列
		f.add(ta);

		myEvent();
		
		f.setVisible(true);

	}

	private void myEvent()
	{
		tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				try
				{
					if(e.getKeyCode()==KeyEvent.VK_ENTER)
					showDir();
				}
				catch (Exception ex)
				{
				}
				
			}
		});

		but.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					showDir();
				}
				catch (Exception ex)
				{
				}
			}
		});

		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}

	public void showDir()throws Exception
	{
		//ta.setText("");
		String urlInfo = tf.getText();//http://192.168.1.101:8080/myweb/demo.html
		
		URL url = new URL(urlInfo);

		URLConnection conn = url.openConnection();

		InputStream in = conn.getInputStream();

		byte[] buf = new byte[1024];
		int len=0;
		while((len=in.read(buf))!=-1)
		{
			ta.setText(new String(buf,0,len));
		}
		

	}






	public static void main(String[] args) throws Exception
	{
		new MyIE2();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值