A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MUFCRyanCR7 中级黑马   /  2015-9-4 21:53  /  439 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
使用图形界面模拟浏览器,同时使用URL中的URLConnection类实现网络的连接
URLConnection是应用层的类,使用后会将位于传输层的信息(如http的应答消息头)拆包,
但是无法解析网页特有的编程语言,如HTML语言,此方法将Socket、URL等服务封装起来,可以
实现URL内容的自动获取和发送,此类一旦使用,就会连接指定网址,使用十分方便,一定要
熟练掌握并运用。
*/

package net.socket.tcp;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class GUIURL
{
        private Frame f;
        private Button b;
        private TextField tf;
        private TextArea ta;
        private Dialog d;
        private Button but;
        private Label lab;;
        GUIURL()
        {
                init();
        }
        public void init()
        {
                f = new Frame("窗体测试");
               
                f.setBounds(300,100,600,500);
                f.setLayout(new FlowLayout());               
                tf = new TextField(50);
                b = new Button("转到");
                ta = new TextArea(25,40);
                f.add(tf);
                f.add(b);
                f.add(ta);
                d = new Dialog(f,"错误提示信息",true);
                d.setBounds(500,300,300,100);
                d.setLayout(new FlowLayout());
                lab = new Label();
                but = new Button("确定");
                d.add(lab);
                d.add(but);
                myEvent();
                f.setVisible(true);
        }
       
        private void myEvent()
        {
                f.addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e)
                        {
                                System.exit(0);
                        }
                });
                //通过按钮“转到”实现目录输出功能及对话框弹出功能
                b.addActionListener(new ActionListener()
                {
                       
                        public void actionPerformed(ActionEvent e)
                        {
                                try
                                {
                                        showDir();
                                }
                                catch (Exception ex)
                                {
                                }
                                       
                        }
                });
                //实现对话框的窗口关闭功能
                d.addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e)
                        {
                                d.setVisible(false);
                        }
                });
                //实现对话框上的按钮“确定”的功能
                but.addActionListener(new ActionListener()
                {
                       
                        public void actionPerformed(ActionEvent e)
                        {
                                d.setVisible(false);       
                        }
                });
                //通过回车键实现按钮“转到”的功能
                tf.addKeyListener(new KeyAdapter()
                {
                        public void keyPressed(KeyEvent e)
                        {
                                if (e.getKeyCode()==KeyEvent.VK_ENTER)
                                {
                                        try
                                        {
                                                showDir();
                                        }
                                        catch (Exception ex)
                                        {
                                        }
                                       
                                }
                        }
                });
        }

        private void showDir() throws Exception
        {
                ta.setText("");
                String urlPath = tf.getText();
                URL url = new URL(urlPath);
                URLConnection conn = url.openConnection();
                System.out.println(conn);
                InputStream in = conn.getInputStream();
                byte[] buf = new byte[1024*1024*5];
                int line = in.read(buf);
                ta.append(new String(buf,0,line));       
                in.close();
        }

        public static void main(String[] args)
        {
                new GUIURL();
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马