awt实现登陆界面+数据库

public class AwtTest extends Frame implements ActionListener{MenuItem menuItem1 = null;Menu menu3 = null;Button b = null;TextField text = null;public AwtTest(){this.setLayout(null);thi
摘要由CSDN通过智能技术生成

public class AwtTest extends Frame implements ActionListener{

MenuItem menuItem1 = null;

Menu menu3 = null;

Button b = null;

TextField text = null;

public AwtTest(){

this.setLayout(null);

this.setBounds(0, 0,500,500);

this.setResizable(false);

MenuBar menuBar = new MenuBar();

Menu menu1= new Menu("学员管理");

Menu menu2 = new Menu("班级管理");

menuItem1 = new MenuItem("添加学员");

MenuItem menuItem2 = new MenuItem("删除学员");

MenuItem menuItem3 = new MenuItem("添加班级"

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是具体实现代码,我将分别给出登录和注册表单的代码: 登录表单: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class LoginForm extends JFrame implements ActionListener { private JLabel titleLabel, usernameLabel, passwordLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; private Connection connection; public LoginForm() { // 设置窗体标题 setTitle("登录"); // 创建组件 titleLabel = new JLabel("登录"); titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); usernameLabel = new JLabel("用户名:"); passwordLabel = new JLabel("密码:"); usernameField = new JTextField(20); passwordField = new JPasswordField(20); loginButton = new JButton("登录"); // 设置布局 setLayout(new BorderLayout()); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); panel.add(usernameLabel); panel.add(usernameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(new JLabel()); panel.add(loginButton); add(titleLabel, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); // 添加监听器 loginButton.addActionListener(this); // 连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "123456"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } // 设置窗体属性 setSize(400, 200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM Users WHERE username='" + username + "' AND password='" + password + "'"); if (resultSet.next()) { JOptionPane.showMessageDialog(this, "登录成功!"); dispose(); // 进入应用程序主界面 } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } catch (SQLException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new LoginForm(); } } ``` 注册表单: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class RegisterForm extends JFrame implements ActionListener { private JLabel titleLabel, usernameLabel, passwordLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton registerButton; private Connection connection; public RegisterForm() { // 设置窗体标题 setTitle("注册"); // 创建组件 titleLabel = new JLabel("注册"); titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); usernameLabel = new JLabel("用户名:"); passwordLabel = new JLabel("密码:"); usernameField = new JTextField(20); passwordField = new JPasswordField(20); registerButton = new JButton("注册"); // 设置布局 setLayout(new BorderLayout()); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); panel.add(usernameLabel); panel.add(usernameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(new JLabel()); panel.add(registerButton); add(titleLabel, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); // 添加监听器 registerButton.addActionListener(this); // 连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "123456"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } // 设置窗体属性 setSize(400, 200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == registerButton) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM Users WHERE username='" + username + "'"); if (resultSet.next()) { JOptionPane.showMessageDialog(this, "用户名已存在!"); } else { statement.executeUpdate("INSERT INTO Users (username, password) VALUES ('" + username + "', '" + password + "')"); JOptionPane.showMessageDialog(this, "注册成功!"); dispose(); // 进入应用程序主界面 } } catch (SQLException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new RegisterForm(); } } ``` 以上代码仅供参考,你可以根据实际需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值