当然这两个直接运行不了,还必须下那上一个当中的两个项目包
这个是聊天软件客户端的精华:
package passion;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class Chat_client extends javax.swing.JFrame {
private int SendPort;
private int RecvPort;
private DatagramSocket Client ;
private DatagramSocket Server;
private DatagramPacket packet;
private RecvMes RecM;
private Thread Td;
private int running;
private String IPContent = "10.1.0.41";
public Chat_client() {
initComponents();
guiPosition();
start();
running=0;
}
public void guiPosition() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dlgSize = getSize();
if(dlgSize.height > screenSize.height)
dlgSize.height = screenSize.height;
if(dlgSize.width > screenSize.width)
dlgSize.width = screenSize.width;
setLocation((screenSize.width - dlgSize.width)/2,(screenSize.height - dlgSize.height)/2);
}
private int initSocket() {
SendPort=4321;
RecvPort=1234;
byte[] rbuf = new byte[256];
packet = new DatagramPacket(rbuf,rbuf.length);
try {
Client = new DatagramSocket();
Server = new DatagramSocket(RecvPort);
}
catch(Exception e){
return 0;
}
return 1;
}
public void start(){
if(running == 0) {
if (initSocket() > 0) {
RecM = new RecvMes(showTextPane,Server,packet);
Td = new Thread(RecM);
Td.start();
running = 1;
}
}
else {
JOptionPane.showMessageDialog(this,"程序已经运行!","错误",1);
}
}
public class RecvMes implements Runnable{
private javax.swing.JTextPane RecvPane;
private DatagramSocket Server;
private DatagramPacket packet;
private int running = 0;
public void SetStopTag() {
running = 0;
}
public void run() {
running = 1;
while(running == 1) {
try {
Server.receive(packet);
String recieve = new String(packet.getData(),0,packet.getLength());
String info = RecvPane.getText();
RecvPane.setText(info+"好友说:\r"+recieve+"\r");
PrintWriter pw=new PrintWriter(new FileWriter("record.txt",true),true);
SimpleDateFormat fat=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
pw.println(fat.format(new Date()).trim());
pw.println("好友说:\r"+recieve+"\r");
}
catch(Exception e) {
running = 0;
}
}
}
public RecvMes(javax.swing.JTextPane rTextPane,DatagramSocket s,DatagramPacket p) {
RecvPane = rTextPane;
packet = p;
Server = s;
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
showTextPane = new javax.swing.JTextPane();
jScrollPane2 = new javax.swing.JScrollPane();
sendTextPane = new javax.swing.JTextPane();
jButton3 = new javax.swing.JButton();
sendButton = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu2 = new javax.swing.JMenu();
jSeparator1 = new javax.swing.JSeparator();
jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
jMenu3 = new javax.swing.JMenu();
jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jScrollPane1.setViewportView(showTextPane);
jScrollPane2.setViewportView(sendTextPane);
jButton3.setText("查看记录");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sendButton.setText("发送");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendButtonActionPerformed(evt);
}
});
jButton1.setText("清除记录");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jMenu2.setText("团队介绍");
jMenu2.add(jSeparator1);
jRadioButtonMenuItem2.setSelected(true);
jRadioButtonMenuItem2.setText("关于");
jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButtonMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jRadioButtonMenuItem2);
jMenuBar1.add(jMenu2);
jMenu3.setText("帮助");
jRadioButtonMenuItem3.setSelected(true);
jRadioButtonMenuItem3.setText("帮助");
jRadioButtonMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButtonMenuItem3ActionPerformed(evt);
}
});
jMenu3.add(jRadioButtonMenuItem3);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sendButton))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton3)
.addGap(28, 28, 28)
.addComponent(jButton1)
.addGap(88, 88, 88)
.addComponent(sendButton)
.addGap(14, 14, 14))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jRadioButtonMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
String str=null;
try{
FileInputStream in=new FileInputStream("help.txt");
byte buf[]=new byte[1024];
in.read(buf);
str=new String(buf,0,buf.length).trim();
JOptionPane.showMessageDialog(this,str,"帮助",1);
in.close();
} catch(Exception e){}
}
private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
String str=null;
try{
FileInputStream in=new FileInputStream("aboat.txt");
byte buf[]=new byte[1024];
in.read(buf);
str=new String(buf,0,buf.length).trim();
in.close();
JOptionPane.showMessageDialog(this,str,"关于",1);
} catch(Exception e){}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String str=null;
try{
FileInputStream in=new FileInputStream("record.txt");
byte buf[]=new byte[2048];
in.read(buf);
str=new String(buf,0,buf.length).trim();
JOptionPane.showMessageDialog(this,str,"聊天记录",1);
in.close();
} catch(Exception e){}
}
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
String SendInfo = sendTextPane.getText().trim();
byte[] sbuf = SendInfo.getBytes();
try {
if(sbuf.length != 0) {
Client.send(new DatagramPacket(sbuf,sbuf.length,InetAddress.getByName(IPContent),SendPort));
String remain = showTextPane.getText();
showTextPane.setText(remain+"您说:\r"+SendInfo+"\r");
PrintWriter pw=new PrintWriter(new FileWriter("record.txt",true),true);
SimpleDateFormat fat=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
pw.println(fat.format(new Date()).trim());
pw.println("您说:\r"+SendInfo+"\r");
}
} catch(Exception e) {
JOptionPane.showMessageDialog(this,"发送失败!!","错误",1);
}
showTextPane.setEditable(false);
sendTextPane.setText("");
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
if(running == 1) {
RecM.SetStopTag();
Client.close();
Server.close();
running = 0;
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//File f = new File("record.txt");
// JOptionPane.showMessageDialog(this, "您确定要删除程序目录下的记录文档吗?", "okay?", 1);
//boolean b = f.delete();
//System.out.println(b);
JOptionPane.showMessageDialog(this, "您确定要删除程序目录下的记录文档吗?", "okay?", 1);
FileOutputStream fos;
try {
fos = new FileOutputStream("record.txt");
String s1="";
byte buf1[]=s1.getBytes();
fos.write(buf1);
fos.close();
} catch (Exception ex) {
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Chat_client c = new Chat_client();
c.setVisible(true);
c.setTitle("聊天窗口");
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton sendButton;
private javax.swing.JTextPane sendTextPane;
private javax.swing.JTextPane showTextPane;
// End of variables declaration
}
忘了这个数据库了
create database project;
use project;
create table message(
name varchar(10) primary key not null,
password varchar(15) not null,
mail varchar(25),
phone varchar(15)
);
insert into message values("Jim","123","Jim@126.com","111");