class LogPane extends JPanel implements ActionListener
{
JPanel toyPanel;//图片面板
JPanel entryPanel;//标签,数据输入控件,按键变量面板

//标签变量
JLabel logoimagetoy,user,pwd;

//数据输入控件变量
JTextField tuser;
JPasswordField tpwd;

//按键变量
JButton log,reg; PreparedStatement select; JPanel p;
CardLayout c;

LogPane(JPanel bigPanel,CardLayout cl)
{
p=bigPanel;
c=cl; //初始化面板
toyPanel=new JPanel();
entryPanel=new JPanel();
entryPanel.setBackground(Color.white);

setLayout(new BorderLayout());
add(toyPanel,BorderLayout.CENTER);
add(entryPanel,BorderLayout.SOUTH); //设置边框大小
BevelBorder edge=new BevelBorder(BevelBorder.RAISED);
CompoundBorder comp=new CompoundBorder(new BevelBorder(BevelBorder.LOWERED),new LineBorder(Color.black));

Font font=new Font("宋体",12,12);//创建字体 //初始化标签
user=new JLabel("用户名: ");
pwd=new JLabel("密  码: "); //设置字体大小
user.setFont(font);
pwd.setFont(font); //设置字体颜色
user.setForeground(Color.black);
pwd.setForeground(Color.black);

//初始化数据输入控件
tuser=new JTextField(10);
tpwd=new JPasswordField(10); tuser.setBorder(comp);
tpwd.setBorder(comp);

//初始化按钮
log=new JButton("登 录");
reg=new JButton("注 册");
log.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置按键生成手形光标
reg.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置按键生成手形光标
log.setBackground(Color.white);//设置按键背景颜色
reg.setBackground(Color.white);//设置按键背景颜色
log.setFont(font);//设置按键字体大小
reg.setFont(font);//设置按键字体大小
log.setToolTipText("登录");
reg.setToolTipText("注册");
log.setBorder(edge);
reg.setBorder(edge);
log.addActionListener(this);//设置注册按键事件
reg.addActionListener(this);//设置登录按键事件 //初始化图象
Icon logoImage=new ImageIcon("d:\\aa\\Images\\toy.jpg");
logoimagetoy=new JLabel(logoImage);

//添加控件
entryPanel.add(user);
entryPanel.add(tuser);
entryPanel.add(pwd);
entryPanel.add(tpwd);
entryPanel.add(log);
entryPanel.add(reg);
toyPanel.add(logoimagetoy); connectDb();
} public void connectDb() //连接数据库
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:test","sa","");
select=con.prepareStatement("select count(*) from Login_04 where vUserName=? and cPassword=?");
}
catch(Exception e)
{
System.out.println(e);
} } public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reg)
c.show(p,"regPane");
else
{
String strName=tuser.getText();
String strPwd=new String(tpwd.getPassword());
 if(checkName(strName))
if(checkPwd(strPwd))
clicklog(strName);
else
JOptionPane.showMessageDialog(this,"密码不能为空!!");
else
JOptionPane.showMessageDialog(this,"用户名不能为空!!");
}
} public boolean checkName(String strName)
{
boolean temp=true;
if(strName.length()==0)
temp=false;
return temp;
} public boolean checkPwd(String strPwd)
{
boolean temp=true;
if(strPwd.length()==0)
temp=false;
return temp;
} public void clicklog(String strName)
{
try
{
String strPwd=new String(tpwd.getPassword());
select.setString(1,strName);
select.setString(2,strPwd);
ResultSet rs=select.executeQuery();
rs.next();
if(rs.getInt(1)!=1)
JOptionPane.showMessageDialog(this,"登录失败,用户名或密码错误!!");
else
c.show(p,"shoppingPane");
}
catch(Exception e)
{
c.show(p,"shoppingPane");
}

}
}