如何用Java编写用户注册界面?

如何用Java编写用户注册界面?,第1张

界面建议用可视化来做,美观且便捷。下面这个是完全用代码写的,仅供参考。\x0d\x0aimport javax.swing.*\x0d\x0aimport java.awt.event.*\x0d\x0aimport java.awt.*\x0d\x0aimport java.sql.*\x0d\x0apublic class Register extends JFrame {\x0d\x0a JLabel jl1 = new JLabel("用户名")\x0d\x0a JTextField jt1 = new JTextField()\x0d\x0a JLabel jl2 = new JLabel("邮箱")\x0d\x0a JTextField jt2 = new JTextField()\x0d\x0a JLabel jl3 = new JLabel("密码")\x0d\x0a JPasswordField jpw1 = new JPasswordField()\x0d\x0a JLabel jl4 = new JLabel("密码确认")\x0d\x0a JPasswordField jpw2 = new JPasswordField()\x0d\x0a JButton register = new JButton("注册")\x0d\x0a JButton clean = new JButton("清空")\x0d\x0a public Register(){\x0d\x0a setLayout(new GridLayout(5,2))\x0d\x0a add(jl1)\x0d\x0a add(jt1)\x0d\x0a add(jl2)\x0d\x0a add(jt2)\x0d\x0a add(jl3)\x0d\x0a add(jpw1)\x0d\x0a add(jl4)\x0d\x0a add(jpw2)\x0d\x0a add(register)\x0d\x0a add(clean)\x0d\x0a String name = jt1.getText()\x0d\x0a String email = jt2.getText()\x0d\x0a String pw = jpw1.getText()\x0d\x0a register.addActionListener(new ActionListener(){\x0d\x0a public void actionPerformed(ActionEvent e){\x0d\x0atry{\x0d\x0a Class.forName("com.mysql.jdbc.Driver")\x0d\x0a Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","")\x0d\x0a Statement sta = con.createStatement()\x0d\x0a sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)")\x0d\x0a JOptionPane.showMessageDialog(null,"注册成功","提示",JOptionPane.INFORMATION_MESSAGE)\x0d\x0a}\x0d\x0acatch(Exception ex){\x0d\x0a ex.getStackTrace()\x0d\x0a}\x0d\x0a }\x0d\x0a })\x0d\x0a clean.addActionListener(new ActionListener(){\x0d\x0a public void actionPerformed(ActionEvent e){\x0d\x0ajt1.setText("")\x0d\x0ajt2.setText("")\x0d\x0ajpw1.setText("")\x0d\x0ajpw2.setText("")\x0d\x0a }\x0d\x0a })\x0d\x0a }\x0d\x0a public static void main(String[] args){\x0d\x0a Register frame = new Register()\x0d\x0a frame.setTitle("用户注册")\x0d\x0a frame.setLocationRelativeTo(null)\x0d\x0a frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)\x0d\x0a frame.setSize(400,400)\x0d\x0a frame.setVisible(true)\x0d\x0a }\x0d\x0a}

//这个是我写的,里面有连接数据库的部分。你可以拿去参考一下

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.sql.*

class LoginFrm extends JFrame implements ActionListener// throws Exception

{

JLabel lbl1 = new JLabel("用户名:")

JLabel lbl2 = new JLabel("密码:")

JTextField txt = new JTextField(5)

JPasswordField pf = new JPasswordField()

JButton btn1 = new JButton("确定")

JButton btn2 = new JButton("取消")

public LoginFrm() {

this.setTitle("登陆")

JPanel jp = (JPanel) this.getContentPane()

jp.setLayout(new GridLayout(3, 2, 5, 5))

jp.add(lbl1)

jp.add(txt)

jp.add(lbl2)

jp.add(pf)

jp.add(btn1)

jp.add(btn2)

btn1.addActionListener(this)

btn2.addActionListener(this)

}

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == btn1) {

try {

Class.forName("com.mysql.jdbc.Driver")// mysql数据库

Connection con = DriverManager.getConnection(

"jdbc:mysql://localhost/Car_zl", "root", "1")// 数据库名为Car_zl,密码为1

System.out.println("com : "+ con)

Statement cmd = con.createStatement()

String sql = "select * from user where User_ID='"

+ txt.getText() + "' and User_ps='"

+ pf.getText() + "'"

ResultSet rs = cmd

.executeQuery(sql)// 表名为user,user_ID和User_ps是存放用户名和密码的字段名

if (rs.next()) {

JOptionPane.showMessageDialog(null, "登陆成功!")

} else

JOptionPane.showMessageDialog(null, "用户名或密码错误!")

} catch (Exception ex) {

}

if (ae.getSource() == btn2) {

System.out.println("1111111111111")

//txt.setText("")

//pf.setText("")

System.exit(0)

}

}

}

public static void main(String arg[]) {

JFrame.setDefaultLookAndFeelDecorated(true)

LoginFrm frm = new LoginFrm()

frm.setSize(400, 200)

frm.setVisible(true)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8091448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存