android简单的登录界面代码_java简单的用户登录界面+mysql

1.概述

一个简单的swing登录界面,使用了简单的JDBC. 如图:

4a33ac26b46b81d852097db380022577.png

31516b8f0366b0f16cccb0851bf1ea05.png

2.UI

(1)主界面

主界面使用了31网格布局+三个JPanel,中间的JPanel使用了22网格布局:

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Enumeration;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class UserManagement
{
    private JFrame mainFrame = new JFrame("登录");
    private Container container = mainFrame.getContentPane();
    private JLabel titleLabel = new JLabel("登录/注册", JLabel.CENTER);

    private JPanel inputField = new JPanel();
    private JLabel usernameLabel = new JLabel("用户名:", JLabel.CENTER);
    private JTextField username = new JTextField();
    private JLabel passwordLabel = new JLabel("密码:", JLabel.CENTER);
    private JPasswordField password = new JPasswordField();

    private JPanel buttonField = new JPanel();
    private JButton save = new JButton("登录/注册");
    private JButton cancel = new JButton("取消");

    public UserManagement()
    {
        init();
        setFont(new Font("微软雅黑",Font.PLAIN,14));
        addEvent();
    }

    private void init()
    {
        container.setLayout(new GridLayout(3, 1, 0, 10));
        container.add(titleLabel);

        inputField.setLayout(new GridLayout(2, 2, 5, 5));
        inputField.add(usernameLabel);
        inputField.add(username);
        inputField.add(passwordLabel);
        inputField.add(password);
        container.add(inputField);

        buttonField.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
        buttonField.add(save);
        buttonField.add(cancel);
        container.add(buttonField);

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(300, 200);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }

    private void setFont(Font font)
    {
        FontUIResource fontRes = new FontUIResource(font);
        for(Enumeration<Object> keys = UIManager.getDefaults().keys();keys.hasMoreElements();)
        {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if(value instanceof FontUIResource)
                UIManager.put(key, fontRes);
        }
    }

    private void addEvent()
    {
        save.addActionListener(
            e->
            {
                User user = new User();
                user.setName(username.getText());
                user.setPassword(new String(password.getPassword()));
                if(DBUtils.exists(user))
                    new UserInformation(DBUtils.getByName(user.getName()));
                else
                    JOptionPane.showConfirmDialog(null,
                    "添加"+(DBUtils.add(user) ? "成功" : "失败"), "",JOptionPane.CLOSED_OPTION);
            }
        );

        cancel.addActionListener(
            e->
            {
                mainFrame.dispose();
            }
        );
    }

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

重点说一下几行代码:

mainFrame.setLocationRelativeTo(null);

使整个JFrame处于屏幕水平居中与垂直居中位置.

private void setFont(Font font)
{
    FontUIResource fontRes = new FontUIResource(font);
    for(Enumeration<Object> keys = UIManager.getDefaults().keys();keys.hasMoreElements();)
    {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if(value instanceof FontUIResource)
            UIManager.put(key, fontRes);
    }
}

设置所有组件的字体.

cancel.addActionListener(
  e->
    {
        mainFrame.dispose();
    }
);

按钮添加关闭窗口事件.

JOptionPane.showConfirmDialog(null,"添加"+(DBUtils.add(user) ? "成功" : "失败"), "",JOptionPane.CLOSED_OPTION);

提示信息框.

(2)用户信息界面

用户信息界面采用了31网格,同样3个JPanel,中间的JPanel布局为32网格.

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Enumeration;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class UserInformation
{
    private JFrame mainFrame = new JFrame("用户信息");
    private Container container = mainFrame.getContentPane();
    private JLabel titleLabel = new JLabel("用户信息", JLabel.CENTER);

    private JPanel inputField = new JPanel();
    private JLabel idLabel = new JLabel("Id",JLabel.CENTER);
    private JTextField id = new JTextField();
    private JLabel usernameLabel = new JLabel("Username", JLabel.CENTER);
    private JTextField username = new JTextField();
    private JLabel passwordLabel = new JLabel("Password", JLabel.CENTER);
    private JPasswordField password = new JPasswordField();

    private JPanel buttonField = new JPanel();
    private JButton update = new JButton("更新");
    private User user;

    public UserInformation(User user)
    {
        if(user == null)
            mainFrame.dispose();
        this.user = user;
        init();
        setFont(new Font("微软雅黑", Font.PLAIN, 14));
        addEvent();
    }

    private void init()
    {
        container.setLayout(new GridLayout(3,1,0,10));
        container.add(titleLabel);

        inputField.setLayout(new GridLayout(3,2,0,3));
        inputField.add(idLabel);
        inputField.add(id);
        id.setText(user.getId());
        id.setEditable(false);
        inputField.add(usernameLabel);
        username.setText(user.getName());
        inputField.add(username);
        inputField.add(passwordLabel);
        password.setText(user.getPassword());
        inputField.add(password);
        container.add(inputField);

        buttonField.setLayout(new FlowLayout());
        buttonField.add(update);
        container.add(buttonField);

        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setSize(300,250);
    }

    private void setFont(Font font)
    {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();)
        {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource)
                UIManager.put(key, fontRes);
        }
    }

    private void addEvent()
    {
        update.addActionListener(
            e->
            {
                user.setName(username.getText());
                user.setPassword(new String(password.getPassword()));
                JOptionPane.showConfirmDialog(null, "更新"+(DBUtils.modify(user) ? "成功" : "失败"),"确认",JOptionPane.CLOSED_OPTION);
            }
        );
    }
}

这个JFrame不能设置EXIT_ON_CLOSE.因为这不是"主窗体",不然的话点击关闭主窗体也没了.

mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

3.数据库操作

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class DBUtils
{
    private final static String driver = "com.mysql.cj.jdbc.Driver";
    private final static String username = "aa";
    private final static String password = "123456";
    private final static String url = "jdbc:mysql://127.0.0.1/user_test";
    private final static String insert = "insert into user(id,username,password) values(?,?,?)";
    private final static String update = "update user set username = ?,password = ? where id = ?";
    private final static String delete = "delete from user where id = ?";
    private final static String select = "select * from user where username = ?";

    private static Connection connection;

    static
    {
        try
        {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            connection = null;
        }
    }

    public static boolean exists(User user)
    {
        try
        {
            PreparedStatement exist = connection.prepareStatement(select);
            exist.setString(1,user.getName());
            ResultSet existResult = exist.executeQuery();
            return existResult.next();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean add(User user)
    {
        try
        {
            PreparedStatement add = connection.prepareStatement(insert);
            user.setId(UUID.randomUUID().toString().substring(0, 8));
            add.setString(1, user.getId());
            add.setString(2, user.getName());
            add.setString(3, user.getPassword());
            return add.executeUpdate() == 1;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean modify(User user)
    {
        try
        {
            PreparedStatement modify = connection.prepareStatement(update);
            System.out.println(user.getName());
            modify.setString(1, user.getName());
            modify.setString(2, user.getPassword());
            modify.setString(3, user.getId());
            return modify.executeUpdate() == 1;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean delete(User user)
    {
        if(exists(user))
        {
            try
            {
                PreparedStatement del = connection.prepareStatement(delete);
                del.setString(1, user.getId());
                return del.executeUpdate() == 1;
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static User getByName(String name)
    {
        try
        {
            PreparedStatement exist = connection.prepareStatement(select);
            exist.setString(1, name);
            ResultSet existResult = exist.executeQuery();
            if(existResult.next())
            {
                User user = new User();
                user.setId(existResult.getString("id"));
                user.setName(existResult.getString("username"));
                user.setPassword(existResult.getString("password"));
                return user;
            }
            return null;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}

注册驱动后,增删查改,就是注意一下mysql版本与驱动名对应.

4.完整代码

github 码云

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的任务,需要分步骤进行,以下是一个简单的流程: 1. 创建一个新的 Android 项目,打开 Android Studio。 2. 创建一个新的 XML 布局文件,用于用户注册和登录界面。 3. 在 XML 文件中添加所需的 UI 元素,如 EditText、Button、TextView 等。 4. 创建一个 Java 类,用于处理用户注册和登录逻辑。 5. 在 Java 类中编写连接 MySQL 数据库的代码。可以使用 JDBC 驱动程序来连接数据库。 6. 在 Android 项目的 build.gradle 文件中添加 MySQL JDBC 驱动程序的依赖项。 7. 在 AndroidManifest.xml 文件中添加 Internet 权限,以允许应用程序访问互联网。 下面是一个基本的代码示例,用于连接 MySQL 数据库并执行查询操作: ``` public class MySQLConnector { private static final String DB_DRIVER = "com.mysql.jdbc.Driver"; private static final String DB_URL = "jdbc:mysql://your-database-url"; private static final String DB_USER = "your-database-username"; private static final String DB_PASSWORD = "your-database-password"; public void connect() { try { Class.forName(DB_DRIVER); Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); Statement stmt = conn.createStatement(); String sql = "SELECT * FROM users"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { String username = rs.getString("username"); String password = rs.getString("password"); // Do something with the data... } rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个基础示例,您需要根据自己的具体情况进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值