真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java簡(jiǎn)單的用戶登錄界面+mysql

@[TOC]

專注于為中小企業(yè)提供成都網(wǎng)站制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)金寨免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

1.概述

一個(gè)簡(jiǎn)單的swing登錄界面,使用了簡(jiǎn)單的JDBC.
如圖:
java簡(jiǎn)單的用戶登錄界面+mysql
java簡(jiǎn)單的用戶登錄界面+mysql

2.UI

(1)主界面

主界面使用了31網(wǎng)格布局+三個(gè)JPanel,中間的JPanel使用了22網(wǎng)格布局:

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("登錄/注冊(cè)", 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("登錄/注冊(cè)");
    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 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();
    }
}

重點(diǎn)說一下幾行代碼:

mainFrame.setLocationRelativeTo(null);

使整個(gè)JFrame處于屏幕水平居中與垂直居中位置.

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

設(shè)置所有組件的字體.

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

按鈕添加關(guān)閉窗口事件.

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

提示信息框.

(2)用戶信息界面

用戶信息界面采用了31網(wǎng)格,同樣3個(gè)JPanel,中間的JPanel布局為32網(wǎng)格.

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 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) ? "成功" : "失敗"),"確認(rèn)",JOptionPane.CLOSED_OPTION);
            }
        );
    }
}

這個(gè)JFrame不能設(shè)置EXIT_ON_CLOSE.因?yàn)檫@不是"主窗體",不然的話點(diǎn)擊關(guān)閉主窗體也沒了.

mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

3.數(shù)據(jù)庫操作

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;
        }
    }
}

注冊(cè)驅(qū)動(dòng)后,增刪查改,就是注意一下mysql版本與驅(qū)動(dòng)名對(duì)應(yīng).

4.完整代碼

github

碼云


網(wǎng)站標(biāo)題:java簡(jiǎn)單的用戶登錄界面+mysql
本文路徑:http://weahome.cn/article/ihcspd.html

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部