?? 第二十一章例子.txt
字號:
21-例子1
import java.applet.*;import java.awt.*;
import java.awt.event.*;import java.net.*;
public class Example21_1 extends Applet implements ItemListener
{ Choice choice1;TextField text1,text2;
URL url1,url2,url3,url4,url5,url6;
String s1,s2,s3,s4,s5,s6;
public void init()
{ setLayout(new GridLayout(2,2));
choice1=new Choice();
text1=new TextField(10); text2=new TextField(10);
choice1.add("清華大學");choice1.add("大連理工");
choice1.add("yahoo"); choice1.add("263");
add(choice1);add(new Label("選擇一個網(wǎng)址") );add(text1); add(text2);
choice1.addItemListener(this);
s1="http://www.tsinghua.edu.cn";
s2="http://www.dlut.edu.cn";
s3="http://www.yahoo.com";
s4="http://freemail.263.net";
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==choice1)
{
if(choice1.getSelectedIndex()==0)
{ try
{url1=new URL(s1);
}
catch(MalformedURLException g)
{ System.out.println("不正確的URL:"+url1);
}
text1.setText("清華大學") ;
getAppletContext().showDocument(url1);
}
else if(choice1.getSelectedIndex()==1)
{ try
{url2=new URL(s2);
}
catch(MalformedURLException g)
{ System.out.println("不正確的URL:"+url2);
}
text1.setText("大連理工") ;
getAppletContext().showDocument(url2);
}
else if(choice1.getSelectedIndex()==2)
{ try
{url3=new URL(s3);
}
catch(MalformedURLException g)
{ System.out.println("不正確的URL:"+url3);
}
text1.setText("yahoo") ;
getAppletContext().showDocument(url3);
}
else if(choice1.getSelectedIndex()==3)
{ try
{url4=new URL(s4);
}
catch(MalformedURLException g)
{ System.out.println("不正確的URL:"+url4);
}
text1.setText("263") ;
getAppletContext().showDocument(url4);
}
}
else {}
}
}
21-例子2
import java.applet.*;import java.awt.*;
import java.awt.event.*;import java.net.*;
public class Example21_2 extends Applet implements ActionListener
{ Choice choice; Button button;
URL url; TextField text; String s1,s2,s3,s4;
public void init()
{ setLayout(new GridLayout(2,2));
text=new TextField(10); button=new Button("確定");
choice=new Choice();
s1="http://www.tsinghua.edu.cn";
s2="http://www.dlut.edu.cn";
s3="http://www.yahoo.com"; s4="http://cctv.com";
choice.add(s1); choice.add(s2);
choice.add(s3); choice.add(s4);
add(choice);add(button ); add(text); add(new Label());
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{ try
{url=new URL(choice.getSelectedItem());
text.setText("請等待.....");
}
catch(MalformedURLException g)
{ System.out.println("不正確的URL:"+url);
}
getAppletContext().showDocument(url);
}
else
{ }
}
}
21-例子3
import java.applet.*;import java.awt.*;
import java.awt.event.*;import java.net.*;
public class Example21_3 extends Applet implements ActionListener
{ Button button;
URL url;
TextField text;
public void init()
{ text=new TextField(18);
button=new Button("確定");
add(new Label("輸入網(wǎng)址:"));add(text); add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{ try
{url=new URL(text.getText().trim());//trim()的作用是去掉字符串的前后空格
}
catch(MalformedURLException g)
{ text.setText("不正確的URL:"+url);
}
getAppletContext().showDocument(url);
}
}
}
21-例子4
(1)客戶端程序:
import java.io.*;
import java.net.*;
public class Client
{ public static void main(String args[])
{ String s=null;Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{
mysocket=new Socket("localhost",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("你好!");//通過 out向"線路"寫入信息。
while(true)
{ s=in.readUTF();//通過使用in讀取服務器放入"線路"里的信息
if (s!=null) break;
}
mysocket.close();
}
catch(IOException e){System.out.println("無法連接");}
System.out.println(s);
}
}
(2)服務器端程序:
import java.io.*;import java.net.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Socket you=null;String s=null;
DataOutputStream out=null;DataInputStream in=null;
try{ server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("ERRO:"+e1);}
try{ you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true)
{s=in.readUTF();// 通過使用in讀取客戶放入"線路"里的信息
if (s!=null) break;
}
out.writeUTF("你好:我是服務器");//通過 out向"線路"寫入信息
you.close();
}
catch (IOException e)
{System.out.println("ERRO:"+e);}
}
}
21-例子5
(1)客戶端程序
import java.net.*;
import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class ChatRoom extends Applet implements Runnable,ActionListener
{ Button button;TextField text1;TextArea text2;
Socket socket;
DataInputStream in;
DataOutputStream out;
Thread thread;
public void init()
{setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
button=new Button("send");text1=new TextField(12);
text2=new TextArea();
Panel p=new Panel();p.add(text1);p.add(button);
add("Center",text2);add("South",p);
button.addActionListener(this);
}
public void start()
{ try
{socket = new Socket(this.getCodeBase().getHost(), 4331);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if (thread == null)
{thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run()
{String s1=null;
while(true)
{ try{s1=in.readUTF();// 通過使用in讀取服務器放入"線路"里的信息
}
catch (IOException e) {}
if(s1.equals("bye"))
{try{socket.close();break;}
catch (IOException e) {}
}
text2.append(s1+"\n");
}
}
public void actionPerformed(ActionEvent e)
{if (e.getSource()==button)
{ String s=text1.getText();
if(s!=null)
{ try{out.writeUTF(s);}
catch(IOException e1){}
}
else
{ try{out.writeUTF("請說話");}
catch(IOException e1){}
}
}
}
}
(2)服務器端程序
import java.io.*;import java.net.*;
import java.util.*;
public class ServerTwo
{
public static void main(String args[])
{ ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);}
catch(IOException e1) {System.out.println("正在監(jiān)聽"+"ERRO:"+e1);}
try{ you=server.accept();}
catch (IOException e)
{System.out.println("正在等待客戶");}
if(you!=null)
{new Server_thread(you).start(); }
else {continue;}//繼續(xù)等待客戶的呼叫
}
}
}
class Server_thread extends Thread
{ Socket socket;
DataOutputStream out=null;DataInputStream in=null;
String s=null;
Server_thread(Socket t)
{ socket=t;
try {in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{}
}
public void run()
{ while(true)
{ try{s=in.readUTF();// 通過使用in讀取客戶放入"線路"里的信息
}
catch (IOException e) {System.out.println("ERRO:"+e);}
try {if(s.equals("bye"))
{out.writeUTF(s);socket.close() ; }//客戶以離開
else{ try{out.writeUTF("我是服務器你對我說:"+s);}
//通過 out向"線路"寫入信息
catch (IOException e) {}
}
}
catch (IOException e) {}
}
}
}
21-例子6
(1) 客戶端程序
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Computer_client extends Applet
implements Runnable,ActionListener
{ Button 計算;TextField 輸入三邊長度文本框,計算結(jié)果文本框;
Socket socket=null;
DataInputStream in=null; DataOutputStream out=null;
Thread thread;
public void init()
{setLayout(new GridLayout(2,2));
Panel p1=new Panel(),p2=new Panel();
計算=new Button(" 計算");
輸入三邊長度文本框=new TextField(12);計算結(jié)果文本框=new TextField(12);
p1.add(new Label("輸入三角形三邊的長度,用逗號或空格分隔:"));
p1.add( 輸入三邊長度文本框);
p2.add(new Label("計算結(jié)果:"));p2.add(計算結(jié)果文本框);p2.add(計算);
計算.addActionListener(this);
add(p1);add(p2);
}
public void start()
{ try
{socket = new Socket(this.getCodeBase().getHost(), 4331);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if (thread == null)
{thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -