教育路上

全国站>Java>Java基础>java注册界面reg类创建
学员需求

java注册界面reg类创建

摘要:java注册界面创建。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

2021-06-29 11:53网络推荐

发布时间:
2021-06-29 11:53
信息来源:
网络推荐
浏览次数:
2689
java注册界面reg类创建

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对话框
标签:
网友评论
发布评论

访客的评论 2024/03/28 18:15

文中描述的是准确的吗,如何报名!

相关推荐
我也来发表评价关闭
我对该内容的评价:
0
评价500
验证码: 看不清 换一张
提交 (匿名发布,无须担心别人知道您的身份)
学校免费发布信息关闭
我们审核后会尽快展示,如有图片请发邮件到:edu63@foxmail.com

姓      名:

内      容:

手机号码:

验  证  码:  换一张

确认提交
填写需求信息关闭
我们会根据您的需求匹配并审核留言

姓      名:

意向城市:

留      言:

手机号码:

验  证  码:  换一张

确认提交
完善补充本文信息关闭
非常感谢您帮助完善补充本文信息


 换一张

确认提交