swing操作将用户录入数据,包括:编号,密码,姓名,性别,电话号码进行有效性验证,无效则提示,有效且编号不重复的情况下添加到教师集合中Java代码实现
时间: 2024-03-05 16:51:24 浏览: 90
基于Java Swing的猜一猜小游戏【100011231】
可以使用如下的Java代码实现:
```java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
public class TeacherForm extends JFrame implements ActionListener {
private JTextField idField;
private JPasswordField passwordField;
private JTextField nameField;
private JRadioButton maleRadio;
private JRadioButton femaleRadio;
private JTextField phoneField;
private JButton submitButton;
private Set<Integer> idSet = new HashSet<>();
private Set<String> phoneSet = new HashSet<>();
public TeacherForm() {
super("教师信息录入");
JPanel panel = new JPanel(new GridLayout(6, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel idLabel = new JLabel("编号:");
idField = new JTextField(20);
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(20);
JLabel nameLabel = new JLabel("姓名:");
nameField = new JTextField(20);
JLabel genderLabel = new JLabel("性别:");
maleRadio = new JRadioButton("男");
femaleRadio = new JRadioButton("女");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleRadio);
genderGroup.add(femaleRadio);
JPanel genderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
genderPanel.add(maleRadio);
genderPanel.add(femaleRadio);
JLabel phoneLabel = new JLabel("电话号码:");
phoneField = new JTextField(20);
submitButton = new JButton("提交");
submitButton.addActionListener(this);
panel.add(idLabel);
panel.add(idField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(genderPanel);
panel.add(phoneLabel);
panel.add(phoneField);
panel.add(new JLabel(""));
panel.add(submitButton);
setContentPane(panel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private boolean validateData() {
String idStr = idField.getText();
String password = new String(passwordField.getPassword());
String name = nameField.getText();
String gender = maleRadio.isSelected() ? "男" : "女";
String phone = phoneField.getText();
// 编号必须为正整数
int id;
try {
id = Integer.parseInt(idStr);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "编号无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (id <= 0) {
JOptionPane.showMessageDialog(this, "编号无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (idSet.contains(id)) {
JOptionPane.showMessageDialog(this, "编号已存在,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
// 密码长度必须大于等于6
if (password.length() < 6) {
JOptionPane.showMessageDialog(this, "密码无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
// 姓名不能为空
if (name.isEmpty()) {
JOptionPane.showMessageDialog(this, "姓名无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
// 性别必须为男或女
if (!gender.equals("男") && !gender.equals("女")) {
JOptionPane.showMessageDialog(this, "性别无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
// 电话号码必须为11位数字
if (phone.length() != 11 || !phone.matches("\\d+")) {
JOptionPane.showMessageDialog(this, "电话号码无效,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (phoneSet.contains(phone)) {
JOptionPane.showMessageDialog(this, "电话号码已存在,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
if (validateData()) {
int id = Integer.parseInt(idField.getText());
String password = new String(passwordField.getPassword());
String name = nameField.getText();
String gender = maleRadio.isSelected() ? "男" : "女";
String phone = phoneField.getText();
// 添加教师信息到集合中
Teacher teacher = new Teacher(id, password, name, gender, phone);
idSet.add(id);
phoneSet.add(phone);
JOptionPane.showMessageDialog(this, "教师信息添加成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 清空表单
idField.setText("");
passwordField.setText("");
nameField.setText("");
maleRadio.setSelected(true);
phoneField.setText("");
}
}
}
public static void main(String[] args) {
TeacherForm form = new TeacherForm();
form.setVisible(true);
}
}
```
上述代码中,我们使用了Swing框架来构建了一个简单的用户界面,包含了教师信息的各个字段以及提交按钮。我们在`validateData`方法中对用户输入的数据进行了有效性验证,并使用`JOptionPane`来显示错误信息。如果输入的数据无效,程序会提示用户重新输入,直到输入的数据有效为止。如果输入的数据有效且编号不重复,程序会将教师信息添加到集合中,并用`JOptionPane`来显示添加成功的信息。程序还支持多次添加教师信息,用户可以在界面上输入教师信息,并点击提交按钮来提交数据。
阅读全文