目录
案例要求
实现思路
第一步:创建一个模块做这个项目。itheima - employee - sys
第二步:分析项目的业务需求。
- 提供一个登录和注册用户的界面。
- 提供一个人事信息的管理界面:展示全部员工信息,提供一个根据名称查询某个员工信息展示,添加员工信息,删除员工信息,修改员工信息。
- 分析项目的角色:
-- 登录用户:登录名称,密码。
-- 员工信息:ID, 姓名,性别,年龄,电话,职位,入职时间,薪水,部门信息。
第三步:结合AI进行开发,使用ai生成gui界面
代码
App启动类
import com.itheima.ui.LoginUI;
public class App {
public static void main(String[] args) {
new LoginUI();
}
}
Bean包
Employee实体类
package com.itheima.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
// 员工信息:"ID", "姓名", "性别", "年龄", "电话" ,"职位", "入职日期", "薪水", "部门"
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private int id;
private String name;
private String sex;
private int age;
private String phone;
private String position;
private String entryDate;
private double salary;
private String department;
}
User实体类
package com.itheima.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
// 用户信息包含用户名,密码,登录名。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username; // 用户名
private String password; // 密码
private String loginName; // 登录名
}
ui包
AddEmployeeUI
package com.itheima.ui;
import com.itheima.bean.Employee;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
public class AddEmployeeUI extends JFrame {
private JTextField txtId, txtName, txtSex, txtAge, txtPhone, txtPosition, txtSalary, txtDepartment;
private JFormattedTextField txtHireDate;
private JButton btnSave, btnCancel;
private EmployeeManagerUI employeeManagerUI;
public AddEmployeeUI(EmployeeManagerUI employeeManagerUI) {
super("添加员工信息");
this.employeeManagerUI = employeeManagerUI;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
Font labelFont = new Font("楷体", Font.PLAIN, 14);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
JLabel idLabel = new JLabel("ID:");
idLabel.setFont(labelFont);
add(idLabel, gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
txtId = new JTextField(10);
add(txtId, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setFont(labelFont);
add(nameLabel, gbc);
gbc.gridx = 1;
txtName = new JTextField(10);
add(txtName, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
JLabel sexLabel = new JLabel("性别:");
sexLabel.setFont(labelFont);
add(sexLabel, gbc);
gbc.gridx = 1;
txtSex = new JTextField(10);
add(txtSex, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
JLabel ageLabel = new JLabel("年龄:");
ageLabel.setFont(labelFont);
add(ageLabel, gbc);
gbc.gridx = 1;
txtAge = new JTextField(10);
add(txtAge, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
JLabel phoneLabel = new JLabel("电话:");
phoneLabel.setFont(labelFont);
add(phoneLabel, gbc);
gbc.gridx = 1;
txtPhone = new JTextField(10);
add(txtPhone, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
JLabel positionLabel = new JLabel("职位:");
positionLabel.setFont(labelFont);
add(positionLabel, gbc);
gbc.gridx = 1;
txtPosition = new JTextField(10);
add(txtPosition, gbc);
gbc.gridx = 0;
gbc.gridy = 6;
JLabel hireDateLabel = new JLabel("入职日期:");
hireDateLabel.setFont(labelFont);
add(