📄 client.java
字号:
textAge=new JTextField(30);
textPhone=new JTextField(30);
textAdd=new JTextField(30);
buttongroupSex=new ButtonGroup();
buttonOk=new Button("提交");
buttonCancel=new Button("返回");
buttonClose=new Button("重新注册");
pRegiste.setLayout(null); //组件用手动布局
labelTitle.setBounds(10,30,400,80);
labelUserName.setBounds(20,120,100,25);
textFieldUserName.setBounds(120,122,150,20);
labelPassWord.setBounds(20,160,100,25);
pUserPassword.setBounds(120,162,150,20);
labelConfirm.setBounds(20,200,100,25);
pConfirmwd.setBounds(120,202,150,20);
labelSex.setBounds(20,280,100,25);
radioMan.setBounds(200,300,60,20);
radioWoman.setBounds(200,270,100,20);
labelEmail.setBounds(20,240,100,25);
textEmail.setBounds(120,242,150,20);
labelAdd.setBounds(20,400,100,25);
textAdd.setBounds(120,402,150,20);
labelPhone.setBounds(20,360,100,25);
textPhone.setBounds(120,362,150,20);
labelAge.setBounds(20,320,100,25);
textAge.setBounds(120,322,150,20);
buttonOk.setBounds(20,480,60,20);
buttonCancel.setBounds(250,480,60,20);
buttonClose.setBounds(130,480,60,20);
z1.setBounds(280,122,100,20);z2.setBounds(280,162,100,20);
z3.setBounds(280,202,100,30);z4.setBounds(300,322,100,20);
z5.setBounds(280,282,100,20);z6.setBounds(300,362,100,20);
z7.setBounds(280,242,100,20);z0.setBounds(300,402,100,20);
b1.setBounds(120,440,500,15);
b2.setBounds(120,460,500,15);
Font fontstr=new Font("宋体",Font.PLAIN,14);
labelTitle.setFont(new Font("宋体",Font.BOLD,28));
labelUserName.setFont(new Font("隶书",Font.BOLD,18));
labelSex.setFont(new Font("隶书",Font.BOLD,18));
labelPassWord.setFont(new Font("隶书",Font.BOLD,18));
labelConfirm.setFont(new Font("隶书",Font.BOLD,18));
labelEmail.setFont(new Font("隶书",Font.BOLD,18));
labelAdd.setFont(new Font("隶书",Font.BOLD,18));
labelPhone.setFont(new Font("隶书",Font.BOLD,18));
labelAge.setFont(new Font("隶书",Font.BOLD,18));
radioMan.setFont(fontstr);
b1.setFont(new Font("宋体",Font.BOLD,16));
b2.setFont(new Font("宋体",Font.BOLD,18));
radioWoman.setFont(fontstr);
textFieldUserName.setFont(fontstr);
textEmail.setFont(fontstr);
labelTitle.setForeground(Color.green);
labelUserName.setForeground(Color.blue);
labelSex.setForeground(Color.blue);
labelPassWord.setForeground(Color.blue);
labelConfirm.setForeground(Color.blue);
labelEmail.setForeground(Color.blue);
labelAdd.setForeground(Color.blue);
labelPhone.setForeground(Color.blue);
labelAge.setForeground(Color.blue);
radioMan.setForeground(Color.white);
radioWoman.setForeground(Color.white);
radioMan.setBackground(Color.blue);
radioWoman.setBackground(Color.blue);
buttonOk.setForeground(Color.blue);
buttonClose.setForeground(Color.blue);
buttonCancel.setForeground(Color.blue);
buttonOk.setBackground(Color.pink);
buttonClose.setBackground(Color.pink);
buttonCancel.setBackground(Color.pink);
radioMan.setOpaque(false); //设置按钮为透明
radioWoman.setOpaque(false); //设置按钮为透明
pRegiste.add(labelTitle); //组件添加
pRegiste.add(labelUserName);
pRegiste.add(labelSex);
pRegiste.add(labelPassWord);
pRegiste.add(labelConfirm);
pRegiste.add(labelEmail);
pRegiste.add(textFieldUserName);
pRegiste.add(textEmail);
pRegiste.add(pUserPassword);
pRegiste.add(pConfirmwd);
pRegiste.add(labelAdd);
pRegiste.add(textAdd);
pRegiste.add(labelPhone);
pRegiste.add(textPhone);
pRegiste.add(labelAge);
pRegiste.add(textAge);
pRegiste.add(buttonOk);
pRegiste.add(buttonCancel);
pRegiste.add(buttonClose);
pRegiste.add(radioMan);
pRegiste.add(radioWoman);
pRegiste.add(z0);pRegiste.add(z1);pRegiste.add(z2);pRegiste.add(z3);pRegiste.add(z4);
pRegiste.add(z5);pRegiste.add(z6);pRegiste.add(z7);pRegiste.add(b1);pRegiste.add(b2);
buttongroupSex.add(radioMan);
buttongroupSex.add(radioWoman);
Icon logo2 = new ImageIcon("Chat\\20070606111800352.jpg"); // 添加图片
logoimagePosition2 = new JLabel(logo2);
logoimagePosition2.setBounds(0, 0, 500,550); //设置图片大小
pRegiste.add(logoimagePosition2);
fRegiste.setSize(400,550); //设置界面大小
fRegiste.setVisible(true); // 界面可见性
fRegiste.setResizable(false); // 设置最大化不可开启
scrnsize=toolkit.getScreenSize();
fRegiste.setLocation(scrnsize.width/2-fRegiste.getWidth()/2, //将窗口定位在屏幕中央
scrnsize.height/2-fRegiste.getHeight()/2);
buttonOk.addActionListener(this); // 按钮监听初始化
buttonCancel.addActionListener(this);
buttonClose.addActionListener(this);
}
public void keyReleased(KeyEvent e){} // 定义空方法
public void keyTyped(KeyEvent e){} //定义空方法
public void actionPerformed(ActionEvent e) //按钮监听
{
Object source=new Object();
source=e.getSource();
if (source.equals(buttonOk))
{
register();
}
if (source.equals(buttonCancel))
{
new LoginPanel(ipserver);
fRegiste.dispose();
}
if (source.equals(buttonClose)) // 清空按钮监听
{
textFieldUserName.setText(""); // 文本中清空为null
pUserPassword.setText("");
pConfirmwd.setText("");
textEmail.setText("");
textAdd.setText("");
textPhone.setText("");
textAge.setText("");
textFieldUserName.requestFocus(); // 光标返回到用户名文本中
}
}
public void register() //接受客户的详细资料
{
Register_Customer data=new Register_Customer();
data.custName = textFieldUserName.getText(); //定义文件 初始化
data.custPassword = pUserPassword.getText();
data.sex = radioMan.isSelected()?"男":"女";
data.email = textEmail.getText();
if(data.custName.length()==0) //验证用户名是否为空
{
JOptionPane op=new JOptionPane();
op.showMessageDialog(null,"用户名不能为空");
return;
}
if(data.custPassword.length()==0) //验证密码是否为空
{
JOptionPane op=new JOptionPane();
op.showMessageDialog(null,"密码不能为空");
return;
}
if(!data.custPassword.equals(pConfirmwd.getText())) //验证密码的一致性
{
JOptionPane op=new JOptionPane();
op.showMessageDialog(null,"密码两次输入不一致,请重新输入");
return;
}
int Found_flag=0; //验证Email的正确性
for (int i=0;i<data.email.length();i++) //判断标志
{
if(data.email.charAt(i)=='@')
{
Found_flag++;
}
}
if(Found_flag!=1) //判断格式
{
JOptionPane op=new JOptionPane();
op.showMessageDialog(null,"电子邮箱格式不正确,请重新输入");
return;
}
try
{
Socket toServer; //连接到服务器
toServer = new Socket(ipserver,2008); //设置套接字
ObjectOutputStream streamToServer=new ObjectOutputStream //写客户详细资料到服务器socket
(toServer.getOutputStream());
streamToServer.writeObject((Register_Customer)data); //读来自服务器socket的登陆状态et
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+"注册成功")) //显示成功注册
{
textFieldUserName.setText(""); //清空界面
pUserPassword.setText("");
pConfirmwd.setText("");
textEmail.setText("");
textFieldUserName.requestFocus(); //指出光标到文本处
}
streamToServer.close(); //关闭流对象
fromServer.close();
}
catch(InvalidClassException e1) //进行异常处理
{
System.out.println("The Customer class is invalid" + e1);
}
catch(NotSerializableException e2)
{
System.out.println("The object is not serializable" + e2);
}
catch(IOException e3)
{
System.out.println("Cannot write to the server" + e3);
}
}
}
class chatpanel extends Thread implements ActionListener //聊天界面
{
static JFrame fChat;
JPanel pChat;
Button bCls;
Button bExit;
Button bSend;
Button bClear;
Button bSave;
Button bAgain;
Button bPress;
JLabel lUser_List;
JLabel lUser_Message;
JLabel lSend_Message;
JLabel lChat_User;
JLabel lUser_total;
JLabel lCount;
JLabel l;
JTextField tMessage;
java.awt.List liUser_List;
TextArea taUser_Message;
JComboBox cUser;
JCheckBox chPrivateChat;
String ipserver;
String login_name;
String toUser;
String message;
String serverip;
Thread thread;
Dimension scrnsize;
Toolkit toolkit=Toolkit.getDefaultToolkit(); //将窗口定位在屏幕中央
public chatpanel(String name,String ip)
{
login_name=name;
fChat=new JFrame("★峰★聊天室"+"(用户:"+login_name+")");
pChat=new JPanel();
fChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fChat.getContentPane().add(pChat);
pChat.setBackground(Color.black);
// setCursor(Cursor.getPredefinedCursor(12)); //设置鼠标形状
String list[]={"所有人"};
bCls=new Button("清空");
bExit=new Button("退出");
bSend=new Button("发送");
bSave=new Button("保存");
bAgain=new Button("登录其他帐号");
lUser_List=new JLabel("在线用户");
l= new JLabel(new ImageIcon("Chat\\20060218_150228635.jpg"));
lUser_Message=new JLabel("请文明聊天!");
lSend_Message=new JLabel("聊天内容:");
lChat_User=new JLabel("聊天对象:");
lUser_total=new JLabel("在线人数:");
lCount=new JLabel("0");
liUser_List=new java.awt.List();
tMessage=new JTextField(170);
cUser=new JComboBox(list);
chPrivateChat=new JCheckBox("私聊");
taUser_Message=new TextArea("",300,200, ////只能向下滚动
TextArea.SCROLLBARS_VERTICAL_ONLY);
taUser_Message.setEditable(false); // 不可写入
pChat.setLayout(null); // 设置空布局
bCls.setBounds(30,500,60,25);
bExit.setBounds(170,500,60,25);
bSend.setBounds(620,460,50,25);
bSave.setBounds(100,500,60,25);
bAgain.setBounds(570,500,100,25);
lUser_List.setBounds(30,20,100,20);
lUser_Message.setBounds(185,20,300,20);
lChat_User.setBounds(30,430,100,20);
lSend_Message.setBounds(185,460,100,20);
lUser_total.setBounds(30,460,100,20);
lCount.setBounds(110,460,100,20);
liUser_List.setBounds(30,45,150,350);
taUser_Message.setBounds(185,45,480,350);
tMessage.setBounds(250,460,360,20);
cUser.setBounds(110,430,70,20);
l.setBounds(-20, -130, 1000,1000);
Font fontstr=new Font("宋体",Font.PLAIN,12);
Font fontstr1=new Font("隶书",Font.PLAIN,16);
bCls.setFont(fontstr);
bExit.setFont(fontstr);
bSend.setFont(fontstr);
bAgain.setFont(fontstr);
lUser_List.setFont(fontstr1);
lUser_Message.setFont(fontstr1);
lChat_User.setFont(fontstr);
lSend_Message.setFont(fontstr);
lUser_total.setFont(fontstr);
bExit.setBackground(Color.white);
bSend.setBackground(Color.white);
bSave.setBackground(Color.white);
bCls.setBackground(Color.white);
bExit.setForeground(Color.blue);
bSend.setForeground(Color.blue);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -