⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingloginframe.java

📁 基于eclipse的工具开发代码
💻 JAVA
字号:
package com.cownew.ve;

import Java.awt.Font;
import Java.awt.GraphicsConfiguration;
import Java.awt.HeadlessException;
import Java.awt.Rectangle;
import Java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.PreparedStatement;
import Java.sql.ResultSet;
import Java.sql.SQLException;

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.SwingUtilities;

public class SwingLoginFrame extends JFrame
{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JLabel labelUserName = null;
	private JTextField txtUserName = null;
	private JLabel labelPassword = null;
	private JPasswordField txtPassword = null;
	private JButton btnOK = null;
	private JButton btnExit = null;

	public SwingLoginFrame() throws HeadlessException
	{
		super();
		initialize();
	}

	public SwingLoginFrame(GraphicsConfiguration gc)
	{
		super(gc);
		initialize();
	}

	public SwingLoginFrame(String title) throws HeadlessException
	{
		super(title);
		initialize();
	}

	public SwingLoginFrame(String title, GraphicsConfiguration gc)
	{
		super(title, gc);
		initialize();
	}

	private JTextField getTxtUserName()
	{
		if (txtUserName == null)
		{
			txtUserName = new JTextField();
			txtUserName.setBounds(new Rectangle(91, 16, 105, 22));
		}
		return txtUserName;
	}

	private JPasswordField getTxtPassword()
	{
		if (txtPassword == null)
		{
			txtPassword = new JPasswordField();
			txtPassword.setBounds(new Rectangle(91, 51, 105, 25));
		}
		return txtPassword;
	}

	private JButton getBtnOK()
	{
		if (btnOK == null)
		{
			btnOK = new JButton();
			btnOK.setBounds(new Rectangle(30, 93, 75, 30));
			btnOK.setText("登录");
			btnOK.addActionListener(new Java.awt.event.ActionListener() {
				public void actionPerformed(Java.awt.event.ActionEvent e)
				{
					String userName = txtUserName.getText();
					String password = new String(txtPassword.getPassword());
					boolean isOK = false;
					try
					{
						isOK = isCorrect(userName, password);
					} catch (LoginException le)
					{
						JOptionPane.showMessageDialog(
SwingLoginFrame.this, le.getMessage());
						return;
					}
					if(isOK)
					{
						JOptionPane.showMessageDialog(
SwingLoginFrame.this,"登录成功!");
					}
					else
					{
						JOptionPane.showMessageDialog(
SwingLoginFrame.this,"用户名密码不符!");
					}
				}
			});
		}
		return btnOK;
	}

	private JButton getBtnExit()
	{
		if (btnExit == null)
		{
			btnExit = new JButton();
			btnExit.setBounds(new Rectangle(135, 93, 75, 30));
			btnExit.setText("退出");
		}
		return btnExit;
	}

	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				SwingLoginFrame thisClass = new SwingLoginFrame();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	private void initialize()
	{
		this.setSize(255, 164);
		this.setContentPane(getJContentPane());
		this.setTitle("登录");
	}

	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			labelPassword = new JLabel();
			labelPassword.setBounds(new Rectangle(19, 51, 39, 18));
			labelPassword.setText("密码");
			labelUserName = new JLabel();
			labelUserName.setBounds(new Rectangle(19, 16, 39, 18));
			labelUserName.setText("用户名");
			jContentPane = new JPanel();
			jContentPane.setFont(new Font("Dialog", Font.PLAIN, 12));
			jContentPane.setLayout(null);
			jContentPane.add(labelUserName, null);
			jContentPane.add(getTxtUserName(), null);
			jContentPane.add(labelPassword, null);
			jContentPane.add(getTxtPassword(), null);
			jContentPane.add(getBtnOK(), null);
			jContentPane.add(getBtnExit(), null);
		}
		return jContentPane;
	}
	
	private boolean isCorrect(String userName,String password) throws LoginException
	{
		if(userName==null||userName.trim().length()<=0)
		{
			throw new LoginException("用户名不能为空!");
		}
		
		if(password==null||password.trim().length()<=0)
		{
			throw new LoginException("密码不能为空!");
		}
		
		try
		{
			Class.forName("org.gjt.mm.mysql.Driver");
		} catch (ClassNotFoundException e)
		{
			throw new LoginException("加载JDBC驱动出错!",e);
		}
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try
		{
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test");
		} catch (SQLException e)
		{
			throw new LoginException("连接数据库出错!",e);
		}
		
		try
		{
			ps = conn.prepareStatement(
"select count(*) as c from T_User where FUserName=? and FName=?");
			ps.setString(1, userName);
			ps.setString(2, password);
			rs = ps.executeQuery();
			rs.next();
			return rs.getInt("c")>0;
		} catch (SQLException e)
		{
			throw new LoginException("执行SQL错误",e);
		}
		finally
		{
			closeConnection(conn, ps,rs);
		}
		
	}

	private void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs)
	{
		if(rs!=null)
		{
			try
			{
				rs.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if(ps!=null)
		{
			try
			{
				ps.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		if(conn!=null)
		{
			try
			{
				conn.close();
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -