📄 clientframe.java
字号:
//编制人员:秦星 学号:G0417015
//本程序为客户程序
//本程序具有接收和发送功能
package pack_chat;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;
public class clientFrame extends Applet
{
TextArea ta;
static TextField tf,tn;
Label l,l2;
Choice cs;
Button b2,b3,b4;
Panel p1,p2,ap;
Socket sl;
int mark=0;
Color mycolor,mycolor2;
InputStream slIn,in;
DataInputStream dis,din;
OutputStream slout,oot;
DataOutputStream dos,dot;
static Frame frame;
public static void run()
{
if(clientFrame.frame!=null)clientFrame.frame.setVisible(true);
else{
clientFrame cf=new clientFrame();
frame.add("Center",cf);
frame.setSize(500,300);
frame.validate();
frame.setVisible(true);
cf.start();
}
}
public clientFrame()
{
frame=new Frame("客户机");
frame.addWindowListener(new WindowControl());
ap=new Panel();
ap.setLayout(new BorderLayout());
ap.setSize(500,300);
ta=new TextArea("recieve massage ",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);//TextArea.SCROLLBARS_NONE
ta.setEditable(false);
mycolor=new Color(253,221,255);
ta.setBackground(mycolor);
ta.setForeground(Color.red);
tf=new TextField();
p1=new Panel();
ap.add(ta,"Center");
ap.add(p1,"South");
p1.setLayout(new BorderLayout());
p1.add(tf,"Center");
p2=new Panel();
ap.add(p2,"North");
l=new Label(" 主机IP地址:");
p2.add(l);
tn=new TextField();//获取Ip地址,也可以自己写入
try
{
String strr;
int i;
strr=InetAddress.getLocalHost().toString();
for(i=0;i<strr.length();i++)
{
if(strr.charAt(i)=='1')
break;
}
tn.setText(strr.substring(i));
}
catch(UnknownHostException e){};
mycolor2=new Color(167,217,254);
tn.setBackground(mycolor2);
p2.add(tn);
l2=new Label("选择端口");
p2.add(l2);
cs=new Choice();
cs.addItem("Client1(5555)");
//cs.addItem("Client2(4444)");
p2.add(cs);
b2=new Button("Connect to");
p2.add(b2);
b3=new Button("发送");
p1.add(b3,"East");
//b4=new Button("退出");
// p1.add(b4);
ap.setVisible(true);
add(ap,"Center");
}
//.........................................
public void init()
{
}
public void paint(Graphics g)
{
g.setFont(new Font("Helvetica",Font.BOLD+Font.ITALIC,20));
g.setColor(new Color(206,36,198));
g.drawString("Welcome to chat!!!",100,250);
}
public boolean action(Event evt,Object obj) //事件触发代码
{
if(evt.target instanceof Button)
{
String label=(String) obj;
if(label.equals("发送")) //如果点击连接后
{
client();
}
if(label.equals("Connect to"))
{
Thread t=new connection();
t.start();
}
}
return true;
}
public boolean client()
{
if(mark==1)
{
if(tf.getText().equals("")==true)
ta.append("\n"+"can't be sent without message!");
else
{
try
{
sl=new Socket(tn.getText(),443); //获IP地址
slout=sl.getOutputStream();
dos=new DataOutputStream(slout);
dos.writeUTF(tf.getText());
in=sl.getInputStream();
din=new DataInputStream(in);
//String st=new String(din.readUTF());
ta.append("\n"+"From Client:"+tf.getText());
din.close();
dos.close();
slout.close();
sl.close();
}
catch(IOException e)
{
ta.append("\n"+"Exception"+e);
return false;
}
}
return true;
}
else
{
ta.append("\n"+"please click 'Connect to' Button!!"+
"\n"+"please click 'Connect to' Button!!");
return false;
}
}
class connection extends Thread
{
public void run()
{
String str;int port;
str=cs.getSelectedItem();
port=Integer.parseInt(str.substring(8,12));
while(true)
{
try{
sl=new Socket(tn.getText(),port);
mark=1;
oot=sl.getOutputStream();
dot=new DataOutputStream(oot);
slIn=sl.getInputStream();
dis=new DataInputStream(slIn);
String st=new String(dis.readUTF());
ta.append("\n"+"From Host:"+st);
dot.writeUTF(st);
dis.close();
slIn.close();
sl.close();
}
catch(IOException e)
{
ta.append("\n"+"Try to connect!");
break;
}
}
}
}
class WindowControl extends WindowAdapter
{
public void windowClosing(WindowEvent e){
frame.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -