java登录界面增删改减

时间: 2023-09-11 18:12:18 浏览: 37
好的,我可以为您提供一个可以增删改查用户的Java登录界面代码,您可以根据您的需求进行修改和优化。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; public class LoginGUI extends JFrame implements ActionListener { private JLabel userLabel, passwordLabel; private JTextField userField; private JPasswordField passwordField; private JButton loginButton, resetButton, addUserButton, deleteUserButton, updateUserButton; private JTextArea userArea; private ArrayList<User> userList; public LoginGUI() { setTitle("登录界面"); setSize(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets(10, 10, 10, 10); userLabel = new JLabel("用户名: "); constraints.gridx = 0; constraints.gridy = 0; panel.add(userLabel, constraints); userField = new JTextField(20); constraints.gridx = 1; constraints.gridy = 0; panel.add(userField, constraints); passwordLabel = new JLabel("密码: "); constraints.gridx = 0; constraints.gridy = 1; panel.add(passwordLabel, constraints); passwordField = new JPasswordField(20); constraints.gridx = 1; constraints.gridy = 1; panel.add(passwordField, constraints); loginButton = new JButton("登录"); loginButton.addActionListener(this); constraints.gridx = 0; constraints.gridy = 2; panel.add(loginButton, constraints); resetButton = new JButton("重置"); resetButton.addActionListener(this); constraints.gridx = 1; constraints.gridy = 2; panel.add(resetButton, constraints); addUserButton = new JButton("添加用户"); addUserButton.addActionListener(this); constraints.gridx = 2; constraints.gridy = 0; panel.add(addUserButton, constraints); deleteUserButton = new JButton("删除用户"); deleteUserButton.addActionListener(this); constraints.gridx = 2; constraints.gridy = 1; panel.add(deleteUserButton, constraints); updateUserButton = new JButton("修改用户"); updateUserButton.addActionListener(this); constraints.gridx = 2; constraints.gridy = 2; panel.add(updateUserButton, constraints); userArea = new JTextArea(10, 30); userArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(userArea); constraints.gridx = 3; constraints.gridy = 0; constraints.gridheight = 3; panel.add(scrollPane, constraints); add(panel); setVisible(true); userList = new ArrayList<User>(); userList.add(new User("admin", "admin", "管理员")); userList.add(new User("user", "user", "普通用户")); updateUserArea(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String user = userField.getText(); String password = new String(passwordField.getPassword()); User loginUser = null; for (User u : userList) { if (u.getUsername().equals(user) && u.getPassword().equals(password)) { loginUser = u; break; } } if (loginUser != null) { JOptionPane.showMessageDialog(this, "欢迎您!" + loginUser.getRole() + " " + loginUser.getUsername() + " 登录成功!"); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } else if (e.getSource() == resetButton) { userField.setText(""); passwordField.setText(""); } else if (e.getSource() == addUserButton) { String username = JOptionPane.showInputDialog(this, "请输入用户名:"); String password = JOptionPane.showInputDialog(this, "请输入密码:"); String role = JOptionPane.showInputDialog(this, "请输入角色:"); userList.add(new User(username, password, role)); updateUserArea(); } else if (e.getSource() == deleteUserButton) { String username = JOptionPane.showInputDialog(this, "请输入要删除的用户名:"); for (User u : userList) { if (u.getUsername().equals(username)) { userList.remove(u); break; } } updateUserArea(); } else if (e.getSource() == updateUserButton) { String username = JOptionPane.showInputDialog(this, "请输入要修改的用户名:"); for (User u : userList) { if (u.getUsername().equals(username)) { String newPassword = JOptionPane.showInputDialog(this, "请输入新密码:"); String newRole = JOptionPane.showInputDialog(this, "请输入新角色:"); u.setPassword(newPassword); u.setRole(newRole); break; } } updateUserArea(); } } public void updateUserArea() { userArea.setText(""); for (User u : userList) { userArea.append(u.getUsername() + " " + u.getPassword() + " " + u.getRole() + "\n"); } } public static void main(String[] args) { new LoginGUI(); } } class User { private String username; private String password; private String role; public User(String username, String password, String role) { this.username = username; this.password = password; this.role = role; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } } ``` 这个登录界面除了原来的用户名输入框、密码输入框、登录按钮和重置按钮外,还增加了添加用户按钮、删除用户按钮、修改用户按钮和用户信息显示区域。当用户点击添加用户按钮时,程序会弹出对话框让用户输入新用户的信息,然后将新用户添加到用户列表中并更新用户信息显示区域。当用户点击删除用户按钮时,程序会弹出对话框让用户输入要删除的用户名,然后在用户列表中查找该用户并将其删除,最后更新用户信息显示区域。当用户点击修改用户按钮时,程序会弹出对话框让用户输入要修改的用户名,然后在用户列表中查找该用户并将其密码和角色修改为新值,最后更新用户信息显示区域。 上面的代码使用了一个User类来表示用户,包括用户名、密码和角色三个属性,同时提供了对这些属性的访问和修改方法。用户列表使用了一个ArrayList<User>对象来存储,每次添加、删除或修改用户时都需要更新用户信息显示区域。希望能对您有所帮助!

相关推荐

最新推荐

recommend-type

element-ui tree结构实现增删改自定义功能代码

主要介绍了element-ui tree结构实现增删改自定义功能代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

VUE饿了么树形控件添加增删改功能的示例代码

本篇文章主要介绍了VUE饿了么树形控件添加增删改功能的示例代码,非常具有实用价值,有兴趣的可以了解一下
recommend-type

Java中SSM框架实现增删改查功能代码详解

主要介绍了Java中SSM框架实现增删改查功能代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

,MVC2与EF(Entity Framework)实现增删改的实例

以VS2010中MVC2为例,讲解EF简单入门使用。入门级也能看懂。网络资料共享。 更新,减少下载所需积分。
recommend-type

python连接mysql数据库示例(做增删改操作)

python连接mysql数据库示例,提供创建表,删除表,数据增、删、改,批量插入操作,大家参考使用吧
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

设计算法实现将单链表中数据逆置后输出。用C语言代码

如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct node { int data; struct node *next; }; // 定义单链表逆置函数 struct node* reverse(struct node *head) { struct node *prev = NULL; struct node *curr = head; struct node *next; while (curr != NULL) { next
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。