所有功能均已實(shí)現(xiàn),如有不滿(mǎn)意的地方我再修改import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class Login extends JPanel{ //聲明各個(gè)控件 private JLabel user_name_label = null; private JLabel password_label = null; private JTextField user_name_text = null; private JTextField password_text = null; private JButton login = null; private JButton regist = null; //聲明文件用以保存注冊(cè)信息 private final String file_name = "注冊(cè).txt"; public Login() { //獲得各個(gè)控件并且為之設(shè)置顯示文本 user_name_label = new JLabel(); user_name_label.setText("姓名:"); password_label = new JLabel(); password_label.setText("密碼:"); user_name_text = new JTextField(); password_text = new JTextField(); login = new JButton(); login.setText("登錄"); regist = new JButton(); regist.setText("注冊(cè)"); //設(shè)置面板的布局為網(wǎng)格布局 setLayout(new GridLayout(3,2)); //將控件添加到面板里 add(user_name_label); add(user_name_text); add(password_label); add(password_text); add(login); add(regist); //為兩個(gè)按鈕添加監(jiān)聽(tīng) regist.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = user_name_text.getText().toString(); String password = password_text.getText().toString(); String str = null; String[] result = null; try { if((name.length() == 0)&&(password.length() == 0)) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入用戶(hù)名和密碼","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); throw new Exception(""); } else if(name.length() == 0) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入用戶(hù)名","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } else if(password.length() == 0) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入密碼","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } InputStream in = new FileInputStream(file_name); InputStreamReader reader = new InputStreamReader(in); BufferedReader buffered_reader = new BufferedReader(reader); while((str = buffered_reader.readLine()) != null) { result = str.split(" "); if(result[0].equals(name)) { int a = JOptionPane.showConfirmDialog(null,"該用戶(hù)已存在,請(qǐng)重新注冊(cè)","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); throw new Exception(""); } } OutputStream out = new FileOutputStream(file_name,true); OutputStreamWriter writer = new OutputStreamWriter(out); BufferedWriter buffered_writer = new BufferedWriter(writer); buffered_writer.write(name+" "+password); buffered_writer.newLine(); buffered_writer.close(); int a = JOptionPane.showConfirmDialog(null,"恭喜你,注冊(cè)成功!","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } catch(Exception e1) { } } }); login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = user_name_text.getText().toString(); String password = password_text.getText().toString(); String result = null; try { if((name.length() == 0)&&(password.length() == 0)) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入用戶(hù)名和密碼","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); throw new Exception(""); } else if(name.length() == 0) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入用戶(hù)名","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } else if(password.length() == 0) { int a = JOptionPane.showConfirmDialog(null,"請(qǐng)輸入密碼","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } InputStream in = new FileInputStream(file_name); InputStreamReader reader = new InputStreamReader(in); BufferedReader buffered_reader = new BufferedReader(reader); while((result = buffered_reader.readLine()) != null) { if(result.equals(name+" "+password)) { int a = JOptionPane.showConfirmDialog(null,"登陸成功","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); break; } } if(!(result.equals(name+" "+password))) { int a = JOptionPane.showConfirmDialog(null,"用戶(hù)名或密碼錯(cuò)誤","確認(rèn)對(duì)話框",JOptionPane.YES_NO_OPTION); } } catch(Exception e1) { //e1.printStackTrace(); } } }); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500,500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new Login(),BorderLayout.NORTH); }}