[code=java]//接一
reg_pane.add(secondName_value);
secondName_msg = new JLabel("*长度在1-12之间");
secondName_msg.setBounds(200,145,270,20);
reg_pane.add(secondName_msg);
submit = new JButton("注册");
submit.setBounds(60,190,60,20);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
if(name_value.getText().equals("")||
String.valueOf(password_value.getPassword()).equals("")||
String.valueOf(password_value1.getPassword()).equals("")||
secondName_value.getText().equals("")){
secondName_msg.setForeground(Color.red);
return;
}
if(name_msg.getForeground().equals(Color.red)||
password_msg.getForeground().equals(Color.red)||
password_msg1.getForeground().equals(Color.red)||
secondName_msg.getForeground().equals(Color.red)){
return;
}
socket = new Socket("127.0.0.1",9876);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(name_value.getText());
dos.writeUTF(String.valueOf(password_value.getPassword()));
dos.writeUTF(secondName_value.getText());
String msg = dis.readUTF();
if(msg.equals("OK")){
JOptionPane.showMessageDialog(register, "恭喜您,注册成功!");
return;
}
if(msg.equals("NO")){
JOptionPane.showMessageDialog(register, "抱歉,注册失败!");
return;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
reg_pane.add(submit);
reset = new JButton("重置");
reset.setBounds(130,190,60,20);
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
name_value.setText("");
password_value.setText("");
password_value1.setText("");
secondName_value.setText("");
name_msg.setForeground(c);
password_msg.setForeground(c);
password_msg1.setForeground(c);
secondName_msg.setForeground(c);
}
});
reg_pane.add(reset);
register.setVisible(true);
register.validate();
}
});
login_pane.add(reg);
password = new JLabel("密 码:");
password.setBounds(30,70,50,20);
login_pane.add(password);
password_value2 = new JPasswordField();
password_value2.setBounds(80,70,130,20);
login_pane.add(password_value2);
password_value2.setText(reloginPassword);
bg = new ButtonGroup();
onLine = new JRadioButton("在线");
onLine.setBounds(80,100,50,20);
onLine.setSelected(true);
login_pane.add(onLine);
hide = new JRadioButton("隐身");
hide.setBounds(140,100,50,20);
login_pane.add(hide);
bg.add(onLine);
bg.add(hide);
submit = new JButton("登 录");
submit.setBounds(67,130,64,20);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String username = name_value.getText().trim();
String password = String.valueOf(password_value2.getPassword()).trim();
userName = username;
if(userName.equals("")||password.equals("")){
msg_label.setText("提示:用户名和密码不能为空");
return;
}
if(!Character.isLetter(userName.charAt(0))||userName.length()<6||userName.length()>16){
msg_label.setText("提示:用户名有误");
return;
}
char chs[] = userName.toCharArray();
for(int i = 0 ; i<chs.length;i++){
if(!(Character.isLetter(chs)||Character.isDigit(chs))){
msg_label.setText("提示:用户名有误");
return;
}
}
if(password.length()<6||password.length()>16){
msg_label.setText("提示:密码有误");
return;
}
if(onLine.isSelected())state="在线";
else{
state = "隐身";
}
login.dispose();
new ClientMainWindow(username,password,state);
}
});
login_pane.add(submit);
exit = new JButton("退 出");
exit.setBounds(147,130,64,20);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
login_pane.add(exit);
login.setVisible(true);
login.validate();
} catch (Exception e) {
e.printStackTrace();
}
}
}[/code]3.ClientMainWindow类[code=java]package util;
import java.awt.MenuBar;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.MenuDragMouseListener;
import util.doTask.*;
public class ClientMainWindow extends JFrame implements Runnable {
public static ClientShowMyInfo csmi = null;
public static ClientChangePassword ccp = null;
public static ClientChangePassword ccr = null;
public static ClientChatGroup ccg = null;
public static ClientShowFriendInfo csfi = null;
public static ClientShowGroupInfo csgi = null;
public static ClientAddFriend caf = null;
public static ClientAddGroup cag = null;
public static HashMap<String,ClientChatWindow> chat;
public static HashMap<String,ClientChatGroupWindow> chatGroup;
public static String userName;
public String state;
public String password;
public static String mySecondName;
public static int userCount=0;
public static Vector<String> allUserName;
public Vector<HashMap> allFriend;
public HashMap<String,String> friend;
public Vector<String> stopFriend;
public int onNum;
public static Socket u_socket=null;
public static DataOutputStream dos;
public static DataInputStream dis;
public String msg;
public JLabel login_msg;
public JButton login_quit;
public JMenuBar menuBar;
public JMenu menu1;
public JMenuItem menu1_1;
public JMenuItem menu1_2;
public JMenuItem menu1_3;
public JMenu menu2;
public JMenu menu2_1;
public JMenuItem menu2_1_1;
public JMenuItem menu2_1_2;
public JMenu menu2_2;
public JMenuItem menu2_2_1;
public JMenuItem menu2_2_2;
public JMenu menu2_3;
public JMenuItem menu2_3_1;
public JMenuItem menu2_3_4;
public JMenuItem menu2_3_2;
public JMenuItem menu2_3_3;
public JMenu menu3;
public JMenuItem menu3_1;
public JMenuItem menu3_2;
public JMenu menu4;
public JMenuItem menu4_1;
public JMenuItem menu4_2;
public JLabel friendListLabel;
public JScrollPane friendListPane;
public JList memberList;
public static DefaultListModel model;
public JLabel currentNumber;
public String friendSecondName;
public JTextArea mainText;
public JPanel setInfoPane;
public String [] setInfoStrs = {"举报","屏蔽","取消屏蔽"};
public JList setInfoList;
public JPopupMenu jpm;
static String f;
static int num;
static int len;
public ClientMainWindow(){}
public ClientMainWindow(String name,String password,String state){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
});
userName = name;
this.password = password;
this.state = state;
init();
}
public void init(){
this.setLayout(null);
this.setBounds(700,50,260,550);
this.setTitle("聊天程序");
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
write("EXIT");
}
});
this.setVisible(true);
login();
this.validate();
}
public int confirm(){
int num = (JOptionPane.showConfirmDialog(this,"确定退出?","提示信息",0,1));
return num;
}
public void write(String msg){
try {
dos.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
public void showMainWindow(){
stopFriend = new Vector<String>();
this.remove(login_msg);
this.remove(login_quit);
this.setTitle(" UNIX "+mySecondName+"("+userName+")");
menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menu1 = new JMenu("我的信息");
menu1_1 = new JMenuItem("我的资料");
menu1_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
showMyInfo();
write("m"+userName);
}
});
menu1.add(menu1_1);
menu1_2 = new JMenuItem("修改密码");
menu1_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
showChangePassword();
}
});
menu1.add(menu1_2);
menu1_3 = new JMenuItem("退出");
menu1_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(confirm()==1)return;
write("EXIT");
}
});
menu1.add(menu1_3);
menuBar.add(menu1);
menu2 = new JMenu(" 设 置 ");
menu2_1 = new JMenu("更换皮肤");
menu2_1_1 = new JMenuItem("白色冬天");
menu2_1_2 = new JMenuItem("蓝色海洋");
menu2_1.add(menu2_1_1);
menu2_1.add(menu2_1_2);
menu2.add(menu2_1);
menu2_2 = new JMenu(" 添 加 ");
menu2_2_1 = new JMenuItem("添加好友");
menu2_2_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
showAddFriend();
}
});
//接三[/code] |
|