我想设计一个GUI,实现注册的功能。
但是我的学的不够深,暂时不会连接数据库。
我想实现向文本文件uid.txt里输入用户名。当你按了ok按钮后。首先检查在用户名输入框里是否有字符,没有的话,显示提示信息。
有的话,判断2次输入的密码是否相同,不同的话,显示提示信息。相同的话,对uid.txt文件进行搜索,判断是否有相同的用户名,有的话,显示提示信息。
没有的话,将用户名追加写到uid.txt文件里。然后生成一个以用户名命名的二进制文件,在这里面保存密码。
但是我每次输入的时候都会将uid.txt里原有的信息给刷掉,所有原来的信息都没了,只有这次输入的信息。
如果我输入的用户名存在于这个文件夹的话,又会返回一个异常 NullPointerException
这个问题很困扰我啊,我是实在没有办法啦。
我用了 output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
课上老师讲,说如果后面的boolean为true的话,可以对文件进行追加数据。
所以我是这么写的,我看了API文档,貌似也是这么个说法。希望大家能帮帮我了。
谢谢大家啦。import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
private JTextField name = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField confirm = new JPasswordField();
private JLabel messageLabel = new JLabel();
private JLabel nameLabel = new JLabel();
private JLabel passwordLabel = new JLabel();
private JLabel confirmLabel = new JLabel();
private JButton ok = new JButton();
private JButton reset = new JButton();
private JButton exit = new JButton();
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public static void main(String[] args)
{
Register test = new Register();
test.setVisible(true);
}
public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel); }
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(!(name.getText().equals("")))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
break;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}
output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
System.out.println(e);
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Please enter your user name!");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
这个抛出的,发现不是的。当文件里没有我输入的用户名的时候,显示 success! 有的时候会显示 failed!
还请大家帮帮我吧。
一个人好累的,老师成天不见人,想问都问不到。。
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class RegisterTest extends JFrame implements ActionListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
private JTextField name = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField confirm = new JPasswordField();
private JLabel messageLabel = new JLabel();
private JLabel nameLabel = new JLabel();
private JLabel passwordLabel = new JLabel();
private JLabel confirmLabel = new JLabel();
private JButton ok = new JButton();
private JButton reset = new JButton();
private JButton exit = new JButton();
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public static void main(String[] args)
{
RegisterTest test = new RegisterTest();
test.setVisible(true);
}
public RegisterTest()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel); }
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(!(name.getText().equals("")))
{
BufferedReader fileInput = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
break;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
System.out.println("success!");
}
else
{
System.out.println("failed!");
}
}
catch(Exception e)
{
System.out.println(e);
}
// if(password.getText().equals(confirm.getText()))
// {
// BufferedReader fileInput = null;
// PrintWriter output = null;
// ObjectOutputStream pw = null;
// String getUid;
// boolean a = true;
// try
// {
// fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
// getUid = fileInput.readLine();
// while(getUid != null)
// {
// if(getUid.equals(name.getText()))
// {
// a = false;
// break;
// }
// getUid = fileInput.readLine();
// }
//
// fileInput.close();
//
// if(a)
// {
// output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
// pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
// output.println(name.getText());
// pw.writeUTF(password.getText());
// }
// else
// {
// messagePanel.setBackground(Color.red);
// messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
// }
//
// output.close();
// pw.close();
// }
// catch(Exception e)
// {
// messagePanel.setBackground(Color.red);
// messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
// System.out.println(e);
// }
// }
// else
// {
// messagePanel.setBackground(Color.red);
// messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
// }
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Please enter your user name!");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.FileWriter;public class Register extends JFrame implements ActionListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
private JTextField name = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField confirm = new JPasswordField();
private JLabel messageLabel = new JLabel();
private JLabel nameLabel = new JLabel();
private JLabel passwordLabel = new JLabel();
private JLabel confirmLabel = new JLabel();
private JButton ok = new JButton();
private JButton reset = new JButton();
private JButton exit = new JButton();
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public static void main(String[] args)
{
Register test = new Register();
test.setVisible(true);
}
public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel); }
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(!(name.getText().equals("")))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
FileWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("F:/java/GUI/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
break;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
output = new FileWriter("uid.txt",ture);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.append(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}
output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator123!");
System.out.println(e);
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Please enter your user name!");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}这样可以在文件中追加.
就是使用FileWrite的append方法即可