3.软件能够展示个人班级学号姓名等信息。(通过菜单和对话框实现)(10分) 4.软件能够实现注册用户信息功能(用户信息需保存在数据库中)。(10分) 5.软件能够实现登录功能(登录信息需要匹配数据库中的用户信息,若用户名不存在给出用户名不存在的提示;若密码不正确,给出密码不正确的提示;用户名和密码信息正确时显示登录成功,并进入计算器界面)。(10分) 6.上题中的提示信息需要通过对话框的形式显示。(10分) 使用java实现并给出完整代码
根据您的要求,下面是一个简化基于Java的示例程序,该程序实现了展示个人信息、注册用户、登录功能及使用对话框提示错误信息。它使用了Swing库来创建,使用了一个简化版的“数据库”,实际上只是一个内存中的HashMap来存储用户信息,以简化演示。
:实际应用中您需要连接真实的,而不是使用HashMap,以及涉及加密存储密码、错误处理等安全和健壮性考虑,里仅为示例。
import javax.swingimport java.awt.*; import java.awt.event.ActionEvent; import java.event.ActionListener; import java.util.HashMap; import java.util.Map; // Assume we have a User class to store user information class User String username; String password; String fullName; String classNumber; String studentID; public User(String username, String password, String fullName, String classNumber, String studentID) { this.username = username; this.password = password; this.fullName = fullName; this.classNumber = classNumber; this.studentID = studentID; } } public class SimpleApp extends JFrame { // "Database" of users private Map<String, User> users = new HashMap<>(); // Constructor public SimpleApp() { createUserInterface(); } // Create GUI private void createUserInterface() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(, 200); setLocationRelativeTo(null); // Registration button JButton btnRegister = new JButton("Register"); btnRegister.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { User(); } }); // Login button JButton btnLogin = new JButton("Login"); btnLogin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { loginUser(); } }); // Layout setLayout(new FlowLayout()); add(btnRegister); add(btnLogin); } // user function private void registerUser() { JTextField usernameField = new JTextField(5); JTextField passwordField = new JTextField(5); JTextField fullNameField = new JTextField(5); JTextField classField = new JTextField(5); JTextField studentIDField = new JTextField(5 JPanel panel = new JPanel(new GridLayout(0, )); panel.add(new JLabel("Username:")); panel.add(usernameField); panel.add(new JLabel("Password:")); panel.add(passwordField); panel.add(new JLabel("Full Name:")); panel.add(fullNameField); panel.add(new JLabel("Class Number:")); panel.add(classField); panel.add(new JLabel("Student ID:")); panel.add(studentIDField); int result = JOptionPane.showConfirmDialog(null,, "Register", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE if (result == JOptionPane.OK_OPTION) { // This is where you would normally check for valid input and handle errors User = new User(usernameField.getText(), passwordField.getText(), fullNameField.getText(), classNumberField.getText(), studentIDField.getText()); users.put(newUser.username, newUser); JOptionPane.showMessageDialog(this, "User " + newUser.username + " registered successfully!"); } } // Login user function private void loginUser() { JTextField usernameField = new JTextField(5); JTextField passwordField = new JTextField(5); JPanel panel = new JPanel(new GridLayout(0, 2)); panel.add JLabel("Username:")); panel.add(usernameField); panel.add(new JLabel("Password:")); panel.add(passwordField); int result = JOptionPane.showConfirmDialog, panel, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { User user = users.get(usernameField.getText()); if (user == null) { JOptionPane.showMessageDialog(this, " does not exist."); } else if (!user.password.equals(passwordField.getText())) { JOptionPane.showMessageDialog(this, "Password is incorrect."); } else { JOptionPane(this, "Login successful. Welcome, " + user.fullName + "!"); // Here you would navigate to the calculator screen or another part of the application } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SimpleApp().setVisible(true); } }); } }
请注意,您际上需要在实际的Java环境中运行此代码,其中括正确处理多线程GUI更新(invokeLater