以下是基于 Java 的养老院管理系统注册登录界面代码,包括注册和登录页面:
注册页面代码:
```
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Register extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7;
JTextField tf1, tf2, tf5, tf6, tf7;
JPasswordField p1, p2;
JButton btn1, btn2;
Register() {
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(80, 310, 400, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(tf7);
add(btn1);
add(btn2);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysqljdbc", "root", "root");
PreparedStatement ps = con.prepareStatement("insert into reg values(?,?,?,?,?,?,?)");
ps.setString(1, s1);
ps.setString(2, s2);
ps.setString(3, s8);
ps.setString(4, s9);
ps.setString(5, s5);
ps.setString(6, s6);
ps.setString(7, s7);
ResultSet rs = ps.executeQuery();
x++;
if (x > 0) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
}
} catch (Exception ex) {
System.out.println(ex);
}
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
}
}
}
```
登录页面代码:
```
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Login extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1;
JPasswordField pf2;
JButton btn1, btn2;
Login() {
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Login Form in Windows Form:");
l1 = new JLabel("Login Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Username:");
l3 = new JLabel("Password:");
tf1 = new JTextField();
pf2 = new JPasswordField();
btn1 = new JButton("Login");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
pf2.setBounds(300, 110, 200, 30);
btn1.setBounds(50, 160, 100, 30);
btn2.setBounds(170, 160, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(pf2);
add(btn1);
add(btn2);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
char[] s2 = pf2.getPassword();
String s3 = new String(s2);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysqljdbc", "root", "root");
PreparedStatement ps = con.prepareStatement("select * from reg where uname=? and pass=?");
ps.setString(1, s1);
ps.setString(2, s3);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(btn1, "You have successfully logged in");
} else {
JOptionPane.showMessageDialog(btn1, "Incorrect username or password");
}
} catch (Exception ex) {
System.out.println(ex);
}
} else {
tf1.setText("");
pf2.setText("");
}
}
```
注意:该代码需要连接 MySQL 数据库。如果您没有安装 MySQL 数据库或不知道如何连接数据库,请查阅相关资料。