教育路上

全國站>Java>Java基礎>java注冊界面reg類創建
學員需求

java注冊界面reg類創建

摘要:java注冊界面創建。以下是我們為大家整理的,相信大家閱讀完后肯定有了自己的選擇吧。

2021-06-29 11:53網絡推薦

發布時間:
2021-06-29 11:53
信息來源:
網絡推薦
瀏覽次數:
2205
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對話框
標簽:
網友評論
發布評論

訪客的評論 2023/05/29 01:15

文中描述的是準確的嗎,如何報名!

相關推薦
我也來發表評價關閉
我對該內容的評價:
0
評價500
驗證碼: 看不清 換一張
提交 (匿名發布,無須擔心別人知道您的身份)
學校免費發布信息關閉
我們審核后會盡快展示,如有圖片請發郵件到:edu63@foxmail.com

姓      名:

內      容:

手機號碼:

驗  證  碼:  換一張

確認提交
填寫需求信息關閉
我們會根據您的需求匹配并審核留言

姓      名:

意向城市:

留      言:

手機號碼:

驗  證  碼:  換一張

確認提交
糾錯補充本文信息關閉
非常感謝您幫助糾錯補充本文信息


 換一張

確認提交
美女裸体黄网站18禁免费看