教育路上
摘要:java注冊界面創建。以下是我們為大家整理的,相信大家閱讀完后肯定有了自己的選擇吧。
2021-06-29 11:53網絡推薦
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(); } }
訪客的評論 2023/11/08 20:51
文中描述的是準確的嗎,如何報名!