这次写的注册页面并把以前的代码改了改
以前的更改后:
package homework;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class 界面 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public 界面()
{
setTitle("登录");
setBak(); //调用背景方法
JPanel jp = new JPanel(); //创建个JPanel
jp.setOpaque(false); //把JPanel设置为透明 这样就不会遮住后面的背景 这样你就能在JPanel随意加组件了
this.add(jp);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int)screensize.getWidth();
int y = (int)screensize.getHeight();
//获取屏幕的长宽并将界面设置成该大小
this.setSize(x,y);
jp.setLayout(new BorderLayout());
JLabel jj=new JLabel("欢迎进入用户管理系统!");
jj.setFont(new Font("宋体",Font.PLAIN,45));//设置字体的字体,样子,大小
jp.add(jj,BorderLayout.NORTH); //在整体布局的上方
jj.setHorizontalAlignment(SwingConstants.CENTER);//居中
jj.setPreferredSize(new Dimension(0,500)); //宽度300
JLabel j1_useranme=new JLabel("请输入用户名");
j1_useranme.setFont(new Font("宋体",Font.PLAIN,20));
JTextField ja_username=new JTextField(20);
JLabel j2_useranme=new JLabel("请输入密码 ");
j2_useranme.setFont(new Font("宋体",Font.PLAIN,20));
JPasswordField jb_password=new JPasswordField(20);//密码采用密码输入:即输入后显示**
JPanel p=new JPanel();
jp.add(p,BorderLayout.CENTER);
p.setLayout(new BorderLayout());//在边框布局中的center部分嵌入边框布局
JPanel p1=new JPanel();
JPanel p3=new JPanel();
JLabel b=new JLabel(" ");
p1.add(j1_useranme);
p1.add(ja_username);
p3.add(j2_useranme);
p3.add(jb_password);
p.add(b,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p.add(p3,BorderLayout.SOUTH);
b.setPreferredSize(new Dimension(0,40));
JButton j3_ok=new JButton("登录");
j3_ok.addActionListener(new ActionListener() //监听登录按钮
{
public void actionPerformed(ActionEvent e)
{
String jusername=ja_username.getText();
char s[]=jb_password.getPassword();
String jpassword=new String(s);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
//配置连接字符串
String user="sa";//sa超级管理员
String password="手动打码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="(Select * from dbo.PY where ID='"+jusername+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
if(rs.getString("PAWD")==jpassword)//如果密码正确,进入下一个界面
{
new PY();
closeThis();
}
else
{
JOptionPane.showMessageDialog(null,"对不起,输入密码错误.","错误!",JOptionPane.ERROR_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(null,"用户名不存在","错误!",JOptionPane.ERROR_MESSAGE);
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
}
);
JButton j4_register=new JButton("注册");
j4_register.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
closeThis();
new ZC();
}
});
JPanel p2=new JPanel();
p2.add(j3_ok);
p2.add(j4_register);
p2.setPreferredSize(new Dimension(0,100));
jp.add(p2,BorderLayout.SOUTH);//在整体的下方,两个按钮并排
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setBak()//背景方法
{
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\2.jpg"); //添加图片的位置
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
public void closeThis()//关闭当前界面
{
this.dispose();
}
public static void main(String[] args)
{
new 界面().setVisible(true);
}
}
注册界面:
package homework;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ZC extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public ZC()
{
super("注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setLocation(400,150);
setVisible(true);
JLabel jj=new JLabel("朴实无华的注册界面");
jj.setFont(new Font("宋体",Font.PLAIN,35));//设置字体的字体,样子,大小
this.add(jj,BorderLayout.NORTH); //在整体布局的上方
jj.setHorizontalAlignment(SwingConstants.CENTER);//居中
//jj.setPreferredSize(new Dimension(0,500)); //宽度300
JLabel j1_useranme=new JLabel("用户名:");
j1_useranme.setFont(new Font("宋体",Font.PLAIN,20));
JTextField ja_username=new JTextField(20);
JLabel j2_useranme=new JLabel("密码: ");
j2_useranme.setFont(new Font("宋体",Font.PLAIN,20));
JTextField jb_username=new JTextField(20);
JPanel p=new JPanel();
this.add(p,BorderLayout.CENTER);
p.setLayout(new BorderLayout());//在边框布局中的center部分嵌入边框布局
JPanel p1=new JPanel();
JPanel p3=new JPanel();
JLabel b=new JLabel(" ");
p1.add(j1_useranme);
p1.add(ja_username);
p3.add(j2_useranme);
p3.add(jb_username);
p.add(b,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p.add(p3,BorderLayout.SOUTH);
p3.setPreferredSize(new Dimension(0,200));
b.setPreferredSize(new Dimension(0,90));
JPanel p2=new JPanel();
JButton register=new JButton("注册");
p2.add(register);
this.add(p2,BorderLayout.SOUTH);//在整体的下方
p2.setPreferredSize(new Dimension(0,70));
register.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String jusername=ja_username.getText();
String jusername1=jb_username.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//加载对应的jdbc驱动
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
//配置连接字符串
String user="sa";//sa超级管理员
String password="打码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
//创建数据库连接对象
Statement st=conn.createStatement();
//创建SQL语句执行对象
String strSQL="(Select * from dbo.PY where ID='"+jusername+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"用户名已存在","错误!", JOptionPane.ERROR_MESSAGE);
}
else
{
String sql = "insert into dbo.PY(ID,PAWD) values(' "+jusername+"',' "+jusername1+"') ";
PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pst.executeUpdate();
pst.close();
JOptionPane.showMessageDialog(null,"注册成功");
}
conn.close();
//关闭数据库连接
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
}
}