同步
This commit is contained in:
parent
6d5c439fb2
commit
b674ec18c6
23
exp11/Account.java
Normal file
23
exp11/Account.java
Normal file
@ -0,0 +1,23 @@
|
||||
package exp11;
|
||||
|
||||
public class Account {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public Account(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
String getUserName() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.username +"," +this.password;
|
||||
}
|
||||
}
|
@ -1,7 +1,18 @@
|
||||
package exp11;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class AccountDemo {
|
||||
JFrame rejister = new JFrame();
|
||||
public static void main(String[] args) {
|
||||
JFrame register = LoginFrame.creatLonginFrame();
|
||||
|
||||
register.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
90
exp11/AccountManager.java
Normal file
90
exp11/AccountManager.java
Normal file
@ -0,0 +1,90 @@
|
||||
package exp11;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AccountManager {
|
||||
private String userinfoPath = "userinfo.txt";
|
||||
|
||||
private ArrayList<Account> getAccounts() {
|
||||
File userinfo = new File(userinfoPath);
|
||||
ArrayList<Account> accounts = new ArrayList<>();
|
||||
try{
|
||||
BufferedReader reader = new BufferedReader(new FileReader(userinfo));
|
||||
String line;
|
||||
while ((line=reader.readLine()) != null) {
|
||||
String[] strs = line.split(",");
|
||||
Account account = new Account(strs[0], strs[1]);
|
||||
accounts.add(account);
|
||||
}
|
||||
reader.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return accounts;
|
||||
}
|
||||
|
||||
Account getAccount(String username) {
|
||||
Account account = null;
|
||||
ArrayList<Account> accounts = getAccounts();
|
||||
for (Account a : accounts) {
|
||||
if(a.getUserName() == username) {
|
||||
account = a;
|
||||
}
|
||||
}
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public boolean userIsExist(String username) {
|
||||
ArrayList<Account> accounts = getAccounts();
|
||||
for (Account account : accounts) {
|
||||
if (username == account.getUserName()){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addUser(Account account) {
|
||||
if (userIsExist(account.getUserName())) {
|
||||
System.out.println("用户已存在!");
|
||||
}else {
|
||||
File userinfo = new File(userinfoPath);
|
||||
try{
|
||||
FileWriter writer = new FileWriter(userinfo, true);
|
||||
writer.write(account.toString());
|
||||
writer.close();
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
boolean usernameVerify(String username) {
|
||||
if(username.matches("a-zA-Z")){
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("用户名不合法");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
boolean accountVerify(String username, String password) {
|
||||
Account account = getAccount(username);
|
||||
if ((account.getUserName()==username) && (account.getPassword()==password)){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
34
exp11/LoginFrame.java
Normal file
34
exp11/LoginFrame.java
Normal file
@ -0,0 +1,34 @@
|
||||
package exp11;
|
||||
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class LoginFrame {
|
||||
static JFrame creatLonginFrame() {
|
||||
JFrame f = Tools.creatDefaultJFrame("登录");
|
||||
|
||||
JButton loginJButton = new JButton("登录");
|
||||
loginJButton.setBounds(240, 200, 60, 30);
|
||||
loginJButton.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e){
|
||||
System.out.println("点击[登录]");
|
||||
}
|
||||
});
|
||||
|
||||
JButton registerButton = new JButton("注册");
|
||||
registerButton.setBounds(90, 200, 60, 30);
|
||||
registerButton.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e){
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
f.add(loginJButton);
|
||||
f.add(registerButton);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
}
|
25
exp11/RegisterFrame.java
Normal file
25
exp11/RegisterFrame.java
Normal file
@ -0,0 +1,25 @@
|
||||
package exp11;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class RegisterFrame {
|
||||
static JFrame creatRegisterFrame() {
|
||||
JFrame f = Tools.creatDefaultJFrame("注册");
|
||||
|
||||
JButton registerButton = new JButton("注册");
|
||||
registerButton.setBounds(220, 200, 60, 30);
|
||||
registerButton.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
}
|
36
exp11/Tools.java
Normal file
36
exp11/Tools.java
Normal file
@ -0,0 +1,36 @@
|
||||
package exp11;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class Tools {
|
||||
private String userInfoPath = "userinfo.txt";
|
||||
|
||||
public static JFrame creatDefaultJFrame(String title){
|
||||
JFrame f = new JFrame(title);
|
||||
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
f.setSize(400, 300);
|
||||
f.setLayout(null);
|
||||
f.setLocationRelativeTo(f);
|
||||
f.setAlwaysOnTop(true);
|
||||
f.setResizable(false);
|
||||
|
||||
JLabel accountJLabel = new JLabel("账号");
|
||||
accountJLabel.setBounds(50, 50, 50, 20);
|
||||
JTextField accounJTextField = new JTextField();
|
||||
accounJTextField.setBounds(100, 50, 200, 20);
|
||||
|
||||
JLabel passwordJLabel = new JLabel("密码");
|
||||
passwordJLabel.setBounds(50, 100, 50, 20);
|
||||
JPasswordField passwordField = new JPasswordField();
|
||||
passwordField.setBounds(100, 100, 200, 20);
|
||||
|
||||
f.add(accountJLabel);
|
||||
f.add(accounJTextField);
|
||||
f.add(passwordJLabel);
|
||||
f.add(passwordField);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user