📄
字号:
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("选择一个网址") );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("输入网址:"));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("正在监听"+"ERRO:"+e1);}
try{ you=server.accept();}
catch (IOException e)
{System.out.println("正在等待客户");}
if(you!=null)
{new Server_thread(you).start(); }
else {continue;}//继续等待客户的呼叫
}
}
}
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 输入三边长度文本框,计算结果文本框;
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);计算结果文本框=new TextField(12);
p1.add(new Label("输入三角形三边的长度,用逗号或空格分隔:"));
p1.add( 输入三边长度文本框);
p2.add(new Label("计算结果:"));p2.add(计算结果文本框);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 + -