展开全部
package 课设;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.util.Random;
import java.io.*;
import javax.swing.border.*;
public class Register extends JFrame implements ActionListener {//注册用户类
//以下创建程序界面e5a48de588b63231313335323631343130323136353331333339663934
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField nickname = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JPasswordField password = new JPasswordField();
JLabel jLabel9 = new JLabel();
JPasswordField repassword = new JPasswordField();
JLabel jLabel4 = new JLabel();
JTextField email = new JTextField();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JTextField ip = new JTextField();
JLabel jLabel10 = new JLabel();
JTextField getport = new JTextField();
JButton ok = new JButton();
JButton cancel = new JButton();
JLabel jLabel7 = new JLabel();
JRadioButton boy = new JRadioButton();
JRadioButton girl = new JRadioButton();
JLabel jLabel8 = new JLabel();
JComboBox place = new JComboBox();
JComboBox headpic = new JComboBox();
Socket registersocket=new Socket();
DataInputStream in;
DataOutputStream out;
String server;
//***************************
private String[] pics = new String[] {"image/1.jpg", "image/2.jpg","image/3.jpg", "image/4.jpg","image/5.jpg","image/6.jpg","image/7.jpg","image/8.jpg","image/9.jpg","image/10.jpg","image/11.jpg","image/12.jpg","image/13.jpg","image/14.jpg"};//头像
private String[] places = new String[] {//籍贯
"北京","天津","上海","重庆","黑龙江","哈尔滨","辽宁","吉林","内蒙","山西","陕西","河北",
"河南","山东","安徽","浙江","江苏","江西","福建","广东","湖南","湖北","广西","青海",
"甘肃","宁夏","新疆","西藏","四川","贵州","云南","海南","香港","澳门"};
static String sername;//服务器名
int serverport;//服务器端口
public Register(int port,String sername){//构造函数
serverport=port;
server=sername;
Regwin();
}
// public static void main(String args[]){
// new Register(8888,"127.0.0.1");
// }
public void Regwin(){//创建窗口
this.setBounds(200,150,420,460);
panel1.setLayout(null);
this.getContentPane().setLayout(null);
panel1.setBounds(new Rectangle(-2, 0, 419, 452));
this.setTitle("注册");
jLabel1.setText("请填写以下内容");
jLabel1.setForeground(Color.RED);
jLabel1.setBounds(new Rectangle(9, 9, 103, 18));
jLabel2.setText("昵称");
jLabel2.setForeground(Color.RED);
jLabel2.setBounds(new Rectangle(45, 45, 41, 18));
nickname.setBounds(new Rectangle(120, 44, 150, 22));
jLabel3.setText("密码");
jLabel3.setForeground(Color.RED);
jLabel3.setBounds(new Rectangle(45, 70, 41, 18));
password.setBounds(new Rectangle(120, 70, 150, 22));
jLabel9.setText("密码确认");
jLabel9.setForeground(Color.RED);
jLabel9.setBounds(new Rectangle(45, 95, 58, 18));
repassword.setBounds(new Rectangle(120, 95, 150, 22));
jLabel4.setText("电子邮件");
jLabel4.setForeground(Color.RED);
jLabel4.setBounds(new Rectangle(45, 120, 58, 18));
email.setBounds(new Rectangle(120, 120, 150, 22));
jLabel5.setText("头像");
jLabel5.setForeground(Color.RED);
jLabel5.setBounds(new Rectangle(45, 145, 41, 18));
ComboBoxModel model = new HeadPicCombobox(pics);
ListCellRenderer renderer = new HeadpicCellRenderer();
headpic.setBounds(new Rectangle(120, 145, 150, 60));
headpic.setModel(model);
headpic.setRenderer(renderer);
jLabel7.setText("性别");
jLabel7.setForeground(Color.RED);
jLabel7.setBounds(new Rectangle(45, 215, 41, 18));
boy.setText("男");
boy.setForeground(Color.RED);
boy.setBounds(new Rectangle(120, 215, 38, 26));
girl.setText("女");
girl.setForeground(Color.RED);
girl.setBounds(new Rectangle(160, 215, 38, 26));
ButtonGroup group=new ButtonGroup();
group.add(boy);
group.add(girl);
jLabel8.setText("来自");
jLabel8.setForeground(Color.RED);
jLabel8.setBounds(new Rectangle(45, 245, 41, 18));
place=new JComboBox(places);
place.setToolTipText("");
place.setBounds(new Rectangle(120, 245, 163, 22));
jLabel6.setText("IP地址:");
jLabel6.setForeground(Color.RED);
jLabel6.setBounds(new Rectangle(45, 275, 58, 18));
ip.setBounds(new Rectangle(120, 275, 150, 22));
ip.setText("127.0.0.1");
jLabel10.setText("端口号:");
jLabel10.setForeground(Color.RED);
jLabel10.setBounds(new Rectangle(45, 300, 58, 18));
getport.setBounds(new Rectangle(120, 300, 150, 22));
ok.setText("确定");
ok.setBounds(new Rectangle(87, 330, 79, 29));
ok.addActionListener(this);
cancel.setText("取消");
cancel.setBounds(new Rectangle(260, 329, 79, 29));
cancel.addActionListener(this);
this.getContentPane().add(panel1);
panel1.add(jLabel2);
panel1.add(jLabel1);
panel1.add(nickname);
panel1.add(jLabel3);
panel1.add(password);
panel1.add(jLabel9);
panel1.add(repassword);
panel1.add(jLabel4);
panel1.add(email);
panel1.add(jLabel5);
panel1.add(cancel);
panel1.add(ok);
panel1.add(jLabel6);
panel1.add(ip);
panel1.add(jLabel10);
panel1.add(getport);
panel1.add(jLabel7);
panel1.add(boy, null);
panel1.add(jLabel8, null);
panel1.add(girl, null);
panel1.add(place, null);
panel1.add(headpic, null);
panel1.setBackground(Color.cyan);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
InetAddress address;
try {
address = InetAddress.getByName(server);
InetSocketAddress socketaddress=new InetSocketAddress(address,serverport);
registersocket.connect(socketaddress);
in=new DataInputStream(registersocket.getInputStream());//创建输入流
out=new DataOutputStream(registersocket.getOutputStream());//创建输出流
} catch (UnknownHostException e4) {
JOptionPane.showMessageDialog(null, "服务器连接失败");
} catch (SocketException e1) {
System.out.println("与服务器的连接断开了!!!");
}catch (IOException e1) {
e1.printStackTrace();
}
String username=nickname.getText().trim();
String password1=new String(password.getPassword()).trim();
String email1=email.getText().trim();
String place1=place.getSelectedItem().toString().trim();
int pic=headpic.getSelectedIndex();
String sex1=boy.isSelected()?"男":"女";
int port=Integer.parseInt(getport.getText().trim());
String ip1=ip.getText().trim();
int status=0;
if (username.length() == 0) {
JOptionPane.showMessageDialog(null, "用户名不能为空");
return;
}
if (password1.length() == 0) {
JOptionPane.showMessageDialog(null, "密码不能为空");
return;
}
if (!password1.equals(new String(repassword.getPassword()))) {
JOptionPane.showMessageDialog(null, "密码两次输入不一致,请重新输入");
return;
}
int Found_flag = 0; //判断email中的@标识
for (int i = 0; i < email1.length(); i++) {
if (email1.charAt(i) == '@') {
Found_flag++;
}
}
if (Found_flag != 1) {
JOptionPane.showMessageDialog(null, "电子邮箱格式不正确,请重新输入");
return;
}
try {
out.writeUTF("register");//发送新建用户请求
Random r=new Random(); //产生随机号码
int no=r.nextInt(1000000000);
out.writeInt(no);
out.writeUTF(username);
out.writeUTF(password1);
out.writeInt(status);
out.writeInt(pic);
out.writeUTF(sex1);
out.writeUTF(email1);
out.writeUTF(place1);
out.writeUTF(ip1);
out.writeInt(port);
String str=in.readUTF().trim();//从服务器读取信息
if(str.equals("false")){//如果出错
JOptionPane.showMessageDialog(this,"对不起,注册失败!:-(","ok",JOptionPane.INFORMATION_MESSAGE);
}
if(str.equals("ok")){//如果成功就告诉用户其号码
JOptionPane.showMessageDialog(this,"注册成功,你的号码是"+no,"ok",JOptionPane.INFORMATION_MESSAGE);
this.dispose();//关闭该窗口
new New();
}
}
catch (InvalidClassException e1) {
JOptionPane.showMessageDialog(null, "类错误!");
}
catch (NotSerializableException e2) {
JOptionPane.showMessageDialog(null, "对象未序列化!");
}
catch (IOException e3) {
JOptionPane.showMessageDialog(null, "不能写入到指定服务器!");
}finally{
try {
//关闭流对象
out.close();
in.close();
registersocket.close(); //关闭连接
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
else if(e.getSource()==cancel){
this.dispose();
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起