自定义简单 IE 1.采用 简单的GUI
自定义IE2,采用简单的GUI
import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class MyIEByGUI { private Frame f; private TextField tf; private Button btn; private TextArea ta; private Dialog d; private Label lab; private Button okbtn; MyIEByGUI(){ init(); } private void init(){ f=new Frame("my Window"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf=new TextField(60); btn=new Button("转到"); ta=new TextArea(25,70); d=new Dialog(f,"提示信息-self",true); d.setBounds(400,200,250,150); d.setLayout(new FlowLayout()); lab=new Label(); okbtn=new Button("确定"); d.add(lab); d.add(okbtn); f.add(tf); f.add(btn); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { //添加 回车 时候 自动转到 tf.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode()==KeyEvent.VK_ENTER){ showDir(); } } }); //确定按钮事件 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); d.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showDir(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); } private void showDir() { try{ String url=tf.getText(); //http://192.168.1.121:10000/myweb/demo.html int index=url.indexOf("//")+2; int index2=url.indexOf("/",index); String str=url.substring(index,index2);//192.168.1.121:10000 String[] arr=str.split(":"); //192.168.1.121 10000 String host=arr[0]; int prot=Integer.parseInt(arr[1]); String path=url.substring(index2); //myweb/demo.html Socket s=new Socket(host,prot); 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: "+str); //out.println("Connection: Keep-Alive"); 请求完后 还保持连接,直到 请求超时 out.println("Connection: closed"); //请求完后,连接就关闭 out.println(); //结束头部请求 BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream())); String line=null; while((line=bufr.readLine())!=null){ ta.append(line+"\r\n"); } s.close(); tf.setText(""); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new MyIEByGUI(); } }
自定义IE2,采用简单的GUI
import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.URL; import java.net.URLConnection; public class MyIEByGUI1 { private Frame f; private TextField tf; private Button btn; private TextArea ta; private Dialog d; private Label lab; private Button okbtn; MyIEByGUI1(){ init(); } private void init(){ f=new Frame("my Window"); f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); tf=new TextField(60); btn=new Button("转到"); ta=new TextArea(25,70); d=new Dialog(f,"提示信息-self",true); d.setBounds(400,200,250,150); d.setLayout(new FlowLayout()); lab=new Label(); okbtn=new Button("确定"); d.add(lab); d.add(okbtn); f.add(tf); f.add(btn); f.add(ta); myEvent(); f.setVisible(true); } private void myEvent() { //添加 回车 时候 自动转到 tf.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode()==KeyEvent.VK_ENTER){ showDir(); } } }); //确定按钮事件 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); d.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub d.setVisible(false); } }); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showDir(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); } private void showDir() { try{ String urlTest=tf.getText(); //http://192.168.1.121:10000/myweb/demo.html URL url=new URL(urlTest); URLConnection conn= url.openConnection(); // 在内部帮我们进行 socket 连接 InputStream in=conn.getInputStream(); byte[] buf=new byte[1024]; int len=in.read(buf); ta.append(new String(buf,0,len)); tf.setText(""); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new MyIEByGUI1(); } }