发布网友 发布时间:2022-04-26 23:45
共4个回答
热心网友 时间:2022-06-20 09:01
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class TestRegister extends JDialog {
// setup the variable
String name="";
String password="";
JLabel nameL,passwordL;
JTextField nameArea;
JPasswordField passwordArea;
JButton register,login,cancel;
ArrayList <User> userData = new ArrayList <User>();
public static void main(String[] args) {
TestRegister test = new TestRegister();
test.setupLayout();
}
public void setupLayout(){
this.setBounds(300, 200,280,120);
this.setTitle("Register");
this.setResizable(false);
this.setModal(true);
setupTypeArea();
this.getData();
this.setVisible(true);
}
public void setupTypeArea(){
JPanel typeArea = new JPanel();
typeArea.setLayout(new FlowLayout());
nameL = new JLabel("name: ");
passwordL = new JLabel("password: ");
nameArea = new JTextField(15);
passwordArea = new JPasswordField(15);
typeArea.add(nameL);
typeArea.add(nameArea);
typeArea.add(passwordL);
typeArea.add(passwordArea);
this.add(typeArea);
JPanel buttonArea = new JPanel();
register = this.addButton("Register", buttonArea);
login = this.addButton("Login", buttonArea);
cancel = this.addButton("Cancel", buttonArea);
this.add(buttonArea,BorderLayout.SOUTH);
}
public JButton addButton(String name,JPanel panel){
JButton temp = new JButton(name);
temp.addActionListener(new ButtonMonitor());
panel.add(temp);
return temp;
}
public void showMessage(String m){
JOptionPane.showMessageDialog(this, m);
}
public void getData(){
try {
Scanner input = new Scanner(new FileReader("D:/data.txt"));
while(input.hasNextLine()){
this.addUser(input.nextLine());
}
} catch (FileNotFoundException e) {
return;
}
}
public void addUser(String data){
String [] s = data.split(" ");
String name = s[0];
String password = s[1];
userData.add(new User(name,password));
}
public void updateData(){
try {
FileWriter saver = new FileWriter("D:/data.txt");
String data = "";
for(int i=0; i<userData.size();i++){
data += userData.get(i).toString()+"\n";
}
saver.write(data);
saver.close();
} catch (IOException e) {
return;
}
}
public void doRegister(){
String name = nameArea.getText();
String password = passwordArea.getText();
if(!name.isEmpty()&!password.isEmpty()){
for(int i=0;i<userData.size();i++){
if(name.equals(userData.get(i).getName())){
String m = "这个用户名已经注册!!";
this.showMessage(m);
doCancel();
return;
}
}
userData.add(new User(name,password));
updateData();
doCancel();
}
else{
String m="用户名或者密码是空!!!";
this.showMessage(m);
doCancel();
}
}
public void doLogin(){
String name = nameArea.getText();
String password = passwordArea.getText();
for(int i=0;i<userData.size();i++){
if(name.equals(userData.get(i).getName())&&password.equals(userData.get(i).getPassword())){
String m = "登陆成功";
this.showMessage(m);
this.doCancel();
return;
}
}
String m ="用户名和密码不匹配!!";
this.showMessage(m);
this.doCancel();
}
public void doCancel(){
nameArea.setText("");
passwordArea.setText("");
}
class ButtonMonitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Register")){
doRegister();
}else if(e.getActionCommand().equals("Login")){
doLogin();
}else if(e.getActionCommand().equals("Cancel")){
doCancel();
}
}
}
class User{
String name;
String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String toString(){
return name+" "+password;
}
}
}
给你随便写了一下。。。简单登陆用的话用JDialog就够了不必要用JFrame
其实最好是用数据库
热心网友 时间:2022-06-20 09:02
在你的登陆那部分添加下面一段!
try {
BufferedReader input = new BufferedReader(new FileReader(
"M:/a.txt"));
System.out.println("读取文件成功");
String line = null;
line = input.readLine();
System.out.println(line);
if (line.equals(d06.jtf.getText())) {
line = input.readLine();
if (line.substring(1).equals(
String.valueOf(d06.pwd.getPassword()))) {
System.out.println("账号密码一致");
} else {
System.out.println("账号密码不一致");
}
} else {
System.out.println("账号密码不一致");
}
} catch (Exception ee) {
System.out.println("读文件过程中出错");
}
热心网友 时间:2022-06-20 09:02
是要多个用户吗???
那么最好考虑使用数据库。
热心网友 时间:2022-06-20 09:03
学习下...