窗体登录首页

package  cn.massz.tsgl.sort.view;
 
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
 
/**
 * @author LycanYao
 *
 *        
 */
public class Swingtest002 extends JFrame {
	static Statement st; 
    static{
    	try{
    		Class.forName("com.mysql.jdbc.Driver");
    		Connection  con=DriverManager.getConnection("jdbc:mysql://localhost:3306/tsgl?useUnicode=true&characterEncoding=utf8","root","root");	
    		st=con.createStatement();
    	}
    	catch(Exception e){}
    }
	
	public Swingtest002() {
		// 设置标题
		setTitle("请登陆");
		// 绝对布局
		setLayout(null);
		// 定义一个容器
		Container c = getContentPane();
		// 创建"用户名:"标签
		JLabel jl1 = new JLabel("用户名:");
		// 创建文本框
		final JTextField jtf1 = new JTextField();
		// 创建"密码:"标签
		JLabel jl2 = new JLabel("密码:");
		// 创建密码框
		final JPasswordField jpf1 = new JPasswordField();
		// 设置密码字符为*
		jpf1.setEchoChar('*');
		// 创建"提交"按钮
		JButton jb1 = new JButton("提交");
		// 创建"重置"按钮
		JButton jb2 = new JButton("重置");
		
		// 当用户名为"mr",密码为"mrsoft"时点击"提交"按钮弹出"登录成功"提示对话框
		jb1.addActionListener(new ActionListener() {
 
			@Override
			public void actionPerformed(ActionEvent e) {

				String  name=(String)jtf1.getText().trim();
				String  password=new String(jpf1.getText().trim());
				String sql="select * from user where name='"+name+"' and pass='"+password+"'";
			System.out.println("name"+name+"password"+password);
				// TODO Auto-generated method stub
				try {
					ResultSet rs=st.executeQuery(sql);
					if (rs.next()) {
						JOptionPane.showMessageDialog(null, "登陆成功!");
						this.setVisible(false);//关闭当前的窗体
					
						MainFrame frame = new MainFrame();
						frame.setVisible(true);
					
						
						
					} else if (jtf1.getText().trim().length() == 0 || new String(jpf1.getPassword()).trim().length() == 0) {
						JOptionPane.showMessageDialog(null, "不能为空!");
					} else {
						JOptionPane.showMessageDialog(null, "错误");
						// 清零
						jtf1.setText("");
						jpf1.setText("");
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
 
			}

			private void setVisible(boolean b) {
				// TODO Auto-generated method stub
				
			}
		});
		// 实现"重置"按钮功能
		jb2.addActionListener(new ActionListener() {
 
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				jtf1.setText("");
				jpf1.setText("");
 
			}
		});
		// 将各组件添加到容器中
		c.add(jl1);
		c.add(jtf1);
		c.add(jl2);
		c.add(jpf1);
		c.add(jb1);
		c.add(jb2);
		// 设置各组件的位置以及大小
		jl1.setBounds(10, 20, 90, 30);
		jtf1.setBounds(60, 20, 210, 30);
		jl2.setBounds(25, 60, 90, 30);
		jpf1.setBounds(60, 60, 210, 30);
		jb1.setBounds(80, 100, 70, 50);
		jb2.setBounds(150, 100, 70, 50);
		// 设置窗体大小、关闭方式、不可拉伸
		setSize(300, 220);
		setVisible(true);
		setResizable(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
	}
 
	public static void main(String[] args) {
		new Swingtest002();
	}
 
}

 

你可能感兴趣的:(java学习课程)