import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;public class TestPasswordLoad extends JFrame implements ActionListener{
    private JLabel account,password,display;
    private JTextField input_account;
    private TextField input_password;
    private JButton Load,Enrol;
    private JPanel jp1,jp2,jp3;
    public TestPasswordLoad(String s){
        super(s);
        setLayout(new BorderLayout());
        account = new JLabel("Account:  ");
        password = new JLabel("password:");
        display = new JLabel("");
        input_account = new JTextField(15);
        input_password = new TextField(15);
        input_password.setEchoChar('*');
        Load = new JButton("Load");
        Enrol = new JButton("Enrol");
        jp1 = new JPanel(new FlowLayout());
        jp2 = new JPanel(new FlowLayout());
        jp3 = new JPanel(new FlowLayout());
        add(jp1,"North");
        add(jp2);
        add(jp3,"South");
        jp1.add(account);
        jp1.add(input_account);
        jp2.add(password);
        jp2.add(input_password);
        jp2.add(display);
        jp3.add(Load);
        jp3.add(Enrol);
        Enrol.addActionListener(this);
        Load.addActionListener(this);
        setVisible(true);
        setBounds(80,80,360,180);        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == Enrol){
            JFrame jf = new JFrame();
            String ss = "注册账号";
            boolean bb = true;
           new  LoadDialog(jf,ss,bb);       
        }        if(e.getSource() == Load){
        String account =input_account.getText();
        String password = input_password.getText();
        String accountcompare = null;
        String passwordcompare = null;
        try{
        BufferedReader r = new BufferedReader(new FileReader("F:\\Acont_Passwored.txt"));
        accountcompare = r.readLine(); 
        //如何读遍文件中的每一行账号和密码
        passwordcompare =r.readLine();
        }catch(IOException ee){}
        account = "account:"+account;
        password = "password:"+password;
        if(account.equals(account) == true && password.equals(password) ==  true){
            display.setText("密码正确,系统正在登陆");
            input_account.setText(null);
            input_password.setText(null);
        }
        else {
            display.setText("密码错误,请重新输入");
            input_account.setText(null);
            input_password.setText(null);
        }
        }
    }    public static void main(String[] args){
        new TestPasswordLoad("Loading...");
    }
}class LoadDialog extends Dialog implements ActionListener{
    private JLabel account,password,display;
    private JTextField input_account;private TextField input_password;
    private JButton ok;
    private JPanel jp1,jp2,jp3;
    public LoadDialog(Frame f, String s, boolean b){
        super(f,s,b);
        setBounds(80,80,360,180);
        setLayout(new BorderLayout());
        account = new JLabel("Account:  ");
        password = new JLabel("password:");
        display = new JLabel("");
        input_account = new JTextField(15);
        input_password = new TextField(15);
        input_password.setEchoChar('*');
        ok = new JButton("OK");
        jp1 = new JPanel(new FlowLayout());
        jp2 = new JPanel(new FlowLayout());
        jp3 = new JPanel(new FlowLayout());
        add(jp1,"North");
        add(jp2);
        add(jp3,"South");
        jp1.add(account);
        jp1.add(input_account);
        jp2.add(password);
        jp2.add(input_password);
        jp2.add(display);
        jp3.add(ok);
        ok.addActionListener(this);
        setVisible(true);
        validate();
         addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
                setVisible(false);
            }
        });    }    public void actionPerformed(ActionEvent e){
        if(e.getSource() == ok){
        String account =input_account.getText();
        String password = input_password.getText();
        String comp=null;
        try{
        BufferedReader r = new BufferedReader(new FileReader("F:\\Acont_Passwored.txt"));
        comp = r.readLine();
        }catch(IOException ee){}        String compare = "account:"+comp;        if(compare.equals(account)){
            display.setText("该账户已注册");  //display无法显示,并且当输入两个一样德账户时,这两个相同的张户也会记录到文件中,这是为什么
            input_account.setText(null);
        }else{
        try{
        BufferedWriter w = new BufferedWriter(new FileWriter("F:\\Acont_Passwored.txt",true));
        if(account.length()>=5 && password.length()>=6){
            w.write("account:"+account);
            w.newLine();
            w.write("password:"+password);
            w.newLine();
            w.flush();
            w.close();
             setVisible(false);
          }
        else
        {
            display.setText("账号或密码过短,请重新输入"); // display无法显示出来???
            input_account.setText(null);
            input_password.setText(null);
        }
        }catch(IOException ex){
            display.setText("文件错误");
                       }    
                 }
             }
         }
    }

解决方案 »

  1.   

    把TextField换成JPasswordField。
    一般来说不要混用AWT和Swing组件。另:把你的问题说出来,只贴代码不知你到底有什么问题
      

  2.   

    解析文件内容到一个Map里,输入帐号时在Map里查找
      

  3.   

    帮你改好了import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class TestPasswordLoad extends JFrame implements ActionListener {
    private JLabel account, password, display;
    private JTextField input_account;
    private TextField input_password;
    private JButton Load, Enrol;
    private JPanel jp1, jp2, jp3; public TestPasswordLoad(String s) {
    super(s);
    setLayout(new BorderLayout());
    account = new JLabel("Account: ");
    password = new JLabel("password:");
    display = new JLabel("");
    input_account = new JTextField(15);
    input_password = new TextField(15);
    input_password.setEchoChar('*');
    Load = new JButton("Load");
    Enrol = new JButton("Enrol");
    jp1 = new JPanel(new FlowLayout());
    jp2 = new JPanel(new FlowLayout());
    jp3 = new JPanel(new FlowLayout());
    add(jp1, "North");
    add(jp2);
    add(jp3, "South");
    jp1.add(account);
    jp1.add(input_account);
    jp2.add(password);
    jp2.add(input_password);
    jp2.add(display);
    jp3.add(Load);
    jp3.add(Enrol);
    Enrol.addActionListener(this);
    Load.addActionListener(this);
    setVisible(true);
    setBounds(80, 80, 360, 180); addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == Enrol) {
    JFrame jf = new JFrame();
    String ss = "注册账号";
    boolean bb = true;
    new LoadDialog(jf, ss, bb);
    System.out.println("new LoadDialog");
    } if (e.getSource() == Load) {
    String account = input_account.getText();
    String password = input_password.getText();
    String accountcompare = null;
    String passwordcompare = null;
    try {
    BufferedReader r = new BufferedReader(new FileReader(new File("Acont_Passwored.txt")
    ));
    accountcompare = r.readLine();
    // 如何读遍文件中的每一行账号和密码
    passwordcompare = r.readLine();
    } catch (IOException ee) { }
    String ac = "account:" + account;    //你这里也有错误,对比一下你的程序和我的程序
     System.out.println(account);
    String pa = "password:" + password;
    if (account.equals(ac) == true
    && password.equals(pa) == true) {
    display.setText("密码正确,系统正在登陆");
    input_account.setText(null);
    input_password.setText(null);
    } else {
    display.setText("密码错误,请重新输入");
    input_account.setText(null);
    input_password.setText(null);
    }
    }
    } public static void main(String[] args) {
    new TestPasswordLoad("Loading...");
    }}class LoadDialog extends Dialog implements ActionListener {
    private JLabel account, password, display;
    private JTextField input_account;
    private TextField input_password;
    private JButton ok;
    private JPanel jp1, jp2, jp3; public LoadDialog(Frame f, String s, boolean b) {
    super(f, s, b);
    setBounds(80, 80, 360, 180);
    setLayout(new BorderLayout());
    account = new JLabel("Account: ");
    password = new JLabel("password:");
    display = new JLabel("");
    input_account = new JTextField(15);
    input_password = new TextField(15);
    input_password.setEchoChar('*');
    ok = new JButton("OK");
    jp1 = new JPanel(new FlowLayout());
    jp2 = new JPanel(new FlowLayout());
    jp3 = new JPanel(new FlowLayout());
    add(jp1, "North");
    add(jp2);
    add(jp3, "South");
    jp1.add(account);
    jp1.add(input_account);
    jp2.add(password);
    jp2.add(input_password);
    jp2.add(display);
    jp3.add(ok);
    ok.addActionListener(this);
    setVisible(true);
    validate();
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    setVisible(false);
    }
    }); } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == ok) {
    String account = input_account.getText();
    String password = input_password.getText();
    String comp = null;
    try {
    BufferedReader r = new BufferedReader(new FileReader(new File("Acont_Passwored.txt")
    ));
    comp = r.readLine();
    } catch (IOException ee) {
    ee.printStackTrace();
    }//  String compare = "account:"+comp;
    String compare = comp;
    String temp="account:"+account;
    if (compare.equals(temp)) {
    display.setText("该账户已注册"); // display无法显示,并且当输入两个一样德账户时,这两个相同的张户也会记录到文件中,这是为什么
    // input_account.setText(null);
    JOptionPane.showMessageDialog(null, "该用户已注册!");
    setVisible(false);
    } else {
    try {
    BufferedWriter w = new BufferedWriter(new FileWriter(new File("Acont_Passwored.txt")
    , true));    //向文件追加内容
    if (account.length() >= 5 && password.length() >= 6) {
    w.write("account:" + account);
    w.newLine();
    w.write("password:" + password);
    w.newLine();
    w.flush();
    w.close();
    setVisible(false);
    // System.out.println("文件追加内容");   //测试语句
    } else {
    display.setText("账号或密码过短,请重新输入"); // display无法显示出来???
    input_account.setText(null);
    input_password.setText(null);
    }
    } catch (IOException ex) {
    display.setText("文件错误");
    }
    }
    }
    }
    }这里有个文件相关的帖子,http://topic.csdn.net/u/20100511/22/2196893e-9906-4041-b1e9-57ea2c17da05.html
    注意看五楼的回复,会对你有帮助的,加油!