用JAVA打造自己的QQ(六)--源码篇客户端

      客户端共包含10个文件,对应9个窗体和一个自定义的FriendsInfo类,用于保存从服务端获得的一些好友和陌生人信息.客户端的编写过程即是我学习JAVA SWING的使用过程,所以代码都是些基本的对SWING组件的设置更新,下面便是我的实现:

登录界面实现FMLOGIN.JAVA

package client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.util.Vector;

public class fmLogin extends JFrame {
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel1 = new JPanel();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel1 = new JLabel();
  XYLayout xYLayout2 = new XYLayout();
  JTextField jTextField1 = new JTextField();
  JPasswordField jPasswordField1 = new JPasswordField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JTextField servername = new JTextField();
    //*****************
  String server;//服务器名
  static int serport=10000;//端口
  private Socket socket;
  private BufferedReader in;//输入输出流
  private PrintWriter out;
  JLabel jLabel5 = new JLabel();

  public fmLogin() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setBackground(SystemColor.desktop);
    this.setForeground(Color.white);
    this.setResizable(false);
    this.setState(Frame.NORMAL);
    this.setTitle("JICQ登录");
    xYLayout1.setWidth(267);
    xYLayout1.setHeight(189);
    this.getContentPane().setLayout(xYLayout1);
    jLabel2.setFont(new java.awt.Font("隶书", 0, 15));
    jLabel2.setForeground(SystemColor.desktop);
    jLabel2.setBorder(null);
    jLabel2.setOpaque(false);
    jLabel2.setRequestFocusEnabled(true);
    jLabel2.setToolTipText("");
    jLabel2.setText("密    码:");
    jLabel1.setFont(new java.awt.Font("隶书", 0, 15));
    jLabel1.setForeground(SystemColor.desktop);
    jLabel1.setBorder(null);
    jLabel1.setText("JICQ  NO:");
    jPanel1.setLayout(xYLayout2);
    jPanel1.setBackground(SystemColor.inactiveCaptionText);
    jPanel1.setForeground(Color.black);
    jPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
    jTextField1.setBackground(SystemColor.text);
    jTextField1.setForeground(UIManager.getColor("textText"));
    jTextField1.setText("");
    jPasswordField1.setBackground(SystemColor.text);
    jPasswordField1.setForeground(UIManager.getColor("textText"));
    jPasswordField1.setText("");
    jButton1.setBackground(SystemColor.activeCaptionText);
    jButton1.setFont(new java.awt.Font("隶书", 0, 14));
    jButton1.setForeground(SystemColor.desktop);
    jButton1.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton1.setBorderPainted(true);
    jButton1.setMargin(new Insets(2, 14, 2, 14));
    jButton1.setText("登  录");
    jButton1.addActionListener(new fmLogin_jButton1_actionAdapter(this));
    jButton2.setBackground(SystemColor.activeCaptionText);
    jButton2.setFont(new java.awt.Font("隶书", 0, 14));
    jButton2.setForeground(SystemColor.desktop);
    jButton2.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton2.setBorderPainted(true);
    jButton2.setMargin(new Insets(2, 14, 2, 14));
    jButton2.setText("注  册");
    jButton2.addActionListener(new fmLogin_jButton2_actionAdapter(this));
    jButton3.setBackground(SystemColor.activeCaptionText);
    jButton3.setFont(new java.awt.Font("隶书", 0, 14));
    jButton3.setForeground(SystemColor.desktop);
    jButton3.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton3.setBorderPainted(true);
    jButton3.setMargin(new Insets(2, 14, 2, 14));
    jButton3.setText("取  消");
    jButton3.addActionListener(new fmLogin_jButton3_actionAdapter(this));
    jLabel3.setBackground(Color.white);
    jLabel3.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 18));
    jLabel3.setForeground(SystemColor.inactiveCaptionText);
    jLabel3.setBorder(null);
    jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel3.setText("JICQ 2005Beta");
    jLabel4.setText("服务器/IP:");
    jLabel4.setToolTipText("");
    jLabel4.setRequestFocusEnabled(true);
    jLabel4.setOpaque(false);
    jLabel4.setBorder(null);
    jLabel4.setForeground(SystemColor.desktop);
    jLabel4.setFont(new java.awt.Font("隶书", 0, 15));
    servername.setText("localhost");
    servername.setBackground(SystemColor.text);
    servername.setForeground(UIManager.getColor("textText"));
    jLabel5.setForeground(SystemColor.inactiveCaptionText);
    jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
    jLabel5.setText("[CopyRight] jeven2005");
    jPanel1.add(jTextField1, new XYConstraints(98, 8, 138, 19));
    jPanel1.add(jLabel1, new XYConstraints(16, 8, 85, -1));
    jPanel1.add(jLabel2, new XYConstraints(20, 36, 77, -1));
    jPanel1.add(jPasswordField1, new XYConstraints(97, 37, 138, 19));
    jPanel1.add(jLabel4, new XYConstraints(13, 65, 92, -1));
    jPanel1.add(servername, new XYConstraints(97, 65, 138, 19));
    jPanel1.add(jButton1,      new XYConstraints(8, 93, 78, 22));
    jPanel1.add(jButton3,      new XYConstraints(163, 93, 81, 22));
    jPanel1.add(jButton2,   new XYConstraints(85, 93, 79, 22));
    this.getContentPane().add(jLabel5,            new XYConstraints(106, 167, 150, 16));
    this.getContentPane().add(jLabel3,               new XYConstraints(3, 2, 262, 26));
    this.getContentPane().add(jPanel1,                                 new XYConstraints(5, 32, 256, 127));
    this.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
          System.exit(0);
        }
    });
  }

  void jButton1_actionPerformed(ActionEvent e) {

  try{
      Vector myinfo=new Vector();
      String myjicq=this.jTextField1.getText().trim();
      server=this.servername.getText();
      //serport=Integer.parseInt(this.serverport.getText());
      System.out.println("login button clicked");
      Socket socket=new Socket(InetAddress.getByName(server),serport);//连接服务器
      System.out.println("connected to server");
      BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
      PrintWriter out=new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())),true);
      out.println("login");//告诉服务器我要登录
      out.println(this.jTextField1.getText().trim());
      out.println(this.jPasswordField1.getPassword());
      System.out.println("the pwd:"+jPasswordField1.getPassword());
      String str=" ";
        //do{
      str=in.readLine().trim();//从服务器读取消息

       //如果失败就告诉出错
      if(str.equals("pwderror")) JOptionPane.showMessageDialog(this,"密码无效,请验证重输:-(","JICQ2005提示",JOptionPane.INFORMATION_MESSAGE);
      else if(str.equals("usererror")) JOptionPane.showMessageDialog(this,"您的JICQ号有误,请输入正确的JICQ号码","JICQ2005提示",JOptionPane.INFORMATION_MESSAGE);
      else if(str.equals("false")) JOptionPane.showMessageDialog(this,"对不起,服务器正忙,请稍后再试!","JICQ2005提示",JOptionPane.INFORMATION_MESSAGE);
      else{//如果成功就先接收自己的个人信息,再打开主程序
        String name,ip,status,headpic,email,info,comefrom,sex;
        name=in.readLine();
        ip=in.readLine();
        status=in.readLine();
        headpic=in.readLine();
        email=in.readLine();
        info=in.readLine();
        comefrom=in.readLine();
        sex=in.readLine();
        myinfo.add(name);
        myinfo.add(myjicq);
        myinfo.add(ip);
        myinfo.add(status);
        myinfo.add(headpic);
        myinfo.add(email);
        myinfo.add(info);
        myinfo.add(comefrom);
        myinfo.add(sex);
        System.out.println("received myinfo successfully:");
        //System.out.println("myinfo.size:"+myinfo.size());
        this.dispose();
        //int g=Integer.parseInt(this.jTextField1.getText());

        fmMain main=new fmMain(myinfo,server,serport);//changed from:fmMain(g,servername,servport);
        main.pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        //让窗体从屏幕右上角下滑
        double left=screenSize.getWidth()-main.getSize().getWidth();
        main.setLocation((int)left,0);
        main.setVisible(true);
        for (int  h=0;h<=445; h+=1){
           main.setSize(183,h);
        }
        main.setResizable(false);
        //main.setLocation();

      }
      //System.out.println("/n");
      //}while(!str.equals("ok"));
  }catch(IOException e1){System.out.println(e1.getMessage());}
  }


  void jButton2_actionPerformed(ActionEvent e) {
    server=servername.getText().toString().trim();
    this.dispose();
    fmRegister d=new fmRegister(server,serport);//打开新建窗口
    d.pack();
    d.setLocationRelativeTo(this);
    d.show();
  }

  void jButton3_actionPerformed(ActionEvent e) {
       this.dispose();
       System.exit(0);
  }
  public static void main(String[] args){
    fmLogin login=new fmLogin();
    login.pack();
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = login.getSize();
    if (frameSize.height > screenSize.height) {
     frameSize.height = screenSize.height;
   }
   if (frameSize.width > screenSize.width) {
     frameSize.width = screenSize.width;
   }
   login.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
   login.setVisible(true);
  }
}

class fmLogin_jButton1_actionAdapter implements java.awt.event.ActionListener {
  fmLogin adaptee;

  fmLogin_jButton1_actionAdapter(fmLogin adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class fmLogin_jButton2_actionAdapter implements java.awt.event.ActionListener {
  fmLogin adaptee;

  fmLogin_jButton2_actionAdapter(fmLogin adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class fmLogin_jButton3_actionAdapter implements java.awt.event.ActionListener {
  fmLogin adaptee;

  fmLogin_jButton3_actionAdapter(fmLogin adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值