📄 denglu.java
字号:
//==========================
//引入类包
import java.awt.*;//除以“J”打头的控件以外的类包
import javax.swing.*;//以“J”打头的控件类包
import java.awt.event.*;//实现事件
import java.net.*;//实现网络通信
import java.io.*;//实现流信息的传递
import java.util.*;//实现线程
import java.util.Date;//封装系统日期和时间信息
import java.util.Calendar;
import java.util.GregorianCalendar;
//=====================================================
//声明一个接受登陆信息的类
class LoginMessage extends Object implements Serializable
{
String name,password;
}//接受登陆信息的类结束
//==============================================================
//声明一个公有类
public class denglu extends JFrame implements ActionListener
{
JPanel jp;//容器框架变量
JLabel jl_name,jl_pw,jl,jl_ip,jl_p,jl_p1;//静态文本变量
JTextField jt_name,jt_ip;//文本框变量
JPasswordField jpf;//密码框变量
JButton jb1,jb2,jb3;//按钮变量
GridBagLayout gb;//网格包变量
GridBagConstraints gbc;//设置网格包属性变量
String name,password;//存储登陆信息的字符串变量
String ip;//存储IP地址的字符串变量
//====================================
//构造函数
public denglu()
{
super("登录框");
jp=new JPanel();//初始化容器控件
setContentPane(jp);//把容器添加到框架中去
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置应用程序的关闭
jp.setBackground(new Color(118,228,223));//设置容器的背景颜色
//============初始化静态文本
Font f1=new Font("华文新魏",Font.BOLD,16);//设置字体属性
jl_p=new JLabel(new ImageIcon("image/EyeAll.gif"));
jl_p1=new JLabel(new ImageIcon("image/PREVIEW.GIF"));
jl_ip=new JLabel("IP:");
jl_ip.setFont(f1);
jl_name=new JLabel("用户名");//初始化标题
jl_name.setFont(f1);
jl=new JLabel("兔巴哥聊天室");
Font f=new Font("华文新魏",Font.BOLD,24);//设置字体属性
jl.setFont(f);//添加字体属性
jl.setForeground(Color.red);//设置字体颜色
jl_pw=new JLabel("密码");//初始化标题
jl_pw.setFont(f1);//添加字体属性
//==============初始化文本框
jt_ip=new JTextField(20);
jt_name=new JTextField(20);
//==============初始化密码框
jpf=new JPasswordField(20);
//==============初始化按钮
jb1=new JButton("登录");
jb2=new JButton("注册");
jb3=new JButton("清空");
//============实现按钮的监听
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
//============初始化网格包属性
gb=new GridBagLayout();
gbc=new GridBagConstraints();
jp.setLayout(gb);//将网格包添加到容器中去
//=============设置各控件的位置
set(7,0,1,1,jl_p1);
set(7,1,1,1,jl);
set(7,2,1,1,jl_p);
set(3,8,0,0,jl_name);set(7,8,0,0,jt_name);
set(3,13,0,0,jl_pw);set(7,13,0,0,jpf);
set(3,18,0,0,jl_ip);set(7,18,0,0,jt_ip);
set(5,23,0,1,jb1);set(7,23,0,1,jb2);set(9,23,0,1,jb3);
setSize(500,400);
setVisible(true);
}//应用程序中会自动调用的函数结束
//===========================================调用设置控件位置函数
public void set(int i,int j,double x,double y,Component con)
{
gbc.anchor=GridBagConstraints.CENTER;
gbc.gridx=i;
gbc.gridy=j;
gbc.weightx=x;
gbc.weighty=y;
gb.setConstraints(con,gbc);
jp.add(con);
}
//=============================================按钮事件代码
public void actionPerformed(ActionEvent a)
{
Object obj=a.getSource();
name=jt_name.getText();//存取用户名
password=new String(jpf.getPassword());//存取密码
if(obj==jb1)
{
if (name.length()==0 && password.length()==0)//判断当输入的用户名和密码不为空时的情况
{
JOptionPane.showMessageDialog(null,"用户名和密码不能为空!");//弹出消息框
return;
}
if (name.length()==0)//判断当输入的用户名不为空时的情况
{
JOptionPane.showMessageDialog(null,"用户名不能为空!");//弹出消息框
return;
}
if (password.length()==0)//判断当输入的密码不为空时的情况
{
JOptionPane.showMessageDialog(null,"密码不能为空!");//弹出消息框
return;
}
if (jt_ip.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"IP地址不能为空!");//弹出消息框
return;
}
if (jt_ip.getText().length()!=0)
{
ip=jt_ip.getText();//存取地址
}
//================初始化Message类及其内的变量值
LoginMessage d=new LoginMessage();
d.name=name;
d.password=password;
if(name.length()!=0 && password.length()!=0)
{
try//处理异常
{
//设置文件输出流
Socket socket=new Socket(ip,1003);//连接到服务器
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(d);
//==================读取信息
InputStreamReader in=new InputStreamReader(socket.getInputStream());
BufferedReader br=new BufferedReader(in);
String str=br.readLine();
if(str.equals("ok"))//判断当服务器发送过来的信息为"ok"时的情况
{
JOptionPane.showMessageDialog(null,str); //弹出消息
new liao();
dispose();
}
else
{
JOptionPane.showMessageDialog(null,str);//弹出消息框
}
}
catch(Exception e)
{
System.out.println("File write Error!");
}
}
}
if (obj==jb2)
{
try
{
new zhuce();
dispose();
}
catch (Exception e)
{
}
}
if (obj==jb3)//清除所有内容
{
jt_name.setText("");
jpf.setText("");
jt_ip.setText("");
}
}//按钮事件代码结束
//============================================主函数
public static void main(String[] args)
{
new denglu();
}//主函数结束
}//整个公有类结束
//===========================================================================================
//声明一个接受注册信息的类
class Message implements Serializable
{
String name,password,sex,birth,email,question,answer;
}//接受注册信息的类结束
//==============================================================
//声明一个注册类
class zhuce extends JFrame implements ActionListener
{
String name,password,sex,year,month,day,birth,email,question,answer,total;//存储各注册信息的字符串变量
JPanel jp;//容器框架变量
JLabel jl_p,jl_p2,jl,jl_line,jl_line2,jl_name,jl_name2,jl_pw,jl_pw1,jl_pw2,jl_pw22,jl_email,jl_email2,jl_sex,jl_birth,jl_birth2,jl_year,jl_month,jl_day,jl_question,jl_question2,jl_answer,jl_answer2,jl_ip,jl_ip2;//声明静态文本变量
JButton jb1,jb2;//按钮变量
JTextField jt_name,jt_email,jt_question,jt_answer,jt_ip;//文本框变量
JPasswordField jpf,jpf2;//密码框变量
JComboBox jc_year,jc_month,jc_day,jc_sex;//列表框变量
GridBagLayout gb;//网格包变量
GridBagConstraints gbc;//设置网格包属性变量
String ip;//存储IP地址的字符串变量
//====================================构造函数
public zhuce()
{
super("注册表");
jp=new JPanel();//初始化容器控件
setContentPane(jp);//把容器添加到框架中去
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置应用程序的关闭
jp.setBackground(new Color(255,204,255));//设置容器的背景颜色
//============初始化静态文本
jl_p=new JLabel(new ImageIcon("image/marqueeleft.gif"));
jl_p2=new JLabel(new ImageIcon("image/marqueeright.gif"));
jl_ip=new JLabel("IP:");
jl_ip2=new JLabel("*(必填)");
jl_ip2.setForeground(Color.red);//设置字体颜色
jl=new JLabel("‘兔巴哥‘注册框");//初始化标题
Font f=new Font("华文新魏",Font.BOLD,24);//设置字体属性
jl.setForeground(Color.blue);//设置字体颜色
jl.setFont(f);//添加字体属性
jl_line=new JLabel("==============================");
jl_line.setForeground(Color.black);//设置字体颜色
jl_name=new JLabel("用户名:");//初始化标题
jl_name2=new JLabel("*(只能输入字母,数字,下划线)");//初始化标题
jl_name2.setForeground(Color.red);//设置字体颜色
jl_pw=new JLabel("密码:");//初始化标题
jl_pw1=new JLabel("*(请不要使用非法字符)");//初始化标题
jl_pw1.setForeground(Color.red);//设置字体颜色
jl_pw2=new JLabel("确认密码:");//初始化标题
jl_pw22=new JLabel("*(密码6-16位)请再输一遍确认");//初始化标题
jl_pw22.setForeground(Color.red);//设置字体颜色
jl_email=new JLabel("E-mail地址:");//初始化标题
jl_email2=new JLabel("*(请输入您的电子邮箱地址)");//初始化标题
jl_email2.setForeground(Color.red);//设置字体颜色
jl_sex=new JLabel("性别:");//初始化标题
jl_birth=new JLabel("出生日期:");//初始化标题
jl_birth2=new JLabel("(请按照1995-1-1格式写)");//初始化标题
jl_birth2.setForeground(Color.red);//设置字体颜色
jl_year=new JLabel("年");//初始化标题
jl_year.setForeground(Color.red);//设置字体颜色
jl_month=new JLabel("月");//初始化标题
jl_month.setForeground(Color.red);//设置字体颜色
jl_day=new JLabel("日");//初始化标题
jl_day.setForeground(Color.red);//设置字体颜色
jl_question=new JLabel("问题:");//初始化标题
jl_question2=new JLabel("(不超过255个字符)");
jl_question2.setForeground(Color.red);//设置字体颜色
jl_answer=new JLabel("答案:");//初始化标题
jl_answer2=new JLabel("(不超过255个字符)");
jl_answer2.setForeground(Color.red);//设置字体颜色
//==============初始化按钮
jb1=new JButton("开始注册");
jb2=new JButton("重写");
//==============初始化文本框
jt_ip=new JTextField(20);
jt_name=new JTextField(20);
jt_email=new JTextField(20);
jt_question=new JTextField(20);
jt_answer=new JTextField(20);
//==============初始化密码框
jpf=new JPasswordField(20);
jpf2=new JPasswordField(20);
//==============初始化列表框
jc_year=new JComboBox();
for(int i=1900;i<=2004;i++)
jc_year.addItem(""+i);
jc_month=new JComboBox();
for(int k=1;k<=12;k++)
jc_month.addItem(""+k);
jc_day=new JComboBox();
for (int j=1;j<=31;j++)
jc_day.addItem(""+j);
String str[]={"男","女"};
jc_sex=new JComboBox(str);
//============初始化网格包属性
gb=new GridBagLayout();
gbc=new GridBagConstraints();
//============实现按钮的监听
jb1.addActionListener(this);
jb2.addActionListener(this);
jp.setLayout(gb);//将网格包添加到容器中去
//=============设置各控件的位置
set(0,0,jl_p);set(3,0,jl);set(5,0,jl_p2);
set(3,1,jl_line);
set(0,3,jl_name);set(3,3,jt_name);set(5,3,jl_name2);
set(0,5,jl_pw);set(3,5,jpf);set(5,5,jl_pw1);
set(0,7,jl_pw2);set(3,7,jpf2);set(5,7,jl_pw22);
set(0,9,jl_email);set(3,9,jt_email);set(5,9,jl_email2);
set(0,11,jl_sex);set(3,11,jc_sex);
set(0,13,jl_birth);set(3,13,jc_year);set(4,13,jl_year);set(5,13,jc_month);set(6,13,jl_month);set(7,13,jc_day);set(8,13,jl_day);
set(0,15,jl_question);set(3,15,jt_question);set(5,15,jl_question2);
set(0,17,jl_answer);set(3,17,jt_answer);set(5,17,jl_answer2);
set(0,19,jl_ip);set(3,19,jt_ip);set(5,19,jl_ip2);
set(3,25,jb1);set(5,25,jb2);
setSize(600,400);
setVisible(true);
}//构造函数结束
//===========================================调用设置控件位置函数
public void set(int i,int j,Component con)
{
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=i;
gbc.gridy=j;
gb.setConstraints(con,gbc);
jp.add(con);
}
//=============================================按钮事件代码
public void actionPerformed(ActionEvent a)
{
Object obj=a.getSource();
if (obj==jb1)
{
try
{
if (jt_name.getText().length()!=0)//判断当输入的用户名不为空时的情况
{
name=jt_name.getText();//存取用户名
}
else
{
JOptionPane.showMessageDialog(null,"用户名不能为空!");//弹出消息框
return;
}
if (jt_ip.getText().length()!=0)
{
ip=jt_ip.getText();//存取地址
}
if (String.valueOf(jpf.getPassword()).length()!=0)//判断当输入的密码不为空时的情况
{
if (String.valueOf(jpf.getPassword()).length()<6)//判断当密码小于六时的情况
{
JOptionPane.showMessageDialog(null,"密码过于简单,请重新输入!");//弹出消息框
return;
}
if (String.valueOf(jpf.getPassword()).length()>16)//判断当密码大于十六时的情况
{
JOptionPane.showMessageDialog(null,"密码超出范围,请重新输入!");//弹出消息框
return;
}
if (String.valueOf(jpf.getPassword()).equals(String.valueOf(jpf2.getPassword())))//验证密码是否相等
{
password=String.valueOf(jpf.getPassword());//存取密码
}
else
{
JOptionPane.showMessageDialog(null,"密码与原密码不符,请重新输入!");//弹出消息框
jpf2.setText(""); //清除文字
return;
}
}
else
{
JOptionPane.showMessageDialog(null,"密码不能为空!");//弹出消息框
return;
}
sex= (String)jc_sex.getSelectedItem();//存取性别变量信息
year=(String)jc_year.getSelectedItem();//存取年变量信息
month=(String)jc_month.getSelectedItem();//存取月变量 信息
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -