JAVA图书管理系统登陆、注册和添加MySQL创建GUI代码
时间: 2024-02-07 07:03:22 浏览: 23
以下是Java图书管理系统的登陆、注册和添加MySQL创建GUI代码的示例:
1. 登陆界面代码
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
JLabel l1, l2;
JTextField tf1;
JPasswordField pf1;
JButton b1, b2;
Login() {
setTitle("登陆");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
l1 = new JLabel("用户名:");
l1.setBounds(20, 20, 80, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(100, 20, 150, 20);
add(tf1);
l2 = new JLabel("密码:");
l2.setBounds(20, 50, 80, 20);
add(l2);
pf1 = new JPasswordField();
pf1.setBounds(100, 50, 150, 20);
add(pf1);
b1 = new JButton("登陆");
b1.setBounds(20, 80, 100, 20);
add(b1);
b1.addActionListener(this);
b2 = new JButton("注册");
b2.setBounds(150, 80, 100, 20);
add(b2);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
String username = tf1.getText();
String password = new String(pf1.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登陆成功");
dispose();
new Main();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
} else if (ae.getSource() == b2) {
dispose();
new Register();
}
}
public static void main(String[] args) {
new Login();
}
}
```
2. 注册界面代码
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Register extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1, tf2;
JPasswordField pf1;
JButton b1, b2;
Register() {
setTitle("注册");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
l1 = new JLabel("用户名:");
l1.setBounds(20, 20, 80, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(100, 20, 150, 20);
add(tf1);
l2 = new JLabel("密码:");
l2.setBounds(20, 50, 80, 20);
add(l2);
pf1 = new JPasswordField();
pf1.setBounds(100, 50, 150, 20);
add(pf1);
l3 = new JLabel("确认密码:");
l3.setBounds(20, 80, 80, 20);
add(l3);
tf2 = new JTextField();
tf2.setBounds(100, 80, 150, 20);
add(tf2);
b1 = new JButton("注册");
b1.setBounds(20, 110, 100, 20);
add(b1);
b1.addActionListener(this);
b2 = new JButton("返回");
b2.setBounds(150, 110, 100, 20);
add(b2);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
String username = tf1.getText();
String password = new String(pf1.getPassword());
String confirmPassword = tf2.getText();
if (password.equals(confirmPassword)) {
// 将用户名和密码保存到数据库中
// ...
JOptionPane.showMessageDialog(this, "注册成功");
dispose();
new Login();
} else {
JOptionPane.showMessageDialog(this, "密码不一致");
}
} else if (ae.getSource() == b2) {
dispose();
new Login();
}
}
public static void main(String[] args) {
new Register();
}
}
```
3. 添加MySQL代码示例
```
import java.sql.*;
public class AddBook {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "password");
stmt = conn.createStatement();
String sql = "INSERT INTO books (title, author, publisher, price) VALUES ('Java Programming', 'John Smith', 'ACME Publishing', 39.99)";
stmt.executeUpdate(sql);
System.out.println("添加成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
这些代码示例可以帮助你开始创建Java图书管理系统的登陆、注册和添加MySQL功能。但是,请注意这些代码示例并未完全实现所有功能,你需要根据自己的需求和情况进行修改和补充。