java使用对象输入/输出流完成用户注册登录界面,User类(用户名,密码)保存在工程文件夹下res\password.txt中 。(仿P204-例题10-12),使用消息框给出相关信息(自学JOptionPane的showMessageDialog方法的使用)
时间: 2024-01-25 18:02:35 浏览: 58
用面向对象的思想实现:用户注册、登录、修改密码
以下是一个使用对象输入/输出流完成用户注册登录界面的Java代码示例,你可以参考一下:
User.java
```java
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
```
RegisterForm.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class RegisterForm extends JFrame implements ActionListener {
private JLabel lblUsername, lblPassword;
private JTextField txtUsername, txtPassword;
private JButton btnRegister;
public RegisterForm() {
setTitle("User Registration");
setSize(300, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblUsername = new JLabel("Username:");
lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JTextField(20);
btnRegister = new JButton("Register");
btnRegister.addActionListener(this);
Container container = getContentPane();
container.setLayout(new GridLayout(3, 2));
container.add(lblUsername);
container.add(txtUsername);
container.add(lblPassword);
container.add(txtPassword);
container.add(btnRegister);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRegister) {
String username = txtUsername.getText();
String password = txtPassword.getText();
try {
File file = new File("res/password.txt");
if (!file.exists()) {
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof User) {
User user = (User) obj;
if (user.getUsername().equals(username)) {
JOptionPane.showMessageDialog(this, "Username already exists.");
return;
}
}
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new User(username, password));
oos.close();
JOptionPane.showMessageDialog(this, "User registered successfully.");
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
RegisterForm registerForm = new RegisterForm();
registerForm.setVisible(true);
}
}
```
LoginForm.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class LoginForm extends JFrame implements ActionListener {
private JLabel lblUsername, lblPassword;
private JTextField txtUsername, txtPassword;
private JButton btnLogin;
public LoginForm() {
setTitle("User Login");
setSize(300, 140);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblUsername = new JLabel("Username:");
lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JTextField(20);
btnLogin = new JButton("Login");
btnLogin.addActionListener(this);
Container container = getContentPane();
container.setLayout(new GridLayout(3, 2));
container.add(lblUsername);
container.add(txtUsername);
container.add(lblPassword);
container.add(txtPassword);
container.add(btnLogin);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnLogin) {
String username = txtUsername.getText();
String password = txtPassword.getText();
try {
File file = new File("res/password.txt");
if (!file.exists()) {
JOptionPane.showMessageDialog(this, "User does not exist.");
return;
}
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (!(obj instanceof User)) {
JOptionPane.showMessageDialog(this, "User does not exist.");
return;
}
User user = (User) obj;
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
JOptionPane.showMessageDialog(this, "Login successful.");
} else {
JOptionPane.showMessageDialog(this, "Username or password is incorrect.");
}
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
LoginForm loginForm = new LoginForm();
loginForm.setVisible(true);
}
}
```
在这个示例中,我们使用了对象输入/输出流来将User对象序列化到文件中。RegisterForm类和LoginForm类分别代表用户注册和登录界面,通过JOptionPane的showMessageDialog方法来给出相关信息。注意,在读取文件时需要判断文件是否存在,并且在写入文件时需要先读取文件中的对象信息,判断用户是否已经存在。
阅读全文