JavaSwing注册

package gui.compont;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @author 华瑞汤永红
 * @version 1.0
 * @data 2022-11-21 9:28
 */
public class RegisterForm extends JFrame {
    /**
     * 编号(序号)
     */
    JLabel lblId;
    JTextField txtId;
    /**
     * 学号
     */
    JLabel lblSid;
    JTextField txtSid;
    /**
     * 姓名
     */

    JLabel lblName;
    JTextField txtName;
    /**
     * 身份证
     */
    JLabel lblIdCard;
    JTextField txtIdCard;
    /**
     * 爱好
     */
    JLabel lblHobbies;
    JCheckBox cobGame;
    JCheckBox cobCode;
    JCheckBox cobSport;
    /**
     * 学历
     */
    JLabel lblQue;
    JComboBox comQue;
    /**
     * 电话
     */
    JLabel lblPhone;
    JTextField txtPhone;
    /**
     * 联系地址
     */
    JLabel lblAddress;
    JTextField txtAddress;
    /**
     * 备注
     */
    JLabel lblComment;
    JTextArea txtComment;
    /**
     * 头像
     */
    JLabel lblHeadIcon;
    JTextArea txtHeadIcon;
    /**
     * 婚否
     */
    JLabel lblIsMarried;
    JCheckBox ckIsMarried;

    /**
     *
     */
    JButton btnReg;
    JButton btnClose;
    //
    private ImageIcon image;

    public RegisterForm() {
        URL resource = RegisterForm.this.getClass().getClassLoader().getResource("images/logo.png");
        Image image = resource != null ? Toolkit.getDefaultToolkit().getImage(resource) : null;
        setIconImage(image);
        setTitle("学员注册");
        setSize(320, 700);
        setLayout(null);
        /**
         * 编号
         */
        lblId = new JLabel("编号");
        txtId = new MyTextField();

        //定位
        lblId.setBounds(50, 30, 80, 30);

        txtId.setLocation(100, 30);
        /**
         * 学号
         */
        lblSid = new JLabel("学号");
        txtSid = new MyTextField();
        //定位
        lblSid.setBounds(50, 80, 80, 30);

        txtSid.setLocation(100, 80);
        /**
         * 姓名
         */
        lblName = new JLabel("姓名");
        txtName = new MyTextField();
        lblName.setBounds(50, 130, 80, 30);

        txtName.setLocation(100, 130);
        /**
         * 身份证
         */
        lblIdCard = new JLabel("身份证");
        txtIdCard = new MyTextField();
        lblIdCard.setBounds(50, 180, 80, 30);

        txtIdCard.setLocation(100, 180);
        /**
         * 爱好
         */
        lblHobbies = new JLabel("爱好");
        cobGame = new JCheckBox("游戏");
        cobCode = new JCheckBox("编码");
        cobSport = new JCheckBox("运动");
        lblHobbies.setBounds(50, 230, 80, 30);
        cobGame.setBounds(100, 230, 60, 30);
        cobCode.setBounds(160, 230, 60, 30);
        cobSport.setBounds(220, 230, 60, 30);
        cobGame.setOpaque(false);
        cobCode.setOpaque(false);
        cobSport.setOpaque(false);
        /**
         * 学历
         */
        lblQue = new JLabel("学历");
        String names[] = {"请选择", "研究生", "本科", "大专"};
        comQue = new JComboBox(names);
        comQue.setOpaque(false);
        lblQue.setBounds(50, 280, 80, 30);
        comQue.setBounds(100, 280, 150, 30);
        /**
         * 电话
         */
        lblPhone = new JLabel("电话");
        txtPhone = new MyTextField();
        lblPhone.setBounds(50, 330, 80, 30);

        txtPhone.setLocation(100, 330);
        /**
         * 联系地址
         */
        lblAddress = new JLabel("地址");
        txtAddress = new MyTextField();
        lblAddress.setBounds(50, 380, 80, 30);

        txtAddress.setLocation(100, 380);
        /**
         * 备注
         */
        lblComment = new JLabel("备注");
        txtComment = new JTextArea();

        lblComment.setBounds(50, 430, 80, 30);
        txtComment.setBounds(100, 430, 150, 30);
        /**
         * 头像
         */
        lblHeadIcon = new JLabel("头像");
        txtHeadIcon = new JTextArea();

        lblHeadIcon.setBounds(50, 480, 80, 30);
        txtHeadIcon.setBounds(100, 480, 150, 30);
        txtHeadIcon.setLocation(100, 480);
        /**
         * 婚否
         */
        lblIsMarried = new JLabel("婚否");
        ckIsMarried = new JCheckBox();
        ckIsMarried.setOpaque(false);
        lblIsMarried.setBounds(50, 530, 80, 30);
        ckIsMarried.setBounds(100, 530, 120, 30);

        btnReg = new JButton("注册");
        btnClose = new JButton("关闭");
        btnReg.setBounds(50, 580, 100, 30);
        btnClose.setBounds(160, 580, 100, 30);
        //
        add(lblId);
        add(txtId);
        add(lblSid);
        add(txtSid);
        add(lblName);
        add(txtName);
        add(lblIdCard);
        add(txtIdCard);
        add(lblHobbies);
        add(cobGame);
        add(cobCode);
        add(cobSport);
        add(lblQue);
        add(comQue);
        add(lblPhone);
        add(txtPhone);

        add(lblAddress);
        add(txtAddress);
        add(lblComment);
        add(txtComment);
        add(lblHeadIcon);
        add(txtHeadIcon);
        add(lblIsMarried);
        add(ckIsMarried);
        add(btnReg);
        add(btnClose);
        //放背景
        JLabel lblBg = new JLabel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                URL url = RegisterForm.this.getClass().getClassLoader().getResource("gui\\compont\\bg.PNG");
                ImageIcon image = new ImageIcon(url);
                image.paintIcon(this, g, 0, 0);

            }
        };
        lblBg.setBounds(0, 0, 320, 700);
        //lblBg.setOpaque(true);
        //lblBg.setBackground(Color.PINK);
        //C:\code\java106\s1\javabase\javaoop\src\gui\compont\bg.PNG
        //URL url = RegisterForm.this.getClass().getClassLoader().getResource("gui\\compont\\bg.PNG");
        //ImageIcon icon = new ImageIcon(url);
        //lblBg.setIcon(icon);
        add(lblBg);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        new RegisterForm();
    }
}


class MyTextField extends JTextField {
    public MyTextField() {
        setOpaque(false);
        setSize(150, 30);
        //setBorder(null);
        //setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }

}

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一些简单的步骤来实现一个 Java Swing MySQL GUI 登录注册页面。 首先,您需要创建一个 MySQL 数据库,包括一个用户表,其中包含用户名和密码字段。接下来,您需要创建一个 Java Swing 界面,包括登录和注册选项卡。 对于登录选项卡,您需要创建一个包含用户名和密码输入字段的表单,并在提交按钮上添加一个 ActionListener,该 ActionListener 将检查输入的用户名和密码是否匹配数据库中的值。 对于注册选项卡,您需要创建一个包含用户名和密码输入字段的表单,并在提交按钮上添加一个 ActionListener,该 ActionListener 将检查输入的用户名是否已经存在于数据库中。如果不存在,则将该用户名和密码添加到用户表中。 以下是一个简单的示例代码,它可以帮助您开始创建 Java Swing MySQL GUI 登录注册页面: ```java import java.sql.*; import javax.swing.*; public class LoginGUI extends JFrame { private JTextField tfUsername; private JPasswordField pfPassword; private JButton btnLogin; private JButton btnRegister; // 定义 MySQL 数据库连接信息 private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String DB_USER = "root"; private static final String DB_PASS = "password"; public LoginGUI() { super("Login or Register"); // 创建登录和注册选项卡 JTabbedPane tabbedPane = new JTabbedPane(); JPanel loginPanel = createLoginPanel(); JPanel registerPanel = createRegisterPanel(); tabbedPane.addTab("Login", loginPanel); tabbedPane.addTab("Register", registerPanel); // 添加选项卡到主窗口 this.add(tabbedPane); // 设置窗口大小和可见性 this.setSize(300, 150); this.setVisible(true); } // 创建登录选项卡 private JPanel createLoginPanel() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 添加用户名和密码字段 JLabel lbUsername = new JLabel("Username:"); tfUsername = new JTextField(20); JLabel lbPassword = new JLabel("Password:"); pfPassword = new JPasswordField(20); panel.add(lbUsername); panel.add(tfUsername); panel.add(lbPassword); panel.add(pfPassword); // 添加登录按钮 btnLogin = new JButton("Login"); btnLogin.addActionListener(e -> login()); panel.add(btnLogin); return panel; } // 创建注册选项卡 private JPanel createRegisterPanel() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 添加用户名和密码字段 JLabel lbUsername = new JLabel("Username:"); tfUsername = new JTextField(20); JLabel lbPassword = new JLabel("Password:"); pfPassword = new JPasswordField(20); panel.add(lbUsername); panel.add(tfUsername); panel.add(lbPassword); panel.add(pfPassword); // 添加注册按钮 btnRegister = new JButton("Register"); btnRegister.addActionListener(e -> register()); panel.add(btnRegister); return panel; } // 登录方法 private void login() { try { // 获取数据库连接 Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); // 查询用户表是否存在匹配的记录 String query = "SELECT * FROM users WHERE username = ? AND password = ?"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, tfUsername.getText()); stmt.setString(2, new String(pfPassword.getPassword())); ResultSet rs = stmt.executeQuery(); // 如果找到匹配记录,则登录成功 if (rs.next()) { JOptionPane.showMessageDialog(this, "Login successful!"); } else { JOptionPane.showMessageDialog(this, "Invalid username or password."); } // 关闭数据库连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } // 注册方法 private void register() { try { // 获取数据库连接 Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); // 检查用户名是否已经存在 String query = "SELECT * FROM users WHERE username = ?"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, tfUsername.getText()); ResultSet rs = stmt.executeQuery(); // 如果用户名已经存在,则注册失败 if (rs.next()) { JOptionPane.showMessageDialog(this, "Username already exists."); } else { // 否则,将新用户添加到用户表中 query = "INSERT INTO users (username, password) VALUES (?, ?)"; stmt = conn.prepareStatement(query); stmt.setString(1, tfUsername.getText()); stmt.setString(2, new String(pfPassword.getPassword())); stmt.executeUpdate(); JOptionPane.showMessageDialog(this, "Registration successful!"); } // 关闭数据库连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } public static void main(String[] args) { // 启动应用程序 new LoginGUI(); } } ``` 请注意,此代码仅为示例代码,应根据您的具体要求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值