⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initial.java

📁 这是一个简单的聊天程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
gbc.gridx=i;gbc.gridy=j;
gbc.gridwidth=k;gbc.gridheight=r;
gbl.setConstraints(com,gbc);
jp.add(com);
}
public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==o1)
{
String n=new String(t2.getPassword());
String m=new String(t3.getPassword());
String l=t1.getText().trim();
String a,b,c,d;
if(l.length()<6)
{
JOptionPane.showMessageDialog(null,"用户名长度不能小于6");
a="0";
}
else a="1";
if(n.length()<6)
{
JOptionPane.showMessageDialog(null,"密码长度不能小于6");
b="0";
}
else b="1";
if(!n.equals(m))
{
JOptionPane.showMessageDialog(null,"你输入的密码前后不一致");
c="0";
}
else c="1";
if(a.equals("1")&&b.equals("1")&&c.equals("1"))
{
d="1"; 
JOptionPane.showMessageDialog(null,"ok 已发送.....");

}
else d="0";
if(d.equals("1"))
{
document doc=new document();
doc.name=t1.getText().trim();
doc.pwd=new String(t2.getPassword());
try
{
ObjectOutputStream oos=new ObjectOutputStream(sk.getOutputStream());
oos.writeObject(doc);
System.out.println("427");
}
catch(Exception e){System.out.println("420");}

}
}
if(o==o2)
{
new dl(ip);
dispose();
}
}

};//////////////////////////////////////////////////////////
class chat extends JFrame implements Runnable,ActionListener,WindowListener,ListSelectionListener,KeyListener///////////聊天类
{
JPanel jp;
TextArea tao,tai;
JButton jb,jb_q,jb_f;
Socket sk,sks,sokv,sokve,skv;////////////++++++*/
String ip; 
Thread t;
Date d;
JLabel jl;
Icon i;
JTextField jtf;
ServerSocket ss,ssv,ssve;/////++++++++*/
Vector v,ve;//////////////++++++
JList jlist;/////////++++++
String a,test,testname;
JList jtlis;
int b;
int key;
// boolean c;

public chat(String ip)
{
super("客户端聊天室");
this.ip=ip;
jp=new JPanel();
setContentPane(jp);
jp.setBackground(new Color(27,228,163));
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addKeyListener(this);

tao=new TextArea("",18,30,tao.SCROLLBARS_BOTH);
tao.setEditable(false);
tao.setBackground(new Color(190,250,231));
tai=new TextArea("",7,30,tao.SCROLLBARS_BOTH);

jb=new JButton("发送");
jb.setMnemonic('f');
jb.setBackground(new Color(151,225,198));
jb.addActionListener(this);

jb_q=new JButton("清空");
jb_q.setMnemonic('x');

jb_q.setBackground(new Color(151,225,198));
jb_q.addActionListener(this);
jb_f=new JButton("私聊");
jb_f.setBackground(new Color(151,225,198));
jb_f.addActionListener(this);
jtf=new JTextField(20);
i=new ImageIcon("f:/04912111/project/logowin.gif");
jl=new JLabel(i);
this.addWindowListener(this);
v=new Vector();

v.addElement("1123");
ve=new Vector();
ve.addElement("223");
jlist=new JList(v);
jtlis=new JList(ve);
jlist.addListSelectionListener(this);
//c=false;

jp.add(jl);
jp.add(tao);
jp.add(tai);
jp.add(jb);
jp.add(jb_q);
jp.add(jb_f);
jp.add(jtf);
jp.add(jlist);
jp.add(jtlis);

setSize(250,600);
setVisible(true);
setLocation(400,100);
setResizable(false);

try
{/////////////////////////////////////////////////
sk=new Socket(ip,1003);
new adjust();
jlist.setListData(v);
jtlis.setListData(ve);
ss=new ServerSocket(1004);
ssv=new ServerSocket(1005);////////////+++++++
ssve=new ServerSocket(1006);
System.out.print("every port ok");/////////////+++++
}
catch(Exception e)
{
}
t=new Thread(this);
t.start();
}
public void keyPressed(KeyEvent e)
{

key=e.getKeyCode();//////////////////////////////111111
if(key==KeyEvent.VK_ALT)////////////////keyevent.vk_alt/////////////////********
JOptionPane.showMessageDialog(null,"614");
send();

}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}

public void valueChanged(ListSelectionEvent lse)
{
a=jlist.getSelectedValue().toString();
b=jlist.getSelectedIndex();
JOptionPane.showMessageDialog(null,a+b);

int aaaaa=ve.size();
JOptionPane.showMessageDialog(null,""+b);
JOptionPane.showMessageDialog(null,""+aaaaa);
test=ve.elementAt(b).toString();
testname=v.elementAt(b).toString();
// JOptionPane.showMessageDialog(null,test+"111");
try
{
skv=new Socket(test,1004);
System.out.println("test 1004 610");
}
catch(Exception e){}
/* finally
{
try
{
skv.close();
}
catch(Exception ee){JOptionPane.showMessageDialog(null,ee.getMessage());}
}*/
// a=null;
// b="";
jlist.setListData(v);
}

public void windowActivated(WindowEvent e) 
{
}
public void windowClosed(WindowEvent e) 
{
}
public void windowClosing(WindowEvent e)
{
try
{
System.out.println("611 excuate");
DataOutputStream dos=new DataOutputStream(sk.getOutputStream());
dos.writeUTF("输入错误");
sk.close();
}
catch(Exception ee){}
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
} 
public void run()
{
try
{
while(true)
{
sks=ss.accept();
new connect(sks);
sokv=ssv.accept();/////////+++++
new vc(sokv);
sokve=ssve.accept();////////+++++
new vec(sokve);
}
}
catch(Exception e){}
}
class connect extends Thread
{
Socket sks;
//String message;
public connect(Socket s)
{
sks=s;
this.start();
}
public void run()
{
DataInputStream dis;
try
{
while(true)
{
d=new Date();
String ft=ip+"to me";
String time=""+ft+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
dis=new DataInputStream(sks.getInputStream());
String mess=dis.readUTF();
tao.append(time+"\n"+mess+"\n");
}
}
catch(Exception e){}
finally
{
try
{
skv.close();
}
catch(Exception ee){JOptionPane.showMessageDialog(null,ee.getMessage());}
}

}
};
class adjust extends Thread
{
Date d;
public adjust()
{
this.start();
}
public void run()
{
try
{
while(true)
{
d=new Date();
jtf.setText(""+d);
this.sleep(1000);
}
}
catch(Exception e){}
}
}

public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
//if(c=false)
//{
if(o==jb)
{
//if(c=false)
// {
send();
tai.setText("");
// tai.requestFocus();
//}
////if(c=true)
//{
/* sendsc();
tai.setText("");
tai.requestFocus();
}*/
}
if(o==jb_f)
{
sendsc();
tai.setText("");
// tai.requestFocus();
}

if(o==jb_q)
{
tao.setText("");
//tai.requestFocus();
}

}
public void send()
{
DataOutputStream dos;
String toMess=tai.getText().trim();
if(toMess.equals(""))
{
//tai.requestFocus();
JOptionPane.showMessageDialog(null,"输入字符不能为空");
}
else
{
try
{
d=new Date();
String ft="Local to"+" "+ip;
String time=""+ft+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
tao.append(time+"\n"+toMess+"\n");
dos=new DataOutputStream(sk.getOutputStream());
dos.writeUTF(toMess);
}
catch(Exception e)
{
}
}

}

public void sendsc()
{
DataOutputStream dos;
String toMess=tai.getText().trim();
if(toMess.equals(""))
{
tai.requestFocus();
JOptionPane.showMessageDialog(null,"输入字符不能为空");
}
else
{
try
{
d=new Date();
String ft="悄悄话"+" "+testname;
String time=""+ft+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
tao.append(time+"\n"+toMess+"\n");
dos=new DataOutputStream(skv.getOutputStream());
dos.writeUTF(toMess);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}

}


}


class vc extends Thread/////////////++++++++1005 名称 v
{
Socket sokv;
String vis;
public vc(Socket s)
{
sokv=s;
this.start();
}
public void run()
{
//ObjectInputStream ois;
DataInputStream dis;
try
{
while(true)
{
//ois=new ObjectInputStream(sokv.getInputStream());
// Vector vi=(Vector)ois.readObject();
dis=new DataInputStream(sokv.getInputStream());
vis=dis.readUTF();
v.removeAllElements();
v.addElement(vis);
//v.addElement("服务器");
jlist.setListData(v);
}
}
catch(Exception e)
{
}
}
};
class vec extends Thread/////////////+++++++++++1006 ip ve
{
Socket sokve;
String vis;
public vec(Socket s)
{
sokve=s;
this.start();
}

public void run()
{
DataInputStream dis;
try
{
while(true)
{

dis=new DataInputStream(sokve.getInputStream());
vis=dis.readUTF();
ve.removeAllElements();
ve.addElement(vis);
//ve.addElement(ip);
jtlis.setListData(ve);

}
}

catch(Exception e)
{
}
}
};
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -