请高手给这个程序写详细注解,并且希望有人帮忙将其连接数据库,帮忙弄好本地实现两个人的互聊…… 以下是代码
//登录界面:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Login extends JFrame implements ActionListener
{
TextField txtPort;
TextField txtIP;
TextField txtName;
JButton btnLogin;
JButton btnCancel;
Socket toServer;
BufferedReader in;
PrintStream out;
public Login(){
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
Label l1=new Label("port");
Label l2=new Label("IP");
Label l3=new Label("昵称");
txtPort=new TextField("9999",15);
txtIP=new TextField("192.168.0.12",15);
txtName=new TextField("神心",15);
p2.add(l1);
p2.add(txtPort);
p3.add(l2);
p3.add(txtIP);
p4.add(l3);
p4.add(txtName);
GridLayout g=new GridLayout(3,1);
p1.setLayout(g);
p1.add(p2);
p1.add(p3);
p1.add(p4);
p1.setBackground(new Color(250,150,200));
btnLogin=new JButton("登陆");
btnCancel=new JButton("取消");
btnLogin.addActionListener(this);
btnCancel.addActionListener(this);
BorderLayout border1=new BorderLayout();
this.setLayout(border1);
p5.add(btnLogin);
p5.add(btnCancel);
this.add(BorderLayout.SOUTH,p5);
this.add(BorderLayout.CENTER,p1);
this.setSize(350,250);
this.setTitle("聊天室用户登录");
this.setResizable(false);
this.setLocation(450,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//实现"登录"和"取消"按钮的易做图
public void actionPerformed(ActionEvent e){
JButton button = (JButton)e.getSource();
if(button.equals(btnCancel)){
dispose();//如果客户按了"取消"按钮,那么登录窗口就关闭
}
else {//如果客户按了"登录",那么就连接服务器
LoginStart ls = new LoginStart();
ls.start();
this.dispose();
}
}//end of actionPerformed()
class LoginStart extends Thread
{
public void run(){
int port = Integer.parseInt(txtPort.getText());//取得端口号
String ip = txtIP.getText();//取得要连接的服务器的IP地址
String name = txtName.getText();//取得客户姓名
try {
toServer = new Socket(ip,port); //连接服务端 socket("主机名",端口号);
in = new BufferedReader(new InputStreamReader(toServer.getInputStream())); //建立输入流
out = new PrintStream(toServer.getOutputStream());//输出流
//out.print(name+"\n");
new Client(out,in,name);//如果登录成功,则弹出客户端聊天窗口
//dispose(); //同时关闭登录窗口
}
catch (Exception e2) {
System.out.println("登录失败!");//如果登录失败,则打印出"登录失败",以告知客户
e2.printStackTrace();
}
}
}//end of LoginStart
public static void main(String[] args){
new Login();//启动登录窗口
}
}
--------------------编程问答-------------------- //聊天室客户端
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
class Client extends JFrame
{
PrintStream out; //输出流
BufferedReader in; //输入流
String name;
TextArea area = new TextArea();
TextArea msgA = new TextArea();
TextField frdNameT = new TextField(10);
List list =new List();
Panel p10=new Panel();
JButton sendButton = new JButton("发送");
JButton closeButton = new JButton("关闭");
JButton privateChatButton = new JButton("私聊");
JButton jb1 = new JButton("设置背景颜色");
JButton jb2 = new JButton("设置字体颜色");
JButton jb3 = new JButton("更改头像");
JButton jb4 = new JButton("添加表情");
JButton jb5 = new JButton(new ImageIcon("粗体.JPG"));
JButton jb6 = new JButton(new ImageIcon("斜体.JPG"));
Label lb1 = new Label("昵称:");
ImageIcon picture = new ImageIcon();
JLabel label = new JLabel(picture,SwingConstants.CENTER);
ImageIcon picture1 = new ImageIcon("1.JPG");
ImageIcon picture2 = new ImageIcon("2.JPG");
ImageIcon picture3 = new ImageIcon("3.JPG");
ImageIcon picture4 = new ImageIcon("4.JPG");
//ImageIcon picture5 = new ImageIcon("5.JPG");
//ImageIcon picture6 = new ImageIcon("6.JPG");
//ImageIcon picture7 = new ImageIcon("7.JPG");
ImageIcon picture8 = new ImageIcon("8.JPG");
//ImageIcon picture9 = new ImageIcon("9.JPG");
void init(){
this.pack();
/////设置几个面板////////
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
Panel p6=new Panel();
Panel p7=new Panel();
Panel p8=new Panel();
Panel p9=new Panel();
lb1.setBackground(Color.pink);
////////设置两个主要的文本框////////////
BorderLayout border1=new BorderLayout();
p1.setLayout(border1);
p1.add(BorderLayout.CENTER,area);
area.setEditable(false);
FlowLayout f2=new FlowLayout(FlowLayout.LEFT);
p3.setLayout(f2);
p3.add(jb1);
p3.add(jb2);
p3.add(jb3);
p3.add(jb4);
p3.add(jb5);
p3.add(jb6);
BorderLayout border2=new BorderLayout();
p5.setLayout(border2);
p5.add(BorderLayout.NORTH,p3);
p5.add(BorderLayout.CENTER,msgA);
p5.setBackground(Color.pink);
BorderLayout border3=new BorderLayout();
p7.setLayout(border3);
p7.add(BorderLayout.CENTER,p1);
p7.add(BorderLayout.SOUTH,p5);
FlowLayout fl=new FlowLayout(FlowLayout.RIGHT);
p8.setLayout(fl);
p8.add(new JLabel("friendName"));
p8.add(frdNameT);
p8.add(privateChatButton);
p8.add(sendButton);
p8.add(closeButton);
p8.setBackground(Color.pink);
BorderLayout border4=new BorderLayout();
p9.setLayout(border4);
p9.add(BorderLayout.CENTER,p7);
p9.add(BorderLayout.SOUTH,p8);
//窗口关闭
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
BorderLayout border5=new BorderLayout();
p10.setLayout(border5);
p10.add(BorderLayout.CENTER,list);
p10.add(BorderLayout.NORTH,label);
p10.setBackground(Color.pink);
//添加背景颜色的易做图
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JColorChooser chooser4=new JColorChooser();
Color color=chooser4.showDialog(Client.this,"我的调色盘",Color.yellow);
area.setBackground(color);
msgA.setBackground(color);
list.setBackground(color);
}
});
//添加字体颜色的易做图
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JColorChooser chooser4=new JColorChooser();
Color color=chooser4.showDialog(Client.this,"我的调色盘",Color.yellow);
area.setForeground(color);
msgA.setForeground(color);
list.setForeground(color);
}
});
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JButton label1 = new JButton(picture1);
label1.addActionListener(new Change1());
JButton label2 = new JButton(picture2);
label2.addActionListener(new Change2());
JButton label3 = new JButton(picture3);
label3.addActionListener(new Change3());
JButton label4 = new JButton(picture4);
label4.addActionListener(new Change4());
//JButton label5 = new JButton(picture5);
//label5.addActionListener(new Change5());
//JButton label6 = new JButton(picture6);
//label6.addActionListener(new Change6());
//JButton label7 = new JButton(picture7);
//label7.addActionListener(new Change7());
JButton label8 = new JButton(picture8);
label8.addActionListener(new Change4());
//JButton label9 = new JButton(picture9);
//label9.addActionListener(new Change9());
JFrame f=new JFrame();
GridLayout g=new GridLayout(1,5);
f.setResizable(false);
f.setLayout(g);
f.add(label1);
f.add(label2);
f.add(label3);
f.add(label4);
f.add(label8);
f.setSize(250,200);
f.setVisible(true);
}
});
BorderLayout border=new BorderLayout();
this.setLayout(border);
this.add(BorderLayout.NORTH,lb1);
this.add(BorderLayout.CENTER,p9);
this.add(BorderLayout.EAST,p10);
this.setTitle("聊天室客户端");
this.setSize(700,600);
this.setVisible(true);
sendButton.addActionListener(new ActionListener(){//"发送"按钮易做图
public void actionPerformed(ActionEvent e){
String msg = msgA.getText();
msgA.setText("");//清空输入文本域
out.println(msg);
}
});
msgA.addKeyListener(new KeyAdapter(){//键盘易做图---按下Enter发送信息
public void keyPressed(KeyEvent e){
int value = e.getKeyCode();
if(value == KeyEvent.VK_ENTER){
String msg = msgA.getText();
msgA.setText("");//清空输入文本域
out.println(msg);
}
}
});
closeButton.addActionListener(new ActionListener(){//"关闭"按钮易做图
public void actionPerformed(ActionEvent e){
System.exit(100);
}
});
jb5.addActionListener(new ActionListener(){//"粗体"按钮易做图
public void actionPerformed(ActionEvent e){
msgA.setFont(new Font("BOLD",Font.BOLD,15));
}
});
jb6.addActionListener(new ActionListener(){//"斜体"按钮易做图
public void actionPerformed(ActionEvent e){
msgA.setFont(new Font("ITALIC",Font.ITALIC,15));
}
});
}//end of init()
public Client(PrintStream out,BufferedReader in,String name) {//构造方法
this.out = out;
this.in = in;
this.name = name;
lb1.setText("昵称:"+ name);
init();//弹出客户端窗口
out.println(name);
receive();//开始与服务器通信
}
public void receive() { //接受服务端发来的信息。
try {
String text = null;
while ((text = in.readLine()) != null) {
this.area.append(text + "\n"); //在area上添加读入的内容
}
}
catch (IOException e) {
e.printStackTrace();// TODO Auto-generated catch block
}
finally{
try
{
in.close();
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
class Change1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
label.setIcon(picture1);
}
}
class Change2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
label.setIcon(picture2);
}
}
class Change3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
label.setIcon(picture3);
}
}
class Change4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
label.setIcon(picture4);
}
}
}
--------------------编程问答-------------------- 聊天室服务器
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Server extends JFrame {
private TextArea msgA = new TextArea();//服务器用来输入的文本域
private TextArea area = new TextArea();//服务器显示消息的文本域
private TextField portT = new TextField("9999");
private java.awt.List list = new java.awt.List();//在线客户列表
private ServerSocket server;//服务器
private Socket client;//客户端
private Map<String, Socket> container = new HashMap<String, Socket>();//HashMap
void init() {
/////设置几个面板////////
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Panel p5 = new Panel();
Panel p6 = new Panel();
Panel p7 = new Panel();
Panel p8 = new Panel();
Panel p9 = new Panel();
Panel p10 = new Panel();
///////////////////////////////////////////
////////设置两个主要的文本框////////////
Label lb1 = new Label("");
lb1.setBackground(new Color(200,200,250));
Label lb2 = new Label("port");
BorderLayout border1 = new BorderLayout();
p1.setLayout(border1);
p1.add(BorderLayout.CENTER, area);
BorderLayout border2 = new BorderLayout();
p5.setLayout(border2);
p5.add(BorderLayout.NORTH, p3);
p5.add(BorderLayout.CENTER, msgA);
BorderLayout border3 = new BorderLayout();
p7.setLayout(border3);
p7.add(BorderLayout.CENTER, p1);
p7.add(BorderLayout.SOUTH, p5);
p7.setBackground(new Color(250,150,200));
JButton startButton = new JButton("启动服务");
JButton sendButton = new JButton("发送");
JButton flushButton = new JButton("刷新列表");
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
p8.setLayout(fl);
p8.add(lb2);
p8.add(portT);
p8.add(startButton);
p8.add(sendButton);
p8.add(flushButton);
p8.setBackground(new Color(250,200,200));
BorderLayout border4 = new BorderLayout();
p9.setLayout(border4);
p9.add(BorderLayout.CENTER, p7);
p9.add(BorderLayout.SOUTH, p8);
p10.setLayout(new BorderLayout());
Label lb3 = new Label("在线用户");
lb3.setBackground(new Color(200,200,250));
p10.add(lb3,BorderLayout.NORTH);
p10.add(list,BorderLayout.CENTER);
area.setEditable(false);
BorderLayout border = new BorderLayout();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
--------------------编程问答-------------------- 聊天室服务器剩余代码
this.setLayout(border);
this.add(BorderLayout.NORTH, lb1);
this.add(BorderLayout.CENTER, p9);
this.add(BorderLayout.EAST, p10);
this.setTitle("聊天服务器 ");
this.setBounds(100, 100, 600, 550);
this.setVisible(true);
//"启动服务"按钮易做图
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Start();
t.start();
}
});//启动按钮易做图----end
//发送按钮易做图
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg = msgA.getText();
if (msg != null && msg != "") {//如果消息不为空
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
broadcast(time + "[系统消息]: " + msg);//那么广播这条系统消息
area.append(time + "[系统消息]: " + msg + "\n");
msgA.setText("");
}
}
});//end
//刷新按钮易做图
flushButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (list != null) {
list.removeAll();//清空在线用户列表,以便刷新
}
Set<Map.Entry<String, Socket>> mySet = container.entrySet();
for (Iterator<Map.Entry<String, Socket>> it = mySet.iterator(); it.hasNext();) {//刷新列表
Map.Entry<String, Socket> me = it.next();
String aClientName = me.getKey();
Socket aClient = me.getValue();
String aClientAddress = aClient.getInetAddress().getHostAddress();
list.add(aClientName + "----" + aClientAddress);
}//end of for
}//end of actionPerformed
});//end
msgA.addKeyListener(new KeyAdapter(){//键盘易做图---按下Enter发送信息
public void keyPressed(KeyEvent e){
int value = e.getKeyCode();
if(value == KeyEvent.VK_ENTER){
String msg = msgA.getText();
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
broadcast(time + "[系统消息]: " + msg);//那么广播这条系统消息
area.append(time + "[系统消息]: " + msg + "\n");
msgA.setText("");
}
}
});
}//end of init()
class Start extends Thread //启动服务器的线程
{
public void run() {
try {
if(server != null && !server.isClosed()){
area.append("警告!服务器已在启动中!\n");
}
else{
server = new ServerSocket(Integer.parseInt(portT.getText()));//启动服务器
area.append((new Date()).toString() +'\n'+ "[恭喜!^_^]"+"服务器启动成功!" + "\n\n");
}
while(true){
client = server.accept();
Thread sgS = new SgService(client);//为每位登录的客户创建一个线程
sgS.start();//启动客户线程
}
}
catch (Exception e) {
System.out.println("cuowu 1");
e.printStackTrace();
}
}//end of run()
} //end of Start
//处理单个客户的线程
class SgService extends Thread//SgService---begin
{
private Socket client = null;//客户套接字
private BufferedReader br = null;//输入流
private PrintStream ps = null;
private int counter = 0;//记录是第几次接受用户的信息
private int mark = 0;//记录用户说不文明用语的次数
private String name = null;
SgService(Socket client) {//构造方法
this.client = client;
}
public void run() {//run()----begin
try {
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
ps = new PrintStream(client.getOutputStream());
String msg = null;
while ((msg = br.readLine()) != null) {//无限循环,意味着可以和客户进行无限次交互
if(counter == 0){
name = msg;
container.put(name,client);
list.add(name + "----" + client.getInetAddress().getHostAddress());
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = hh + ":" + mm + ":" + ss + " ";
broadcast(time + name + "上线了!");
area.append(time + name + "上线了!\n\n");
counter ++;
}
else{
if(msg.equals("你是个笨蛋")){
mark = mark + 1;
if(mark < 3){
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
singleCom(time +"[系统消息]: 警告!请注意文明用语!" ,client);
//在服务器端显示这个用户说不文明语言的次数
area.append(time + "[系统消息]: " + name +"第" + mark+ "次使用了不文明用语,特此警告!");
}
else{//如果客户第三次说不文明的话,系统就把他踢出聊天室
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
if(mark == 3){
singleCom(time + "[系统消息]: 由于您多次使用了不文明用语,经警告无效,现已被踢出聊天室!" ,client);//告诉他本人
}
removeClient(name,client);//从系统列表中删除这个人的信息
//并向所有人广播这一消息
broadcast(time + "[系统消息]: " + name + "由于多次使用了不文明用语,经警告无效,现已被踢出聊天室!");
area.append(time + "[系统消息]: " + name + "由于多次使用了不文明用语,经警告无效,现已被踢出聊天室!\n");
}
//broadcast("[系统消息]: " + name + "由于使用了不文明用语,已被踢出聊天室!");
//area.append("[系统消息]: " + name + "由于使用了不文明用语,已被踢出聊天室!\n");
//list.remove(name + "----" + client.getInetAddress().getHostAddress());
//container.remove(name);
}
else{
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
area.append(time + name+"说:"+msg + "\n\n");//将客户发送的消息显示在服务器的窗口中
broadcast(time + name+"说:"+msg+"\n");//同时向每个在线客户广播这则消息
}
}
}//end of while
}
catch (Exception ex) {
Calendar ca = new GregorianCalendar();
int hh = ca.get(Calendar.HOUR_OF_DAY);
int mm = ca.get(Calendar.MINUTE);
int ss = ca.get(Calendar.SECOND);
String time = new String(hh + ":" + mm + ":" + ss + " ");
area.append(time + name + "下线了!\n");
broadcast(time + name + "下线了!\n");
list.remove(name + "----" + client.getInetAddress().getHostAddress());
container.remove(name);
}
}//run()---end
}//SgService-----end
void removeClient(String name,Socket client){//踢人
list.remove(name + "----" + client.getInetAddress().getHostAddress());
container.remove(name);
}
//广播
void broadcast(String msg) {//broadcast()-----begin
Set<Map.Entry<String, Socket>> mySet = container.entrySet();
Iterator<Map.Entry<String, Socket>> it = mySet.iterator();
String message = msg;
while (it.hasNext()) {//如果列表中还有客户
Map.Entry<String, Socket> me = it.next();
Socket tempClient = me.getValue();
singleCom(message, tempClient);//就把消息向发给这个客户
}
}//broadcast()-----end
//向单个客户发送消息
private void singleCom(String msg, Socket aClient) {//singleCommunication------begin
try {
PrintStream pw = new PrintStream(aClient.getOutputStream());
String message = msg;
pw.println(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}//singleCommunication----end
public static void main(String[] args) {
new Server().init(); //启动服务器窗口
System.out.println("服务器:");
}
}//end of Server
补充:Java , Eclipse