📄 loginfrm.java
字号:
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
public class LoginFrm {
private JFrame f;
private JButton set,login,request,setCfm;
private JTextField numF;
private JPasswordField pasF;
private ConfrgDlg dlg;
private String ip,port;
private LoginAdapter loginApt;
public LoginFrm(){
buildGUI();
hookEvent();
init();
}
private void buildGUI(){
f = new JFrame("QQ用户登陆");
f.setSize(330,330);
f.setResizable(false);
f.setIconImage(
new ImageIcon("Icon/titleIcon.gif").getImage());
Container c = f.getContentPane();
c.setBackground(Color.CYAN);
/**
* South panel
*/
JPanel pNth = new JPanel();
pNth.setLayout(new GridLayout(3,1,0,0));
JPanel numP = new JPanel();
JPanel pasP = new JPanel();
JPanel bar = new JPanel();
GridBagLayout gb = new GridBagLayout();
numP.setLayout(gb);
pasP.setLayout(gb);
GridBagConstraints gbc = new GridBagConstraints();
JLabel numLab = new JLabel("QQ号码",SwingConstants.CENTER);
JLabel pasLab = new JLabel("QQ密码",SwingConstants.CENTER);
numF = new JTextField(20);
pasF = new JPasswordField(20);
MyUtilities.addGCmp(numP,numLab,gb,gbc,0,0,1,1,1,1);
MyUtilities.addGCmp(numP,numF,gb,gbc,1,0,1,1,1,1);
MyUtilities.addGCmp(pasP,pasLab,gb,gbc,0,0,1,1,1,1);
MyUtilities.addGCmp(pasP,pasF,gb,gbc,1,0,1,1,1,1);
pNth.add(numP);
pNth.add(bar);
pNth.add(pasP);
c.add(pNth,BorderLayout.NORTH);
/**
* Center panel
*/
ImageIcon bgImg = new ImageIcon("Icon/load.gif");
JLabel bgLab = new JLabel(bgImg);
c.add(bgLab,BorderLayout.CENTER);
/**
* south panel
*/
JPanel pSouth = new JPanel();
pSouth.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
this.set = new JButton("设置");
this.login = new JButton("登录");
this.request = new JButton("申请号码");
/**
* 设置对话框里面的按钮
*/
this.setCfm = new JButton("确定");
pSouth.add(set);
pSouth.add(request);
pSouth.add(login);
c.add(pSouth,BorderLayout.SOUTH);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
/*居中*/
f.setLocation( (size.width-f.getWidth())/2,
(size.height-f.getHeight())/2);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
private void hookEvent(){
this.login.addActionListener(new Adapter());
this.set.addActionListener(new Adapter());
this.request.addActionListener(new Adapter());
this.setCfm.addActionListener(new Adapter());
this.f.addComponentListener(new ComponentAdapter(){
public void componentMoved(ComponentEvent e){
if(dlg !=null){
resetDlgLocation();
}
}
});
}
private void init(){
this.ip = "172.17.74.118";
this.port= "9999";
}
private void login(){
/**
* check null
*/
String number = this.numF.getText();
String password = String.valueOf(this.pasF.getPassword());
if(number.equals("")||
password.equals("")){
JOptionPane.showMessageDialog(null,"输入栏不能为空!",
"输入有误",JOptionPane.ERROR_MESSAGE);
return;
}else{
try{
this.loginApt = new LoginAdapter();
}catch (Exception e) {
JOptionPane.showMessageDialog(
null,"对不起,聊天服务器暂时没有开启","连接错误",
JOptionPane.ERROR_MESSAGE);
return;
}
try{
/**
* 验证客户的登录权限
*/
String feedBack = this.loginApt.checkInfo(number, password);
/*登录失败 */
if(feedBack.equals("Invalid")){
JOptionPane.showMessageDialog(
null,"用户名密码有误或者重复登录","登录失败",
JOptionPane.ERROR_MESSAGE);
}else if(feedBack.equals("valid")){
/**
* 注销登录框,显示在线列表
*/
this.f.dispose();
new OnLineFrm(this.loginApt.input,number,ip,port);
}
}catch(Exception e)
{}
}
}
private void request() throws Exception{
new RequestFrm(this.ip,this.port);
}
private void setConfirm(){
//保存dlg中的有用信息
this.ip = dlg.ipF.getText();
this.port = dlg.portF.getText();
dlg.dispose();
}
// 处理设置框的显示
private void showDlg(){
dlg = new ConfrgDlg();
resetDlgLocation();
}
//归位
private void resetDlgLocation(){
int x = f.getLocation().x;
int y = f.getLocation().y + f.getHeight();
dlg.setLocation(x, y);
}
class LoginAdapter{
private Socket connection;
private ObjectInputStream input;
private ObjectOutputStream output;
public LoginAdapter()throws Exception{
this.connectS();
output = new ObjectOutputStream(
this.connection.getOutputStream());
input = new ObjectInputStream(
this.connection.getInputStream());
}
/**
* 从服务器读出验证消息
*/
public String checkInfo(String num,String pas) throws Exception{
String msg = null;
String loginMsg = this.connection.getLocalAddress().getHostAddress()+
" "+num+" "+pas;
output.writeObject(new Message("登录",loginMsg));
try{
msg = ((Message)this.input.readObject()).sysMsg;
}catch(Exception e)
{}
return msg;
}
/**
* 传给OnlineFrm
*/
public Socket getConnection(){return this.connection;}
private void connectS()throws IOException
{
this.connection = new Socket(
ip,Integer.parseInt(port));
}
}
class Adapter implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("设置")){
showDlg();
}
else if(e.getActionCommand().equals("登录")){
login();
}
else if(e.getActionCommand().equals("确定")){
setConfirm();
}
else if(e.getActionCommand().equals("申请号码")){
try{
request();
}catch(Exception c){
JOptionPane.showMessageDialog(null,"申请号码失败");
}
}
}
}
class ConfrgDlg extends JDialog{
private JTextField ipF,portF;
public ConfrgDlg(){
this.setResizable(false);
this.setUndecorated(true);
JLabel ipLab = new JLabel("请输入服务器IP: ");
JLabel portLab = new JLabel("请输入服务器port: ");
ipF = new JTextField(20);
portF = new JTextField(20);
ipF.setText("172.17.74.118");
portF.setText("9999");
/**
* buttonPane
*/
JPanel buttonP = new JPanel();
buttonP.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonP.add(setCfm);
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(gb);
MyUtilities.addGCmp(this,ipLab,gb,gbc,0,0,1,1,1,1);
MyUtilities.addGCmp(this,ipF,gb,gbc,1,0,1,1,1,1);
MyUtilities.addGCmp(this,new JPanel(),gb,gbc,0,1,2,1,1,1);
MyUtilities.addGCmp(this,portLab,gb,gbc,0,2,1,1,1,1);
MyUtilities.addGCmp(this,portF,gb,gbc,1,2,1,1,1,1);
MyUtilities.addGCmp(this,buttonP,gb,gbc,0,3,2,1,1,1);
this.pack();
this.setVisible(true);
}
}
public static void main(String[] args){
LoginFrm f = new LoginFrm();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -