public class StudentRegedit extends JFrame { // 学生学号:输入框,学生密码:输入框,注册按钮, // 点击注册按钮,注册成功提示注册成功,进入登陆成功页面, // 用IOUtils中的writerFileByXuLie和readFileByXuLie将信息写入students.txt文件; // 注册不成功提示注册失败(学号相同不可以注册) public StudentRegedit(String title) throws HeadlessException { super(title); //2 获取面板 Container container = this.getContentPane(); //3 往面板加入组件:2个JLabel和2个文本输入框 ,1 个登录按钮 //创建面板:方便修改位置 JPanel jPanel = new JPanel(); jPanel.setLayout(null); //布局设置为空,之后可以手动设置组件的坐标位置和大小 //创建JLabel(学生学号) JLabel stuNoLabel = new JLabel("学生学号:"); stuNoLabel.setFont(new Font("微软雅黑", 0, 13)); //定义组件的位置和宽高 stuNoLabel.setBounds(10, 20, 80, 25); //把组件添加到JPanel上 jPanel.add(stuNoLabel); //创建文不域用于用户输入学生学号 JTextField stuNoField = new JTextField(20); //设置文本域的位置和宽高 stuNoField.setBounds(100, 20, 165, 25); //把文本域组件添加上 jPanel.add(stuNoField); //创建JLabel(密码) JLabel stuPassLabel = new JLabel("学生密码:"); stuPassLabel.setFont(new Font("微软雅黑", 0, 13)); //设置位置和大小 stuPassLabel.setBounds(10, 50, 80, 25); //添加组件 jPanel.add(stuPassLabel);补全
时间: 2023-06-16 15:03:28 浏览: 66
//创建文本域用于用户输入学生密码
JPasswordField stuPassField = new JPasswordField(20);
//设置文本域的位置和宽高
stuPassField.setBounds(100, 50, 165, 25);
//添加组件
jPanel.add(stuPassField);
//创建注册按钮
JButton regBtn = new JButton("注册");
//设置按钮的位置和宽高
regBtn.setBounds(130, 90, 80, 25);
//添加按钮组件
jPanel.add(regBtn);
//为注册按钮添加点击事件监听器
regBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取学生学号和密码
String stuNo = stuNoField.getText().trim();
String stuPass = new String(stuPassField.getPassword()).trim();
//判断学号是否已被注册
if (isStuNoExist(stuNo)) {
JOptionPane.showMessageDialog(null, "注册失败,学号已被注册");
} else {
//将学生信息写入文件
Student student = new Student(stuNo, stuPass);
IOUtils.writerFileByXuLie("students.txt", student);
JOptionPane.showMessageDialog(null, "注册成功");
//跳转到登陆页面
new StudentLogin("学生登陆").setVisible(true);
dispose();
}
}
});
//设置面板的位置和宽高
jPanel.setBounds(0, 0, 300, 150);
//将面板添加到容器上
container.add(jPanel);
}
/**
* 判断学号是否已被注册
*
* @param stuNo 学生学号
* @return true-已被注册,false-未被注册
*/
private boolean isStuNoExist(String stuNo) {
List<Student> studentList = IOUtils.readFileByXuLie("students.txt");
for (Student student : studentList) {
if (student.getStuNo().equals(stuNo)) {
return true;
}
}
return false;
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)