运行如图
参考代码如下
import java.awt.*import java.awt.event.*
import javax.swing.*
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf
JPasswordField jpf
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel()//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10))//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"))
JLabel jl1 = new JLabel("用户名:")
jtf = new JTextField(10)
JLabel jl2 = new JLabel("密码:")
jpf = new JPasswordField(10)
jpf.setEchoChar('*')//用*号来隐藏密码的显示
jp1.add(jl1)jp1.add(jtf)
jp1.add(jl2)jp1.add(jpf)
jpc.add(jp1)
add(jpc)
JButton jb1 = new JButton("提交")
jb1.addActionListener(this)
jb1.setActionCommand("yes")
JButton jb2 = new JButton("取消")
jb2.addActionListener(this)
jb2.setActionCommand("no")
JPanel jp2 = new JPanel()
jp2.add(jb1)jp2.add(jb2)
add(jp2,BorderLayout.SOUTH)
setTitle("用户注册界面")
setSize(280, 280)
setLocationRelativeTo(null)//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE)//
setVisible(true)
}
public static void main(String[] args) {
new RegDemo()
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim()
String pwd = new String(jpf.getPassword())
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码")
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd)
}
}else{
jtf.setText("")
jpf.setText("")
}
}
}
制作一个类似于QQ登陆的界面,以下数据库部分,你可以自己修该下,默认为Access;
import java.awt.*
import javax.swing.*
import java.awt.event.*
import java.sql.*
class JieMian extends JFrame;
{
JFrame f
JLabel lb1
JLabel lb2
JTextField tf
JPasswordField pwd
JButton b1
JButton b2
JPanel p1
JPanel p2
Connection con
Statement stmt
String use
char passw[]=new char[10]
ResultSet rs
String k1,k2
public void init()
f=new JFrame("登陆界面")
init()
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ei)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
con=DriverManager.getConnection("jdbc:odbc:dat")
stmt=con.createStatement()
use=tf.getText()
passw=pwd.getPassword()
String passww=new String(passw)
rs=stmt.executeQuery("select * from user where 用户名='"+use+"' and 密码='"+passww+"'")
while(rs.next())
{
System.out.println(k1=rs.getString("用户名"))
System.out.println(k2=rs.getString("密码"))
}
if(k1.equals(use)&&(k2.equals(passww)))
{
JOptionPane.showMessageDialog(null,"用户登陆成功")//登陆成功
}
else
{
JOptionPane.showMessageDialog(null,"请检查用户信息,登陆失败")//登录失败
}
rs.close()
stmt.close()
con.close()
}
catch (Exception ey){JOptionPane.showMessageDialog(null,"请检查用户信息,登陆失败")}
}
})
f.setLayout(new GridLayout(2,1,10,5))
f.setVisible(true)
f.setSize(210,150)
f.setLocation(550,300)
f.setResizable(false)
f.setDefaultCloseOperation(EXIT_ON_CLOSE)
b2.addActionListener(new ActionListener(){//58
public void actionPerformed(ActionEvent e){
f.setVisible(false)
}
})
}
}
public class DengLu
{
public static void main(String[] args)
{
new JieMian()
}
}。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)