1.java åQQ
2.跪求一个Java编写的多人聊天程序源代码
java åQQ
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import java.awt.Button;
import java.awt.Panel;
import java.lang.String;
import javax.swing.JOptionPane;
public class QQ extends Frame implements ActionListener{
Label Lname ,Lpassword; //å®ä¹ç¨æ·ååå¯ç çæ ç¾
TextField Tname; //å®ä¹ç¨æ·åçææ¬æ¡
JPasswordField Tpassword; //å®ä¹å¯ç çææ¬æ¡ã注ï¼ä½¿ç¨JPasswordFieldç好å¤å°±æ¯è¾å ¥çå¯ç æ¯ä»¥****æ¾ç¤ºç»ç¨æ·ç
Panel panel1=new Panel(); //å®ä¹é¢æ¿
Button Load,login; //å®ä¹ç»éå注åçæé®
boolean b=false;
File file1; //å®ä¹è®°å½ç¨æ·æ³¨åçç¨æ·ååå¯ç çæ件夹
Writer writer;
String []Usersname=new String[]; //å®ä¹è®°å½ç¨æ·åçæ°ç»
String []Userspassword=new String[];//å®ä¹è®°å½å¯ç çæ°ç»
public QQ(){
Lname=new Label ("ç¨æ·å");Lname.setBackground(Color.red);//设置æ ç¾çèæ¯é¢è²
Lpassword=new Label ("å¯ç ");Lpassword.setBackground(Color.red);//设置æ ç¾çèæ¯é¢è²
Tname=new TextField ();
Tpassword=new JPasswordField ();
Load=new Button("ç»é");Load.setBackground(Color.lightGray);
login=new Button("注å");login.setBackground(Color.lightGray);
add(panel1,BorderLayout.CENTER);
setLayout(new CardLayout(3,2));
panel1.add(Lname);
panel1.add(Tname);
panel1.add(Lpassword);
panel1.add(Tpassword);
panel1.add(Load);Load.addActionListener(this);
panel1.add(login);login.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
// ç¨æ·ç»å½äºä»¶å¤ç
if (e.getSource() == Load) {
String name1 = Tname.getText();
String password1 = Tpassword.getText();
boolean flag = false;
int index = 0;
for (int i = 0; i < Usersname.length; i++) {
if (name1.equals(Usersname[i])) {
flag = true;
index = i;
break;
}
}
if (flag && password1.equals(Userspassword[index])) {
//å¤çç¨æ·ç»å½æååçåé¦ä¿¡æ¯
JOptionPane.showMessageDialog(null, "æåæ¨ç»éæå!", "æ¶æ¯", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "对ä¸èµ·æ¨çç¨æ·åæå¯ç é误!", "é误",JOptionPane.ERROR_MESSAGE);
}
}
// ç¨æ·æ³¨å模åçäºä»¶å¤ç
if (e.getSource() == login) {
String NCmp = Tname.getText();
String PCmp = Tpassword.getText();
boolean flag = false;
for (int i = 0; i < Usersname.length; i++) {
if (NCmp.equals(Usersname[i])) {
flag = true;
break;
}
}
if (flag) {
JOptionPane.showMessageDialog(null, "对ä¸èµ·æ¨çç¨æ·åå·²ç»æ³¨å!", "é误", JOptionPane.ERROR_MESSAGE);
} else {
int index = 0;
JOptionPane.showMessageDialog(null, "注åæå!", "æ¶æ¯", JOptionPane.INFORMATION_MESSAGE);
for (int i = 0; i < Usersname.length; i++) {
if (Usersname[i] == null) {
Usersname[i] = NCmp;
index = i;
break;
}
}
Userspassword[index] = PCmp;
}
try{
file1=new File("Student.dat");
FileWriter fw=new FileWriter(file1,true);
fw.write("ç¨æ·å"+"\t\t"+"å¯ç "+"\n");
fw.write(NCmp+"\t\t"+PCmp+"\n");
fw.close();
}catch (IOException e1){
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame Fstudent=new QQ();
Fstudent.setSize(,);
Fstudent.setTitle("QQ仿çç³»ç»");
// å ³éçªå£
Fstudent.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Fstudent.setVisible(true);
}
}
èªå·±åç åµåµä¸å¥½ææ 主è¦åè½å°±æ¯ç»é å注å
注åæç¨æ·åååæ¶ä¼æ示ï¼æ¥éï¼
ä½ è¿è¡ä¸ å°±å¯ä»¥äº
ä¸å¥½è¿å¯ä»¥èç³»åµåµ
è¿ä¸ªä¸è¥¿ è¿æ¯èªå·±å»æ¸ç´¢å§ ç½ä¸çèµæå¾å¤ è¦ç¥é ä½ å¦æççå软件å¼åçè¯ ä½ å°±è¦å»èªå¦å¾å¤ä¸è¥¿ èä¸æ¯ å»å¼ç°æç å¦ä¼å¥½å¥½ ç¾åº¦å§ èä¸æ¯æé®
跪求一个Java编写的多人聊天程序源代码
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Server
{
public Server()
{
try
{
ServerSocket s=new ServerSocket();
Socket ss=s.accept();
OutputStream out=ss.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
InputStream in=ss.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已经连接到服务器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"
+"你的链接端口:"+ss.getLocalPort()+"\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
//接受客户端信息
class ReadMessage extends Thread
{
private DataInputStream din;
public ReadMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while (true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"客户端说:"+str);
if (str.equals("bye"))
{
System.out.println("客户端下线!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// 发出服务器信息
class SendMessage extends Thread
{
private DataOutputStream dout;
public SendMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
InputStreamReader inr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inr);
String str;
try
{
while(true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("服务器退出!咸鱼源码搭建教程");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
class Client
{
public Client()
{
try
{
Socket s=new Socket("..1.2",10010100源码);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("服务器你好!我是1010101010源码客户端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
class ReaderMessage implements Runnable
{
private DataInputStream din;
public ReaderMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while(true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"服务器说:"+str);
if (str.equals("bye"))
{
System.out.println("服务器已经关闭,此程序自动退出!docway 源码");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable
{
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
String str;
InputStreamReader inf=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inf);
try
{
while (true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("客户端自己退出!sauerbraten源码");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}