📄 loginface.java
字号:
package qq;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.net.InetAddress;
public class LoginFace extends JFrame {
JLabel ID = new JLabel();
JLabel PWD = new JLabel();
JTextField userId = new JTextField();
JPasswordField passWord = new JPasswordField();
JButton login = new JButton();
JButton register = new JButton();
XYLayout xYLayout = new XYLayout();
public LoginFace() {
super("QQ用户登录");
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout);
ID.setFont(new java.awt.Font("黑体", Font.PLAIN, 20));
ID.setText("帐号");
userId.setToolTipText("请输入QQ帐号");
passWord.setToolTipText("请输入QQ密码");
PWD.setFont(new java.awt.Font("黑体", Font.PLAIN, 20));
PWD.setText("密码");
register.setText("申请帐号");
register.addActionListener(new LoginFace_register_actionAdapter(this));
login.setSelectedIcon(null);
login.setText("用户登陆");
login.addActionListener(new LoginFace_login_actionAdapter(this));
this.getContentPane().setBackground(Color.lightGray);
this.setCursor(null);
this.setVisible(true);
xYLayout.setWidth(310);
xYLayout.setHeight(217);
this.setForeground(Color.black);
this.setResizable(false);
this.addWindowListener(new LoginFace_this_windowAdapter(this));
this.getContentPane().add(ID, new XYConstraints(40, 43, 44, 32));
this.getContentPane().add(userId, new XYConstraints(103, 43, 153, 32));
this.getContentPane().add(PWD, new XYConstraints(40, 93, 44, 32));
this.getContentPane().add(passWord, new XYConstraints(103, 91, 153, 32));
this.getContentPane().add(register, new XYConstraints(163, 147, -1, 30));
this.getContentPane().add(login, new XYConstraints(56, 147, -1, 30));
this.setLocation(450, 250);
this.pack();
this.setVisible(true);
}
//main方法
public static void main(String[] args) {
LoginFace lf = new LoginFace();
}
//关闭程序
public void this_windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
//用户登陆(用户登陆按钮事件)
public void login_actionPerformed(ActionEvent actionEvent) {
try {
Client client = new Client();//实例化客户端公用类
client.userInfo.setInfoType(1);//设置类型为登陆请求
client.userInfo.setUserId(Integer.valueOf(userId.getText()));//获得用户输入的帐号
client.userInfo.setPassWord(String.valueOf(passWord.getPassword()));//获得用户输入的密码
String[] ip = String.valueOf(InetAddress.getLocalHost()).split("/", 2);//获得本机IP
client.userInfo.setUserIp(ip[1]);//设置本机IP
client.userInfo.setUserPort(client.userPort+client.userInfo.getUserId());//设置用户端口
client.userInfo.setOnLine(false);//设置在线状态为false,用以判断登陆是否成功
client.send(client.userInfo);//向服务器端发送登陆请求
client.userInfo = (UserInfo)client.get(client.userInfo.getUserPort());//获得服务器端返回的登陆信息
if (!client.userInfo.getOnLine()) {//在线状态为false说明登陆失败
JOptionPane.showMessageDialog(null, "帐号或密码错误,请正确输入!");
} else {//否则说明登陆成功
Client.localUser.put(client.userInfo.getUserId(),client.userInfo);//将用户信息添加进本地用户组里面
this.dispose();//释放登陆窗口资源
MainFace mf = new MainFace();//实例化主面板
mf.mainFace(client.userInfo.getUserId());//向主面板传输该用户ID
Thread thread = new Thread(mf);//实例化线程
thread.start();//启动线程
}
} catch (Exception em) {
em.printStackTrace();
}
}
//申请帐号(申请帐号按钮事件)
public void register_actionPerformed(ActionEvent actionEvent) {
RegisterFace register = new RegisterFace();
}
}
class LoginFace_this_windowAdapter extends WindowAdapter {
private LoginFace adaptee;
LoginFace_this_windowAdapter(LoginFace adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent windowEvent) {
adaptee.this_windowClosing(windowEvent);
}
}
class LoginFace_login_actionAdapter implements ActionListener {
private LoginFace adaptee;
LoginFace_login_actionAdapter(LoginFace adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.login_actionPerformed(actionEvent);
}
}
class LoginFace_register_actionAdapter implements ActionListener {
private LoginFace adaptee;
LoginFace_register_actionAdapter(LoginFace adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.register_actionPerformed(actionEvent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -