Java聊天室程序源码 Java即时通讯代码 Java局域网聊天系统 Java即时通讯 Java聊天系统
public Swingtest002() {
// 设置标题
setTitle("请登陆");
// 绝对布局
setLayout(null);
// 定义一个容器
Container c = getContentPane();
// 创建"用户名:"标签
JLabel jl1 = new JLabel("用户名:");
// 创建文本框
final JTextField jtf1 = new JTextField();
// 创建"密码:"标签
JLabel jl2 = new JLabel("密码:");
// 创建密码框
final JPasswordField jpf1 = new JPasswordField();
// 设置密码字符为*
jpf1.setEchoChar('*');
// 创建"提交"按钮
JButton jb1 = new JButton("提交");
// 创建"重置"按钮
JButton jb2 = new JButton("重置");
// 当用户名为"mr",密码为"mrsoft"时点击"提交"按钮弹出"登录成功"提示对话框
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name=(String)jtf1.getText().trim();
String password=new String(jpf1.getText().trim());
String sql="select * from user where name='"+name+"' and pass='"+password+"'";
System.out.println("name"+name+"password"+password);
// TODO Auto-generated method stub
try {
ResultSet rs=st.executeQuery(sql);
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陆成功!");
this.setVisible(false);//关闭当前的窗体
MainFrame mainframe=new MainFrame();
} else if (jtf1.getText().trim().length() == 0 || new String(jpf1.getPassword()).trim().length() == 0) {
JOptionPane.showMessageDialog(null, "不能为空!");
} else {
JOptionPane.showMessageDialog(null, "错误");
// 清零
jtf1.setText("");
jpf1.setText("");
}
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void setVisible(boolean b) {
// TODO Auto-generated method stub
}
});