服务器端:
UserSeverFrame:
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class UserSeverFrame extends JFrame implements ActionListener { private JTextArea ja1; private JTextArea ja2; private JTextArea ja3; private JTextField jf; private JButton jb; ServerSocket serverSocket; Socket socket ; public void init() { this.setTitle("服务器端"); this.setSize(500, 500); this.setResizable(false); jb = new JButton("发送"); jf = new JTextField(30); jf.setEditable(false); ja1 = new JTextArea(22, 22); ja1.setEditable(false); ja2 = new JTextArea(3, 30); ja3 = new JTextArea(22, 8); ja3.setEditable(false); JPanel jp = new JPanel(); jp.add(ja1); jp.add(ja3); JPanel jp2 = new JPanel(new GridLayout(2, 1)); jp2.add(ja2); jp2.add(jb); JPanel jp3 = new JPanel(); jp3.add(jf); this.add(jp3, BorderLayout.NORTH); this.add(jp, BorderLayout.CENTER); 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(); } public UserSeverFrame() { this.init(); try { serverSocket = new ServerSocket(8888); } catch (IOException e1) { e1.printStackTrace(); } try { socket = serverSocket.accept(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ReciveThread r = new ReciveThread(socket, ja1, jf); r.start(); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { BufferedWriter bw = null; OutputStream stream = null; try { stream = socket.getOutputStream(); String string = null; String[] split = null; string = ja2.getText(); if (!(ja2.getText().equals(""))) { split = string.split("\\n"); } jf.setText(socket.getInetAddress().toString()); bw = new BufferedWriter(new OutputStreamWriter(stream)); int count = 0; while (split != null) { for (String s : split) { bw.write(s + "\r\n"); bw.flush(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String format = sdf.format(new Date()); ja1.append(format + "\n" + s + "\n"); count++; } if (count == split.length) { break; } } ja2.setText(""); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void actionPerformed(ActionEvent arg0) { } }
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("E:/新建文本文档.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("E:/新建文本文档.txt"))); while((string2=br.readLine())==null) { bw=new BufferedWriter(new FileWriter("E:/新建文本文档.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) { } }
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(); } } } }
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("E:/新建文本文档.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) { } }
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("E:/新建文本文档.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); UserSeverFrame u=new UserSeverFrame(); } } 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) { } }
客户端:
UserSocketFrame:
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.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class UserSocketFrame extends JFrame implements ActionListener { private JTextArea ja1; private JTextArea ja2; private JTextArea ja3; private JTextField jf; private JButton jb; Socket socket; public void init() { this.setTitle("客户端"); this.setSize(500, 500); this.setResizable(false); jb = new JButton("发送"); jf = new JTextField(30); jf.setEditable(false); ja1 = new JTextArea(22, 22); ja1.setEditable(false); ja2 = new JTextArea(3, 30); ja3 = new JTextArea(22, 8); ja3.setEditable(false); JPanel jp = new JPanel(); jp.add(ja1); jp.add(ja3); JPanel jp2 = new JPanel(new GridLayout(2, 1)); jp2.add(ja2); jp2.add(jb); JPanel jp3 = new JPanel(); jp3.add(jf); this.add(jp3, BorderLayout.NORTH); this.add(jp, BorderLayout.CENTER); 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(); } public UserSocketFrame() { this.init(); try { socket = new Socket("localhost", 8888); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ReciveThread r = new ReciveThread(socket, ja1, jf); r.start(); jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { BufferedWriter bw = null; OutputStream stream = null; try { stream = socket.getOutputStream(); String string = null; String[] split = null; string = ja2.getText(); if (!(ja2.getText().equals(""))) { split = string.split("\\n"); } jf.setText(socket.getInetAddress().toString()); bw = new BufferedWriter(new OutputStreamWriter(stream)); int count = 0; while (split != null) { for (String s : split) { bw.write(s + "\r\n"); bw.flush(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String format = sdf.format(new Date()); ja1.append(format + "\n" + s + "\n"); count++; } if (count == split.length) { break; } } ja2.setText(""); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void actionPerformed(ActionEvent arg0) { } }
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("E:/新建文本文档.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("E:/新建文本文档.txt"))); while((string2=br.readLine())==null) { bw=new BufferedWriter(new FileWriter("E:/新建文本文档.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) { } }
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(); } } } }
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("E:/新建文本文档.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) { } }
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("E:/新建文本文档.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); UserSocketFrame u=new UserSocketFrame(); } } 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) { } }