首页
java注册界面reg类创建
日期:2021-10-25 12:52  点击:859
java注册界面创建
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class reg extends JFrame  {
	//定义全局变量或成员变量
	private JButton reg=new JButton("注册");
	private JButton login=new JButton("登陆");
	private JButton regsave=new JButton("确定");
	private JLabel truenametext=new JLabel("姓名");
	private JLabel usertext=new JLabel("用户名");
	private JLabel passtext=new JLabel("密码");
	private JLabel rpasstext=new JLabel("再次密码");
	private JTextField truenamefield=new JTextField(30);
	private JTextField userfield=new JTextField(30);
	private JPasswordField passfield=new JPasswordField(30);
	private JPasswordField rpassfield=new JPasswordField(30);
	private JPanel up=new JPanel();
	private JPanel down=new JPanel(){
		protected void paintComponent(Graphics g) { 
	          super.paintComponent(g); 
	          ImageIcon img = new ImageIcon(reg.class.getResource("background.jpg"));  
	          img.paintIcon(this, g, 0, 0); 
	    } 
	};
	private JPanel center=new JPanel();
	private JDialog dialog;
	private JLabel welcome=new JLabel("欢迎您");
	private JButton update=new JButton("修改密码");
	private JButton chart=new JButton("进入聊天室");
	private JFrame f;
	private String usersuc;
	
	reg(){
		reggui();
		reglistion();
	}
	//注册图形界面
	void reggui(){
		up.setLayout(new GridLayout(2,2));
		up.add(usertext);up.add(userfield);
		up.add(passtext);up.add(passfield);
		down.add(reg);down.add(login);
		center.add(welcome);center.add(update);
		center.add(chart);
		
		f=new JFrame("注册窗口");
		f.setSize(400,200);
		f.setLocation(300,200);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());
		f.add(up,BorderLayout.CENTER);
		f.add(down,BorderLayout.SOUTH);
		f.add(center,BorderLayout.NORTH);
		f.setVisible(true);
		center.setVisible(false);
	}
	//注册监听
	void reglistion() {
		//按钮注册弹窗
		reg.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				dialog = new JDialog(reg.this, true );	
				dialog.setTitle("注册用户名");
				dialog.setSize(300, 200);
				dialog.setLocation(50, 50);
				dialog.setLayout(new GridLayout(5,2));
				dialog.add(truenametext);dialog.add(truenamefield);
				dialog.add(usertext);dialog.add(userfield);
				dialog.add(passtext);dialog.add(passfield);
				dialog.add(rpasstext);dialog.add(rpassfield);
				dialog.add(regsave);
				dialog.setVisible(true);
			}
		});	
		//注册提交数据库
		regsave.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
			String u=userfield.getText();
			String p=passfield.getText();
			String rp=passfield.getText();
			String name=truenamefield.getText();
			if(u.length()<6 ||u.length()>10) {
				message("用户名错误");
				return;
			}
			if(!p.equals(rp)) {
				message("两次密码不一样");
				return;
			}
			int resutl=sql(u,p,name,"find");
			if(resutl==1) {
				message("用户名存在");
				return;
			}
			sql(u,p,name,"insert");
		    welcome.setText("欢迎您:"+u);
		    f.setTitle("欢迎您:"+u);
			dialog.dispose();
			up.setVisible(false);
			down.setVisible(false);
			center.setVisible(true);
			usersuc=u;
			message("注册成功");
			}
		});
		
		//登陆
		login.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				String u=userfield.getText();
				String p=passfield.getText();		
				String name=null;
				int result=sql(u,p,name,"login");
				if(result==1) {
					 welcome.setText("欢迎您:"+u);
					 f.setTitle("欢迎您:"+u);
					 up.setVisible(false);
					 down.setVisible(false);
					 center.setVisible(true);
					 usersuc=u;
				}else {
					 message("用户名或密码错误");
				}
			}
		});
		
		//进入聊天室
		chart.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
			new chart(usersuc);
			}
		});
		
	}
	
	
	int sql(String u,String p,String name,String action) {
		Statement stmt = null;  
		Connection conn = null;  
		ResultSet rs = null;
		int result=0;
		try {
			// 1. 注册数据库的驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 2.通过DriverManager获取数据库连接
			String url = "jdbc:mysql://localhost:3306/test";
			String username = "root";
			String password = "root";
			conn = DriverManager.getConnection(url, username, password);
			// 3.通过Connection对象获取Statement对象
			stmt = conn.createStatement();
			// 4.使用Statement执行SQL语句。
			System.out.print(action);
			if(action.equals("insert")) {
				String sql = "INSERT INTO user(user,pass,truename) value('"+u+"','"+p+"','"+name+"')";
				stmt.executeUpdate(sql);
			}else if(action.equals("find")){
				String sql = "select * from user where user='"+u+"' ";
				rs = stmt.executeQuery(sql);
				if(rs.next()) {
					result=1;
				}
			}else if(action.equals("login")){
				String sql = "select * from user where user='"+u+"' and pass='"+p+"' ";
				rs = stmt.executeQuery(sql);
				if(rs.next()) {
					result=1;
				}
			}else if(action.equals("update")){
				String sql = "update user set pass='"+p+"' where user='"+u+"' ";
				stmt.executeUpdate(sql);	
			}
			
		} catch (Exception e) {
				e.printStackTrace();
		}
	return result;
	}
	
	void message(String str){
		JDialog dialog = new JDialog(reg.this, true );
		JOptionPane.showMessageDialog(dialog, str,"dsfad",JOptionPane.ERROR_MESSAGE);
	}

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

}


上一篇· java读取数据库并显示二维数组并用table表格显示    下一篇·java弹窗JDialog对话框
最新文章
热门文章
关于网站  |  联系我们  |  网站地图  | 
2021-10-25 12:52
Powered by edu63.com
首页 刷新 顶部