📄 logingui.java
字号:
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.io.*;
import java.awt.event.*;
public class LoginGUI extends JFrame implements ActionListener
{
boolean bo;
JLabel lblAddress ;
JLabel lblUser;
JLabel lblPwd;
JTextField txtUser;
JPasswordField txtPwd;
JTextField txtAddress;
JButton bCon;
JButton bRes;
JButton bRecall;
JButton bLogin;
JButton bLogout;
JLabel llblTop ;
Socket soc;
UserInfo user;
ClientItf client;
//ButtonListener buttonlistener;
static Connection con;
static ResultSet pid;
static PreparedStatement stat;
public LoginGUI()
{
super("小不点聊天室 用户登录");
//图片加入区
Icon B=new ImageIcon("spring.gif");
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("6516959_1.jpg")));
user = new UserInfo();
client = new Client();
Container c = this.getContentPane();
JPanel p = new JPanel();
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
p.setLayout(gb);
c.add(p);
llblTop = new JLabel(B);
lblAddress = new JLabel(" IP 地 址:");
lblUser = new JLabel("用 户 名: ");
lblPwd = new JLabel("密 码 ");
txtUser = new JTextField("<请在这儿输入用户名>",10);
txtPwd = new JPasswordField(10);
txtPwd.setToolTipText ("请在这儿重新输入密码 ");
txtAddress = new JTextField("请在这儿输入IP地址",10);
bCon = new JButton("连 接 ");
bRes = new JButton("注 册 ");
bRecall = new JButton(" 忘记密码 ");
bLogin = new JButton(" 登 录 ");
bLogout = new JButton(" 退 出 ");
// bLogout=new JButton(new ImageIcon("Post.jpg"));
//ButtonListener buttonlistener=new ButtonListener(); //**************加 事 件
bRes.addActionListener(this);
bCon.addActionListener(this);
bRecall.addActionListener(this);
bLogin.addActionListener(this);
bLogout.addActionListener(this);
p.setBorder(BorderFactory.createLineBorder(Color.PINK,5));
p.setBorder(BorderFactory.createLineBorder(Color.BLUE,2));
p.setBackground(Color.pink);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gb.setConstraints(llblTop,gbc);
p.add(llblTop,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = new Insets(0,0,5,5);
gb.setConstraints(lblAddress,gbc);
p.add(lblAddress);
gbc.gridx = 1;
gb.setConstraints(txtAddress,gbc);
p.add(txtAddress);
gbc.gridx = 2;
gb.setConstraints(bCon,gbc);
p.add(bCon);
gbc.gridx = 0;
gbc.gridy = 4;
gb.setConstraints(lblUser,gbc);
p.add(lblUser);
gbc.gridx = 1;
gb.setConstraints(txtUser,gbc);
p.add(txtUser);
gbc.gridx = 2;
gb.setConstraints(bRes,gbc);
p.add(bRes);
gbc.gridx = 0;
gbc.gridy = 5;
gb.setConstraints(lblPwd,gbc);
p.add(lblPwd);
gbc.gridx = 1;
gb.setConstraints(txtPwd,gbc);
p.add(txtPwd);
gbc.gridx = 2;
gb.setConstraints(bRecall,gbc);
p.add(bRecall);
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 1;
gb.setConstraints(bLogin,gbc);
p.add(bLogin);
gbc.gridx = 2;
gbc.gridwidth = 7;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.CENTER;
gb.setConstraints(bLogout,gbc);
p.add(bLogout);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton obj= (JButton)e.getSource();
if(obj.equals(bLogin)) //***************************************点击登录
{
//组包、将输入的用户名、密码封装到一个对象中
user.setUserName(txtUser.getText().trim());
user.setUserPwd(txtPwd.getText().trim());
try {
bo = client.login(user);
}
catch(Exception e1){e1.printStackTrace();}
try {
if(bo==false)
{
System.out.println("该用户是无效用户!");
JOptionPane.showMessageDialog(this,"小布点提醒您-该用户是无效用户!");
}
else
{
System.out.println("登录成功!Thank you");
JOptionPane.showMessageDialog(this,"小布点提醒您-登录成功!感谢您的支持!祝您愉快!Thank ");
new chatGUI(client,txtUser.getText().trim());
this.dispose();
}
System.out.println(bo);
}
catch (Exception e1){e1.printStackTrace();}
}
//
if(obj.equals(bRecall))//*******************************点击忘记密码
{
new UpadateInfoGUI(client);
}
if(obj.equals(bRes))//***********************************点击注册
{
new RegistrgGUI(client);
}
if(obj.equals(bLogout))//***********************************点击退出
{
{
JOptionPane.showMessageDialog(bLogin,"确定要退出吗?");
System.exit(0);
}
}
if(obj.equals(bCon))//*********************连接客服器 验证 IP 是否有误
{
soc = client.connection(txtAddress.getText().trim());
if(soc==null)
JOptionPane.showMessageDialog(null,"连接失败");
else
JOptionPane.showMessageDialog(null,"连接成功");
}
}
public static void main(String[] args)
{
new LoginGUI();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -