服务器:
TCPServer :
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class TCPServer {
// 创建一个集合,集合存放客服端的对象
ArrayList<Socket> arrayListSocket = new ArrayList<Socket>();
TCPServer() {
ServerSocket ss;
try {
ss = new ServerSocket(30001);
while (true) {
// 等待客服端连接,一旦有客服端连接服务器,将客服端对象存放到集合中
Socket socket = ss.accept();
arrayListSocket.add(socket);
String ip = socket.getInetAddress().getHostAddress();
System.out.println("ip: " + ip + " 上线了!");
new Thread(new acceptClient(socket, ip)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class acceptClient implements Runnable {
Socket s;
String ip;
acceptClient(Socket s, String ip) {
this.s = s;
this.ip = ip;
}
@Override
public void run() {
try {
// 通知用户上线消息
for (int i = 0; i < arrayListSocket.size(); i++) {
Socket client = arrayListSocket.get(i);
// 下面PrintWriter是可以自带刷新缓存工作,可以简化写入流的操作,
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
pout.println(ip + "上线了!!!");
}
while (true) {
// 读取Socket读取流中的数据
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = bufIn.readLine();
// 下面PrintWriter是可以自带刷新缓存工作,可以简化写入流的操作,
// 通过for()循序服务器将数据发送的arrayListSocket集合中所有客服端
for (int i = 0; i < arrayListSocket.size(); i++) {
Socket client = arrayListSocket.get(i);
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
pout.println(line);
}
}
} catch (IOException e) {
System.out.println("接受:" + s.getInetAddress() + " 信息失败");
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception, IOException {
// 创建服务器对象
new TCPServer();
}
}
LoginFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame implements ActionListener{
public LoginFrame() {
this.setTitle("注册");
JLabel jl=new JLabel("请输入你的账号:");
JTextField jf=new JTextField(20);
JPanel jp=new JPanel(new GridLayout(3, 1));
JButton jb=new JButton("确认");
JButton jb2=new JButton("返回");
this.setSize(150, 150);
this.setResizable(false);
JLabel jl2=new JLabel("请输入你的密码:");
JPasswordField jw=new JPasswordField(20);
jp.add(jf);
jp.add(jl2);
jp.add(jw);
this.add(jl,BorderLayout.NORTH);
this.add(jp, BorderLayout.CENTER);
JPanel jp2=new JPanel();
jp2.add(jb);
jp2.add(jb2);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 150) / 2, (sheight - 150) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int count=0;
String text = jf.getText();
String password = new String(jw.getPassword());
BufferedReader br = null;
BufferedWriter bw=null;
BufferedReader br2 = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text) && split[1].equals(password)) {
JOptionPane.showMessageDialog(LoginFrame.this, "已存在");
count++;
}
}
if(count==0)
{
String string2=null;
br2 = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
while((string2=br.readLine())==null)
{
bw=new BufferedWriter(new FileWriter("ss.txt",true));
String s=text+","+password;
bw.write(s+"\r\n");
bw.flush();
JOptionPane.showMessageDialog(LoginFrame.this, "注册成功");
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
LoginFrame.this.setVisible(false);
MainFrame m=new MainFrame();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
MainFrame:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
//去掉默认布局管理器:JFrame jf = new JFrame("窗口");jf.setLayout(null);
public class MainFrame extends JFrame implements ActionListener {
public MainFrame() throws HeadlessException {
this.setTitle("窗口");
JPanel j1 = new JPanel(new GridLayout(2, 2));
JLabel jl1 = new JLabel(" 用户账号");
JLabel jl2 = new JLabel(" 用户密码");
JTextField jf1 = new JTextField(10);
JPasswordField jp = new JPasswordField(10);
j1.add(jl1);
j1.add(jf1);
j1.add(jl2);
j1.add(jp);
JLabel jl3 = new JLabel(new ImageIcon("img/a.jpg"));
this.add(jl3, BorderLayout.NORTH);
this.add(j1, BorderLayout.CENTER);
this.setSize(500, 500);
this.setResizable(false);
JButton jb = new JButton("提交");
JButton jb2 = new JButton("注册");
JButton jb3 = new JButton("忘记密码?");
JPanel jp2 = new JPanel();
jp2.add(jb);
jp2.add(jb2);
jp2.add(jb3);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 500) / 2, (sheight - 500) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.pack();
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = 0;
Object source = e.getSource();
String text = jf1.getText();
String password = new String(jp.getPassword());
int confirmDialog = JOptionPane.showConfirmDialog(MainFrame.this, "是否确认登陆");
if (confirmDialog == JOptionPane.YES_OPTION) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text) && split[1].equals(password)) {
JOptionPane.showMessageDialog(MainFrame.this, "登陆成功");
count++;
MainFrame.this.setVisible(false);
TCPServer t=new TCPServer();
}
}
if (count == 0) {
JOptionPane.showMessageDialog(MainFrame.this, "请先注册");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int confirmDialog = JOptionPane.showConfirmDialog(MainFrame.this, "是否确认注册");
if (confirmDialog == JOptionPane.YES_OPTION) {
MainFrame.this.setVisible(false);
LoginFrame l = new LoginFrame();
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.setVisible(false);
MissFrame m = new MissFrame();
}
});
}
public static void main(String[] args) {
MainFrame m = new MainFrame();
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
MissFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MissFrame extends JFrame implements ActionListener{
public MissFrame() {
JLabel jl=new JLabel("请输入你的账号:");
JTextField jf=new JTextField(20);
JPanel jp=new JPanel(new GridLayout(2, 1));
JButton jb=new JButton("确认");
JButton jb2=new JButton("返回");
this.setTitle("找回密码");
this.setSize(150, 150);
this.setResizable(false);
JTextArea ja=new JTextArea(1,20);
ja.setEditable(false);
jp.add(jf);
jp.add(ja);
this.add(jl,BorderLayout.NORTH);
this.add(jp, BorderLayout.CENTER);
JPanel jp2=new JPanel();
jp2.add(jb);
jp2.add(jb2);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 150) / 2, (sheight - 150) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String text = jf.getText();
BufferedReader br = null;
int count=0;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text)) {
ja.append(split[1]);
JOptionPane.showMessageDialog(MissFrame.this, "成功");
count++;
}
}
if(count==0)
{
JOptionPane.showMessageDialog(MissFrame.this, "请先注册");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MissFrame.this.setVisible(false);
MainFrame m=new MainFrame();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
ReciveThread:
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ReciveThread extends Thread {
private Socket recive;
private JTextArea ja1;
private JTextField jf;
public ReciveThread(Socket recive, JTextArea ja1, JTextField jf) {
this.recive = recive;
this.ja1 = ja1;
this.jf = jf;
}
@Override
public void run() {
BufferedReader br = null;
try {
while (true) {
InputStream stream = recive.getInputStream();
jf.setText(recive.getInetAddress().toString());
br = new BufferedReader(new InputStreamReader(stream));
String string = null;
while ((string = br.readLine()) != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = sdf.format(new Date());
ja1.append(format+"\n"+string+"\n");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (recive != null) {
recive.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
TCPClient:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TCPClient {
static JFrame f;
static JButton send;
static JButton clear;
static JButton close;
static JTextField name;
static JTextField sendjf;
static JTextArea messageArea;
static Socket s;
public static void main(String[] args) {
connetServer();
new setMenu(s);
new Thread(new recvMsg(s)).start();
}
private static void connetServer() {
try {
s = new Socket("127.0.0.1", 30001);
} catch (Exception e) {
System.out.println("客户端连接服务器失败");
e.printStackTrace();
}
}
}
class recvMsg implements Runnable {
private Socket s;
private JTextArea message;
recvMsg(Socket s) {
this.s = s;
this.message = setMenu.messageArea;
}
@Override
public void run() {
BufferedReader bufIn = null;
while (true) {
try {
bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException e1) {
System.out.println("接收信息失败");
e1.printStackTrace();
}
try {
String recvMsg = bufIn.readLine();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd:hh-mm-ss");
String format = sdf.format(new Date());
message.append(format + "\n" + recvMsg + "\r\n");
setMenu.messageArea.setCaretPosition(setMenu.messageArea.getDocument().getLength());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 创建GUI界面的类
*/
class setMenu {
static JFrame f;
static JButton send;
static JButton clear;
static JButton close;
static JTextField name;
static JTextField sendjf;
static JTextArea messageArea;
Socket s;
setMenu(Socket s) {
this.s = s;
createMenu();
someListener();
}
private void someListener() {
send.addActionListener(new SendMsg3(s));
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
sendjf.addKeyListener(new SendMsg3(s));
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendjf.setText("");
}
});
messageArea.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
}
});
}
private void createMenu() {
f = new JFrame("I am 扣扣");
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
f.setLocation((swidth - 500) / 2, (sheight - 500) / 2);
f.setBounds(50, 50, 700, 600);
JPanel panel = new JPanel();
JLabel label = new JLabel("名字:", JLabel.RIGHT);
name = new JTextField(20);
panel.add(label, JPanel.RIGHT_ALIGNMENT);
panel.add(name, JPanel.RIGHT_ALIGNMENT);
messageArea = new JTextArea("", 25, 50);
messageArea.setEditable(false);
JLabel label_send = new JLabel("发送内容:");
sendjf = new JTextField(30);
JPanel button = new JPanel(new FlowLayout());
send = new JButton("发送");
clear = new JButton("清除");
close = new JButton("关闭");
button.add(label_send);
button.add(sendjf);
button.add(send);
button.add(clear);
button.add(close);
JScrollPane jsp=new JScrollPane(messageArea);
JPanel jpj=new JPanel();
jpj.add(jsp);
f.add(panel, BorderLayout.NORTH);
f.add(jpj, BorderLayout.CENTER);
f.add(button, BorderLayout.SOUTH);
f.setVisible(true);
}
class SendMsg3 implements ActionListener, KeyListener {
Socket s;
SendMsg3(Socket s) {
this.s = s;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// VK_ENTER回车键的虚拟键
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
sendMsg();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
sendMsg();
}
private void sendMsg() {
if (name.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请取别名!");
}
String line = name.getText() + ": " + sendjf.getText();
try {
if (!line.isEmpty()) {
// PrintWriter是可以自带刷新缓存工作,可以简化写入流的操作,
PrintWriter pout = new PrintWriter(s.getOutputStream(), true);
pout.println(line);
System.out.print("input:" + line);
sendjf.setText("");
messageArea.setCaretPosition(messageArea.getDocument().getLength());
} else {
System.out.println("发送数据为空,请重新输入");
JOptionPane.showMessageDialog(null, "发送数据为空,请重新输入!");
}
} catch (Exception e1) {
System.out.println("发送失败");
e1.printStackTrace();
}
}
}
}
LoginFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame implements ActionListener{
public LoginFrame() {
this.setTitle("注册");
JLabel jl=new JLabel("请输入你的账号:");
JTextField jf=new JTextField(20);
JPanel jp=new JPanel(new GridLayout(3, 1));
JButton jb=new JButton("确认");
JButton jb2=new JButton("返回");
this.setSize(150, 150);
this.setResizable(false);
JLabel jl2=new JLabel("请输入你的密码:");
JPasswordField jw=new JPasswordField(20);
jp.add(jf);
jp.add(jl2);
jp.add(jw);
this.add(jl,BorderLayout.NORTH);
this.add(jp, BorderLayout.CENTER);
JPanel jp2=new JPanel();
jp2.add(jb);
jp2.add(jb2);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 150) / 2, (sheight - 150) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int count=0;
String text = jf.getText();
String password = new String(jw.getPassword());
BufferedReader br = null;
BufferedWriter bw=null;
BufferedReader br2 = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text) && split[1].equals(password)) {
JOptionPane.showMessageDialog(LoginFrame.this, "已存在");
count++;
}
}
if(count==0)
{
String string2=null;
br2 = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
while((string2=br.readLine())==null)
{
bw=new BufferedWriter(new FileWriter("ss.txt",true));
String s=text+","+password;
bw.write(s+"\r\n");
bw.flush();
JOptionPane.showMessageDialog(LoginFrame.this, "注册成功");
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
LoginFrame.this.setVisible(false);
MainFrame m=new MainFrame();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
MainFrame:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
//去掉默认布局管理器:JFrame jf = new JFrame("窗口");jf.setLayout(null);
public class MainFrame extends JFrame implements ActionListener {
public MainFrame() throws HeadlessException {
this.setTitle("窗口");
JPanel j1 = new JPanel(new GridLayout(2, 2));
JLabel jl1 = new JLabel(" 用户账号");
JLabel jl2 = new JLabel(" 用户密码");
JTextField jf1 = new JTextField(10);
JPasswordField jp = new JPasswordField(10);
j1.add(jl1);
j1.add(jf1);
j1.add(jl2);
j1.add(jp);
JLabel jl3 = new JLabel(new ImageIcon("img/a.jpg"));
this.add(jl3, BorderLayout.NORTH);
this.add(j1, BorderLayout.CENTER);
this.setSize(500, 500);
this.setResizable(false);
JButton jb = new JButton("提交");
JButton jb2 = new JButton("注册");
JButton jb3 = new JButton("忘记密码?");
JPanel jp2 = new JPanel();
jp2.add(jb);
jp2.add(jb2);
jp2.add(jb3);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 500) / 2, (sheight - 500) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.pack();
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = 0;
Object source = e.getSource();
String text = jf1.getText();
String password = new String(jp.getPassword());
int confirmDialog = JOptionPane.showConfirmDialog(MainFrame.this, "是否确认登陆");
if (confirmDialog == JOptionPane.YES_OPTION) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text) && split[1].equals(password)) {
JOptionPane.showMessageDialog(MainFrame.this, "登陆成功");
count++;
MainFrame.this.setVisible(false);
TCPServer t=new TCPServer();
}
}
if (count == 0) {
JOptionPane.showMessageDialog(MainFrame.this, "请先注册");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int confirmDialog = JOptionPane.showConfirmDialog(MainFrame.this, "是否确认注册");
if (confirmDialog == JOptionPane.YES_OPTION) {
MainFrame.this.setVisible(false);
LoginFrame l = new LoginFrame();
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.setVisible(false);
MissFrame m = new MissFrame();
}
});
}
public static void main(String[] args) {
MainFrame m = new MainFrame();
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
MissFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MissFrame extends JFrame implements ActionListener{
public MissFrame() {
JLabel jl=new JLabel("请输入你的账号:");
JTextField jf=new JTextField(20);
JPanel jp=new JPanel(new GridLayout(2, 1));
JButton jb=new JButton("确认");
JButton jb2=new JButton("返回");
this.setTitle("找回密码");
this.setSize(150, 150);
this.setResizable(false);
JTextArea ja=new JTextArea(1,20);
ja.setEditable(false);
jp.add(jf);
jp.add(ja);
this.add(jl,BorderLayout.NORTH);
this.add(jp, BorderLayout.CENTER);
JPanel jp2=new JPanel();
jp2.add(jb);
jp2.add(jb2);
this.add(jp2, BorderLayout.SOUTH);
Toolkit tk = Toolkit.getDefaultToolkit();
int swidth = tk.getScreenSize().width;
int sheight = tk.getScreenSize().height;
this.setLocation((swidth - 150) / 2, (sheight - 150) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String text = jf.getText();
BufferedReader br = null;
int count=0;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("ss.txt")));
String string = null;
while ((string = br.readLine()) != null) {
String[] split = string.split(",");
if (split[0].equals(text)) {
ja.append(split[1]);
JOptionPane.showMessageDialog(MissFrame.this, "成功");
count++;
}
}
if(count==0)
{
JOptionPane.showMessageDialog(MissFrame.this, "请先注册");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MissFrame.this.setVisible(false);
MainFrame m=new MainFrame();
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
ReciveThread:
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ReciveThread extends Thread {
private Socket recive;
private JTextArea ja1;
private JTextField jf;
public ReciveThread(Socket recive, JTextArea ja1, JTextField jf) {
this.recive = recive;
this.ja1 = ja1;
this.jf = jf;
}
@Override
public void run() {
BufferedReader br = null;
try {
while (true) {
InputStream stream = recive.getInputStream();
jf.setText(recive.getInetAddress().toString());
br = new BufferedReader(new InputStreamReader(stream));
String string = null;
while ((string = br.readLine()) != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = sdf.format(new Date());
ja1.append(format+"\n"+string+"\n");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (recive != null) {
recive.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}