用JAVA打造自己的QQ–原始碼用戶端(八)

來源:互聯網
上載者:User

註冊表單的代碼fmRegister實現如下:

package client;

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

public class fmRegister extends JFrame {
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  XYLayout xYLayout2 = new XYLayout();
  JLabel jLabel2 = new JLabel();
  JTextField nickname = new JTextField();
  JLabel jLabel3 = new JLabel();
  JPasswordField password = new JPasswordField();
  JPasswordField confirm = new JPasswordField();
  JLabel jLabel4 = new JLabel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLabel5 = new JLabel();
  JRadioButton boy = new JRadioButton();
  JRadioButton girl = new JRadioButton();
  JLabel jLabel6 = new JLabel();
  JComboBox headpic = new JComboBox();
  JLabel jLabel7 = new JLabel();
  JTextField email = new JTextField();
  JLabel jLabel8 = new JLabel();
  JLabel jLabel9 = new JLabel();
  JTextArea info = new JTextArea();
  private String[] pics = new String[] { //頭像
      "1.jpg", "3.jpg",
      "5.jpg", "7.jpg"};
  String sername; //伺服器名
  int serverport; //伺服器連接埠
  Vector myinfo=new Vector();
  JTextField comefrom = new JTextField();
  public fmRegister(String s, int port) {
    try {
      sername = s;
      serverport = port;
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    xYLayout1.setWidth(262);
    xYLayout1.setHeight(350);
    this.getContentPane().setBackground(SystemColor.desktop);
    this.setForeground(SystemColor.desktop);
    this.setResizable(false);
    this.setState(Frame.NORMAL);
    this.setTitle("申請JICQ帳號");
    this.getContentPane().setLayout(xYLayout1);
    jLabel1.setFont(new java.awt.Font("隸書", 1, 18));
    jLabel1.setForeground(SystemColor.inactiveCaptionText);
    jLabel1.setOpaque(false);
    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
    jLabel1.setText("JICQ註冊嚮導");
    jPanel1.setLayout(xYLayout2);
    jLabel2.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel2.setForeground(SystemColor.desktop);
    jLabel2.setText("昵  稱:");
    jPanel1.setBackground(SystemColor.inactiveCaptionText);
    jPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
    jPanel1.setVerifyInputWhenFocusTarget(true);
    nickname.setForeground(SystemColor.desktop);
    nickname.setText("");
    jLabel3.setText("密  碼:");
    jLabel3.setForeground(SystemColor.desktop);
    jLabel3.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel4.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel4.setForeground(SystemColor.desktop);
    jLabel4.setText("確  認:");
    jButton1.setBackground(SystemColor.inactiveCaptionText);
    jButton1.setFont(new java.awt.Font("隸書", 0, 14));
    jButton1.setForeground(SystemColor.desktop);
    jButton1.setBorder(BorderFactory.createRaisedBevelBorder());
    jButton1.setText("確    定");
    jButton1.addActionListener(new fmRegister_jButton1_actionAdapter(this));
    jButton2.setBackground(SystemColor.inactiveCaptionText);
    jButton2.setFont(new java.awt.Font("隸書", 0, 14));
    jButton2.setForeground(SystemColor.desktop);
    jButton2.setBorder(BorderFactory.createRaisedBevelBorder());
    jButton2.setText("取    消");
    jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        jButton2_mouseClicked(e);
      }
    });
    password.setText("");
    confirm.setText("");
    jLabel5.setText("性  別:");
    jLabel5.setForeground(SystemColor.desktop);
    jLabel5.setFont(new java.awt.Font("隸書", 0, 14));
    boy.setBackground(SystemColor.inactiveCaptionText);
    boy.setFont(new java.awt.Font("隸書", 0, 14));
    boy.setForeground(SystemColor.desktop);
    boy.setSelected(true);
    boy.setText("帥  哥");
    girl.setText("美  女");
    girl.setForeground(SystemColor.desktop);
    girl.setBackground(SystemColor.inactiveCaptionText);
    girl.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel6.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel6.setForeground(SystemColor.desktop);
    jLabel6.setText("頭  像:");
    headpic.setBackground(SystemColor.window);
    headpic.setForeground(SystemColor.desktop);
    headpic.setBorder(BorderFactory.createLineBorder(Color.black));
    jLabel7.setForeground(SystemColor.desktop);
    jLabel7.setRequestFocusEnabled(true);
    jLabel7.setText("郵  箱:");
    jLabel7.setFont(new java.awt.Font("隸書", 0, 14));
    email.setText("");
    email.setForeground(SystemColor.desktop);
    jLabel8.setText("來  自:");
    jLabel8.setForeground(SystemColor.desktop);
    jLabel8.setRequestFocusEnabled(true);
    jLabel8.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel9.setFont(new java.awt.Font("隸書", 0, 14));
    jLabel9.setText("簽  名:");
    jLabel9.setRequestFocusEnabled(true);
    jLabel9.setForeground(SystemColor.desktop);
    info.setFont(new java.awt.Font("隸書", 0, 14));
    info.setForeground(SystemColor.desktop);
    info.setBorder(BorderFactory.createLineBorder(Color.black));
    info.setCaretColor(SystemColor.desktop);
    info.setText("");
    comefrom.setFont(new java.awt.Font("隸書", 0, 11));
    comefrom.setForeground(SystemColor.desktop);
    comefrom.setText("");
    this.getContentPane().add(jPanel1, new XYConstraints(3, 27, 254, 279));
    jPanel1.add(info,  new XYConstraints(60, 175, 179, 93));
    jPanel1.add(nickname, new XYConstraints(60, 9, 179, 17));
    jPanel1.add(jLabel2, new XYConstraints(8, 6, -1, 23));
    jPanel1.add(jLabel3, new XYConstraints(8, 33, 56, 23));
    jPanel1.add(password, new XYConstraints(60, 37, 179, 17));
    jPanel1.add(jLabel4, new XYConstraints(8, 60, 56, 23));
    jPanel1.add(confirm, new XYConstraints(60, 64, 179, 17));
    jPanel1.add(jLabel5, new XYConstraints(8, 88, 56, 23));
    jPanel1.add(boy, new XYConstraints(75, 92, 70, 18));
    jPanel1.add(girl, new XYConstraints(163, 92, 78, 18));
    jPanel1.add(jLabel7, new XYConstraints(8, 142, 56, 23));
    jPanel1.add(jLabel9, new XYConstraints(8, 169, 56, 23));
    jPanel1.add(email, new XYConstraints(60, 148, 179, 17));
    jPanel1.add(headpic, new XYConstraints(59, 120, 59, 17));
    jPanel1.add(jLabel6, new XYConstraints(8, 115, 56, 23));
    jPanel1.add(jLabel8,  new XYConstraints(126, 119, -1, 17));
    jPanel1.add(comefrom,      new XYConstraints(177, 121, 59, 18));
    this.getContentPane().add(jLabel1, new XYConstraints(46, 0, 163, 24));
    this.getContentPane().add(jButton2, new XYConstraints(145, 318, 93, 21));
    this.getContentPane().add(jButton1, new XYConstraints(21, 317, 93, 21));
    this.addWindowListener(new WindowAdapter()
     {
         public void  windowClosing(WindowEvent e){
              System.exit(0);
         }
     }                     );
  }

  void jButton1_actionPerformed(ActionEvent e) {

      //problem:confirm password
      /*if(!password.getPassword().toString().trim().equals(confirm.getPassword().toString().trim()))
      {
          Object msg="密碼輸入不一致,請重新輸入!";
          JOptionPane.showMessageDialog(this,msg,"JICQ提示您",JOptionPane.ERROR_MESSAGE);
          this.password.setText("");
          this.confirm.setText("");
          return;
      }*/
      if(boy.isSelected()&&girl.isSelected() || (!boy.isSelected()&&!girl.isSelected())){
        JOptionPane.showMessageDialog(this,"請選擇您的性別,‘帥哥’或‘美女’!","JICQ提示您",JOptionPane.ERROR_MESSAGE);
        boy.setSelected(true);
        return;
      }
    try {
      System.out.println(sername);
      System.out.println(serverport);
      System.out.println("now bagin registing your no...");
      Socket socket = new Socket(InetAddress.getByName(sername), serverport); //串連伺服器
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.
          getInputStream()));
      PrintWriter out = new PrintWriter(new BufferedWriter(
          new OutputStreamWriter(socket.getOutputStream())), true);
      String sex,ip;
      if(boy.isSelected())
         sex="帥哥";
      else
         sex="美女";
      ip=socket.getInetAddress().getLocalHost().getHostAddress();
      System.out.println("client ip:"+ip);
      out.println("new"); //發送建立使用者請求
      out.println(nickname.getText().trim()); //發送呢稱等資訊
      out.println(password.getPassword());
      out.println(email.getText().trim());
      out.println(info.getText().trim());
      out.println(comefrom.getText());
      out.println(headpic.getSelectedIndex()); //head picindex
      out.println(sex);//changed:add
      out.println(ip);//changed:add

      String str = " ";
      //do{
      str = in.readLine().trim(); //從伺服器讀取資訊
      //如果出錯
      if (str.equals("false")) JOptionPane.showMessageDialog(this, "對不起,出錯了:-(",
          "JICQ提示您", JOptionPane.INFORMATION_MESSAGE);
      else { //如果成功就告訴使用者其號碼
        String no;
        no = in.readLine().toString(); ///////////////////////////////////////////////toString
        myinfo.add(this.nickname.getText());
        myinfo.add(no);
        myinfo.add(ip);
        myinfo.add("1");
        myinfo.add(String.valueOf(this.headpic.getSelectedIndex()));
        myinfo.add(this.email.getText().trim());
        myinfo.add(this.info.getText());
        myinfo.add(sex);
        myinfo.add(this.comefrom.getText().toString());
        JOptionPane.showMessageDialog(this, "註冊成功,您的JICQ號碼為" + no, "JICQ提示您",
                                      JOptionPane.INFORMATION_MESSAGE);
        this.dispose(); //並開啟主視窗

        fmMain main = new fmMain(myinfo, sername, serverport);
        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);

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

  void jButton2_mouseClicked(MouseEvent e) {
       System.exit(0);
  }
}

class fmRegister_jButton1_actionAdapter implements java.awt.event.ActionListener {
  fmRegister adaptee;

  fmRegister_jButton1_actionAdapter(fmRegister adaptee){
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.