//TCP-自定义图形界面浏览器访问tomcat服务器
import java.awt.*;//GUI
import java.awt.event.*;//监听事件
import java.io.*;
import java.net.*;

class  MyIEByGUI
{
    private Frame f;//窗体
    private TextField tf;//小文本 单行
    private Button but;//转到按钮
    private TextArea ta;//大文本 多行

    private Dialog d;//对话框
    private Label lab;//标签
    private Button okBut;//对话框按钮

    MyIEByGUI()
    {
        init();//初始化方法
    }
    public void init()//初始化方法
    {
        f = new Frame("my window");
        f.setBounds(300,100,600,500);//边界 setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度
        f.setLayout(new FlowLayout());//流式布局
        tf = new TextField(60);
        but = new Button("转到");

        ta = new TextArea(25,70);

        d = new Dialog(f,"提示信息-self",true);//
        d.setBounds(400,200,240,150);
        d.setLayout(new FlowLayout());
        lab = new Label();//Label是空标签
        okBut = new Button("确定");

        d.add(lab);//Label 对象是一个可在容器中放置文本的组件。一个标签只显示一行只读文本。文本可由应用程序更改,但是用户不能直接对其进行编辑。
        d.add(okBut);

        f.add(tf);
        f.add(but);
        f.add(ta);

        myEvent();//监听事件的集合
        f.setVisible(true);//窗体保持显示状态
    }
    private void  myEvent()//监听事件的集合
    {

        okBut.addActionListener(new ActionListener()//监听对话框确定按钮 动作监听器 键盘 鼠标可用
        {
            public void actionPerformed(ActionEvent e)
            {
                d.setVisible(false);
            }
        });

        d.addWindowListener(new WindowAdapter()//对话框窗口适配器 没看出来这个对话框有什么用
        {
            public void windowClosing(WindowEvent e)//窗口关闭事件
            {
                d.setVisible(false);
            }
        });

        tf.addKeyListener(new KeyAdapter()//单行文本框tf 的 按键适配器
        {
            public void keyPressed(KeyEvent e)//监听键盘
            {
                try
                {
                        if(e.getKeyCode()==KeyEvent.VK_ENTER)//键盘按下ENTER
                    showDir();//调用showDir()
                }
                catch (Exception ex)
                {
                }

            }
        });

        but.addActionListener(new ActionListener()//转到按钮but 动作监听器 键盘 鼠标可用
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    showDir();//调用showDir()
                }
                catch (Exception ex)
                {
                }       

            }
        });

        f.addWindowListener(new WindowAdapter()//窗体f适配器
        {
            public void windowClosing(WindowEvent e)//窗口事件 关闭
            {
                System.exit(0); //窗口事件 关闭
            }
        });
    }

    private void showDir()throws Exception
    {
        ta.setText("");//首先清空ta文本框
        String url = tf.getText();//http://192.168.1.254:8080/myweb/demo.html//从文本框得到URL

        int index1 = url.indexOf("//")+2;//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

        int index2 = url.indexOf("/",index1);//语法 stringObject.indexOf(searchvalue,fromindex) 参数描述 searchvalue 必需,第二个参数表示从指定位置开始查找

        String str = url.substring(index1,index2);
        String[] arr = str.split(":");//split切割方法,按指定参数切割成为字符串数组
        String host = arr[0];  
        int port = Integer.parseInt(arr[1]);

        String path = url.substring(index2);//substring()方法参数里表示从指定的下标开始后面的全部字符串
        //ta.setText(str+"...."+path);

        Socket s = new Socket(host,port);

        PrintWriter out = new PrintWriter(s.getOutputStream(),true);

        out.println("GET "+path+" HTTP/1.1");//浏览器需要的信息格式
        out.println("Accept: */*");
        out.println("Accept-Language: zh-cn");
        out.println("Host: 192.168.1.254:11000");
        out.println("Connection: closed");

        out.println();//空2行表示请求头和请求体
        out.println();

        BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));//读取返回信息
        String line = null;
        while((line=bufr.readLine())!=null)
        {
            ta.append(line+"\r\n");//返回信息显示在ta文本区里面
        }
        s.close();

    }

    public static void main(String[] args) 
    {
        new MyIEByGUI();//实例化GUI
    }
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐