Java+Swing+Mysql实现超市管理系统
实现一个超市管理系统涉及许多组件和步骤,以下是一个简化的代码示例,展示了如何使用Java Swing和MySQL创建一个超市管理系统的用户登录界面。
import javax.swing.*;
import java.awt.*;
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 SuperMarketSystem {
private JFrame loginFrame;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userTextField;
private JPasswordField passwordField;
private JButton loginButton;
public SuperMarketSystem() {
initUI();
}
private void initUI() {
loginFrame = new JFrame("超市管理系统登录");
loginFrame.setSize(300, 200);
loginFrame.setLayout(new GridLayout(3, 2));
userLabel = new JLabel("用户名:");
passwordLabel = new JLabel("密码:");
userTextField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("登录");
loginButton.addActionListener(new LoginActionListener());
loginFrame.add(userLabel);
loginFrame.add(userTextField);
loginFrame.add(passwordLabel);
loginFrame.add(passwordField);
loginFrame.add(loginButton);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setLocationRelativeTo(null);
loginFrame.setVisible(true);
}
class LoginActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String username = userTextField.getText();
String password = new String(passwordField.getPassword());
if (authenticate(username, password)) {
JOptionPane.showMessageDialog(loginFrame, "登录成功!");
// 登录成功后的代码
} else {
JOptionPane.showMessageDialog(loginFrame, "登录失败,用户名或密码错误!");
}
}
}
private boolean authenticate(String username, String password) {
String url = "jdbc:mysql://localhost:3306/supermarketdb";
评论已关闭