From b674ec18c6070d45791545ba1ce6877029c03db1 Mon Sep 17 00:00:00 2001 From: skyofdream <1662992055@qq.com> Date: Wed, 15 May 2024 20:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exp11/Account.java | 23 ++++++++++ exp11/AccountDemo.java | 13 +++++- exp11/AccountManager.java | 90 +++++++++++++++++++++++++++++++++++++++ exp11/LoginFrame.java | 34 +++++++++++++++ exp11/RegisterFrame.java | 25 +++++++++++ exp11/Tools.java | 36 ++++++++++++++++ 6 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 exp11/Account.java create mode 100644 exp11/AccountManager.java create mode 100644 exp11/LoginFrame.java create mode 100644 exp11/RegisterFrame.java create mode 100644 exp11/Tools.java diff --git a/exp11/Account.java b/exp11/Account.java new file mode 100644 index 0000000..a800382 --- /dev/null +++ b/exp11/Account.java @@ -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; + } +} diff --git a/exp11/AccountDemo.java b/exp11/AccountDemo.java index 075b08b..269584d 100644 --- a/exp11/AccountDemo.java +++ b/exp11/AccountDemo.java @@ -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); + } + + + } diff --git a/exp11/AccountManager.java b/exp11/AccountManager.java new file mode 100644 index 0000000..47e37f2 --- /dev/null +++ b/exp11/AccountManager.java @@ -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 getAccounts() { + File userinfo = new File(userinfoPath); + ArrayList 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 accounts = getAccounts(); + for (Account a : accounts) { + if(a.getUserName() == username) { + account = a; + } + } + + return account; + } + + public boolean userIsExist(String username) { + ArrayList 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; + } + + +} diff --git a/exp11/LoginFrame.java b/exp11/LoginFrame.java new file mode 100644 index 0000000..6f9f418 --- /dev/null +++ b/exp11/LoginFrame.java @@ -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; + } + +} diff --git a/exp11/RegisterFrame.java b/exp11/RegisterFrame.java new file mode 100644 index 0000000..8f6f2b9 --- /dev/null +++ b/exp11/RegisterFrame.java @@ -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; + } + +} diff --git a/exp11/Tools.java b/exp11/Tools.java new file mode 100644 index 0000000..21ec923 --- /dev/null +++ b/exp11/Tools.java @@ -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; + } + + +} \ No newline at end of file