?? register.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/*<p>Title:HappyChat聊天系統用戶注冊程序</p>
*<p>Description:新用戶通過輸入相關信息注冊成系統用戶</p>
*<p>Copyright:Copyright(C)2006</p>
*<p>Filename:Register.java</p>
*@author 劉志成
*@version 1.0
*/
public class Register extends JFrame implements ActionListener
{
private JComboBox comboBox;
/**
*
*/
private static final long serialVersionUID = 9019746127517522180L;
JPanel pnlRegister;
JLabel lblUserName,lblGender,lblAge;
JLabel lblPassword,lblConfirmPass,lblEmail,logoPosition;
JTextField txtUserName,txtAge,txtEmail;
JPasswordField pwdUserPassword,pwdConfirmPass;
JRadioButton rbtnMale,rbtnFemale;
ButtonGroup btngGender;
JButton btnOk,btnCancel,btnClear;
String strServerIp;
final JLabel headLabel = new JLabel();
//用于將窗口用于定位
Dimension scrnsize;
Toolkit toolkit=Toolkit.getDefaultToolkit();
//構造方法
public Register(String ip)
{
super("[HappyChat]聊天室注冊窗口");
strServerIp=ip;
pnlRegister=new JPanel();
this.getContentPane().add(pnlRegister);
lblUserName=new JLabel("用 戶 名:");
lblGender=new JLabel("性 別:");
lblAge=new JLabel("年 齡:");
lblPassword=new JLabel("口 令:");
lblConfirmPass=new JLabel("確認口令:");
lblEmail=new JLabel("電子郵件:");
txtUserName=new JTextField(30);
txtEmail=new JTextField(30);
txtAge=new JTextField(10);
pwdUserPassword=new JPasswordField(30);
pwdConfirmPass=new JPasswordField(30);
rbtnMale=new JRadioButton("男",true);
rbtnFemale=new JRadioButton("女");
btngGender=new ButtonGroup();
btnOk=new JButton("確定(O)");
btnOk.setMnemonic('O');
btnOk.setToolTipText("保存注冊信息");
btnCancel=new JButton("返回(B)");
btnCancel.setMnemonic('B');
btnCancel.setToolTipText("返回登錄窗口");
btnClear=new JButton("清空(L)");
btnClear.setMnemonic('L');
btnClear.setToolTipText("清空注冊信息");
/* 該布局采用手動布局 *
* setBounds設置組件位置 *
* setFont設置字體、字型、字號 *
* setForeground設置文字的顏色 *
* setBackground設置背景色 *
* setOpaque將背景設置為透明 */
pnlRegister.setLayout(null); //組件用手動布局
pnlRegister.setBackground(new Color(52,130,203));
lblUserName.setBounds(30,80,100,30);
txtUserName.setBounds(110,85,120,20);
lblPassword.setBounds(30,141,100,30);
pwdUserPassword.setBounds(110,146,120,20);
lblConfirmPass.setBounds(30,166,100,30);
pwdConfirmPass.setBounds(110,171,120,20);
lblGender.setBounds(30,191,100,30);
rbtnMale.setBounds(110,196,60,20);
rbtnFemale.setBounds(190,196,60,20);
lblAge.setBounds(30,216,100,30);
txtAge.setBounds(110,221,120,20);
lblEmail.setBounds(30,241,100,30);
txtEmail.setBounds(110,246,120,20);
btnOk.setBounds(246,166,80,25);
btnCancel.setBounds(246,201,80,25);
btnClear.setBounds(246,241,80,25);
Font fontstr=new Font("宋體",Font.PLAIN,12);
lblUserName.setFont(fontstr);
lblGender.setFont(fontstr);
lblPassword.setFont(fontstr);
lblConfirmPass.setFont(fontstr);
lblAge.setFont(fontstr);
lblEmail.setFont(fontstr);
rbtnMale.setFont(fontstr);
rbtnFemale.setFont(fontstr);
txtUserName.setFont(fontstr);
txtEmail.setFont(fontstr);
btnOk.setFont(fontstr);
btnCancel.setFont(fontstr);
btnClear.setFont(fontstr);
lblUserName.setForeground(Color.BLACK);
lblGender.setForeground(Color.BLACK);
lblPassword.setForeground(Color.BLACK);
lblAge.setForeground(Color.BLACK);
lblConfirmPass .setForeground(Color.BLACK);
lblEmail.setForeground(Color.BLACK);
rbtnMale.setForeground(Color.BLACK);
rbtnFemale.setForeground(Color.BLACK);
rbtnMale.setBackground(Color.white);
rbtnFemale.setBackground(Color.white);
btnOk.setBackground(Color.ORANGE);
btnCancel.setBackground(Color.ORANGE);
btnClear.setBackground(Color.ORANGE);
rbtnMale.setOpaque(false);
rbtnFemale.setOpaque(false);
pnlRegister.add(lblUserName);
pnlRegister.add(lblGender);
pnlRegister.add(lblPassword);
pnlRegister.add(lblConfirmPass);
pnlRegister.add(lblEmail);
pnlRegister.add(lblAge);
pnlRegister.add(txtAge);
pnlRegister.add(txtUserName);
pnlRegister.add(txtEmail);
pnlRegister.add(pwdUserPassword);
pnlRegister.add(pwdConfirmPass);
pnlRegister.add(btnOk);
pnlRegister.add(btnCancel);
pnlRegister.add(btnClear);
pnlRegister.add(rbtnMale);
pnlRegister.add(rbtnFemale);
btngGender.add(rbtnMale);
btngGender.add(rbtnFemale);
//設置背景圖片
Icon logo = new ImageIcon("images\\registerlogo.jpg");
logoPosition = new JLabel(logo);
logoPosition.setBounds(0, 0, 360,78);
pnlRegister.add(logoPosition);
this.setSize(360,313);
this.setVisible(true);
this.setResizable(false);
//將窗口定位在屏幕中央
scrnsize=toolkit.getScreenSize();
this.setLocation(scrnsize.width/2-this.getWidth()/2,
scrnsize.height/2-this.getHeight()/2);
Image img=toolkit.getImage("images\\appico.jpg");
this.setIconImage(img);
//三個按鈕注冊監聽
btnOk .addActionListener(this);
btnCancel.addActionListener(this);
btnClear .addActionListener(this);
final JLabel label = new JLabel();
label.setText("頭 像:");
label.setBounds(30, 120, 60, 15);
pnlRegister.add(label);
comboBox = new JComboBox();
comboBox.setAutoscrolls(true);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
comboBox.setBounds(110, 116, 47, 23);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
Icon logo = new ImageIcon("face\\"+comboBox.getSelectedItem().toString()+".jpg");
// TODO 自動生成方法存根
headLabel.setIcon(logo);
}
});
pnlRegister.add(comboBox);
headLabel.setHorizontalAlignment(SwingConstants.CENTER);
//.setIcon(SwingResourceManager.getIcon(Register.class, "face/1.jpg"));
headLabel.setIcon(new ImageIcon("face//1.JPG"));
headLabel.setBounds(247, 88, 74, 72);
pnlRegister.add(headLabel);
} //構造方法結束
//按鈕監聽響應
public void actionPerformed(ActionEvent ae)
{
Object source=new Object();
source=ae.getSource();
if (source.equals(btnOk)) //"確定"按鈕
{
register();
}
if (source.equals(btnCancel)) //"返回"按鈕
{
new Login();
this.dispose();
}
if (source.equals(btnClear)) //"清空"按鈕
{
txtUserName.setText("");
pwdUserPassword.setText("");
pwdConfirmPass.setText("");
txtAge.setText("");
txtEmail.setText("");
}
} //actionPerformed()結束
//////////"確定"按鈕事件響應//////////
@SuppressWarnings({ "deprecation", "static-access" })
public void register()
{
//接受客戶的詳細資料
Register_Customer data=new Register_Customer();
data.custName = txtUserName.getText();
data.custPassword = pwdUserPassword.getText();
data.age = txtAge.getText();
data.sex = rbtnMale.isSelected()?"男":"女";
data.email = txtEmail.getText();
//chenmin
data.head = comboBox.getSelectedItem().toString();
//驗證用戶名是否為空
if(data.custName.length()==0)
{
JOptionPane.showMessageDialog(null,"用戶名不能為空");
return;
}
//驗證密碼是否為空
if(data.custPassword.length()==0)
{
JOptionPane.showMessageDialog(null,"密碼不能為空");
return;
}
//驗證密碼的一致性
if(!data.custPassword.equals(pwdConfirmPass.getText()))
{
JOptionPane.showMessageDialog(null,"密碼兩次輸入不一致,請重新輸入");
return;
}
//驗證年齡是否為空
if(data.age.length()==0)
{
JOptionPane.showMessageDialog(null,"年齡不能為空");
return;
}
//驗證年齡的合法性
int age=Integer.parseInt(txtAge.getText());
if (age<=0||age>100){
JOptionPane.showMessageDialog(null,"年齡輸入不合法");
return;
}
//驗證Email的正確性
int Found_flag=0; //判斷標志
for (int i=0;i<data.email.length();i++)
{
if(data.email.charAt(i)=='@')
{
Found_flag++;
}
}
if(Found_flag!=1)
{
JOptionPane.showMessageDialog(null,"電子郵箱格式不正確,請重新輸入");
return;
}
try
{
//連接到服務器
Socket toServer;
toServer = new Socket(strServerIp,1001);
ObjectOutputStream streamToServer=new ObjectOutputStream (toServer.getOutputStream());
//寫客戶詳細資料到服務器socket
streamToServer.writeObject((Register_Customer)data);
//讀來自服務器socket的登陸狀態
BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
String status=fromServer.readLine();
//顯示成功消息
JOptionPane op=new JOptionPane();
op.showMessageDialog(null,status);
if(status.equals(data.custName+"注冊成功"))
{
txtUserName.setText("");
pwdUserPassword.setText("");
pwdConfirmPass.setText("");
txtAge.setText("");
txtEmail.setText("");
}
//關閉流對象
streamToServer.close();
fromServer.close();
}
catch(InvalidClassException e1)
{
JOptionPane.showMessageDialog(null,"類錯誤!");
}
catch(NotSerializableException e2)
{
JOptionPane.showMessageDialog(null,"對象未序列化!");
}
catch(IOException e3)
{
JOptionPane.showMessageDialog(null,"不能寫入到指定服務器!");
}
} //方法register()結束
public static void main(String args[])
{
new Register("127.0.0.1");
}
} //class Register 結束
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -