当前位置:文档之家› 基于java开发的网络聊天室--------[Login.java]

基于java开发的网络聊天室--------[Login.java]

import javax.swing.*;import java.awt.*;import java.awt.event.*;import .*;import java.io.*;import .*;/*** <p>* Title: HappyChat聊天系统登录程序* </p>* <p>* Description: 根据指定的服务器地址、用户名和密码登录聊天服务器* </p>* <p>* Copyright: Copyright (c) 2006* </p>* <p>* Filename: Login.java* </p>** @author 公子明* @version 1.0**/public class Login extends JFrame implements ActionListener {private static final long serialVersionUID = -8965773902056088264L;//通过这种方式来解决不同的版本之间的串行话问题。

private JPanel pnlLogin;//声明面板private JButton btnLogin, btnRegister, btnExit;//声明按钮private JLabel lblServer, lblUserName, lblPassword, lblLogo;//声明标签private JTextField txtUserName, txtServer;//声明文本域private JPasswordField pwdPassword;//声明密码输入文本域private String strServerIp;//字符串变量// 用于将窗口定位private Dimension scrnsize;private Toolkit toolkit =Toolkit.getDefaultToolkit();//getDefaultToolkit()返回默认工具包/*** 构造登陆窗体*/public Login() {super("登录");//创建一个新的、初始不可见的、标题为登录[HappyChat]聊天室的 Frame。

pnlLogin = new JPanel();this.getContentPane().add(pnlLogin);//this.getContentPane返回此窗体的 contentPane 对象,添加面板//Banner=new JLabel("欢迎光临HappyChat聊天室");//Banner2=new JLabel("---by 公子明");lblServer = new JLabel("服务器(S):");lblUserName = new JLabel("用户名(U):");lblPassword = new JLabel("口令(P):");txtServer = new JTextField(20);txtServer.setText("120.95.160.59");txtServer.setEditable(false);//文本内容不可编辑txtUserName = new JTextField(20);pwdPassword = new JPasswordField(20);btnLogin = new JButton("登录(L)");btnLogin.setToolTipText("登录到服务器");//setToolTipText鼠标放在Button按钮上,出现<登录到服务器>提示信息btnLogin.setMnemonic('L');//菜单快捷键btnRegister = new JButton("注册(R)");btnRegister.setToolTipText("注册新用户");btnRegister.setMnemonic('R');btnExit = new JButton("退出(X)");btnExit.setToolTipText("退出系统");btnExit.setMnemonic('X');/***************************************************************** ******* 该布局采用手动布局 ** setBounds设置组件位置 ** setFont设置字体、字型、字号 ** setForeground设置文字的颜色 ** setBackground设置背景色 ** setOpaque将背景设置为透明 ******************************************************************* *****/pnlLogin.setLayout(null); // 设置jpane;的布局管理器为空,自己手动的设置组件的坐标位置和大小pnlLogin.setBackground(new Color(231,240,249));//设置此组件的背景色//Banner.setBounds(45, 20, 300, 25 );//Banner2.setBounds(200, 50, 100, 25 );lblServer.setBounds(60, 80, 80, 25);//移动组件并调整其大小,使其符合新的有界矩形txtServer.setBounds(150, 80, 120, 25);lblUserName.setBounds(60, 110, 80, 25);txtUserName.setBounds(150, 110, 120, 25);lblPassword.setBounds(60, 140, 80, 25);pwdPassword.setBounds(150, 140, 120, 25);btnLogin.setBounds(30, 200, 80, 25);btnRegister.setBounds(130, 200, 80, 25);btnExit.setBounds(230, 200, 80, 25);Font fontstr = new Font("宋体", Font.PLAIN, 12);Font fontstr2 = new Font("华康少女文字W5(P)", Font.PLAIN, 13);//Font fontstr3 = new Font("黑体", Font.PLAIN, 20);//Font fontstr4 = new Font("華康圓緣體", Font.PLAIN, 14);//Banner.setFont(fontstr3);//Banner2.setFont(fontstr4);lblServer.setFont(fontstr);txtServer.setFont(fontstr);lblUserName.setFont(fontstr);txtUserName.setFont(fontstr);lblPassword.setFont(fontstr);pwdPassword.setFont(fontstr);btnLogin.setFont(fontstr2);btnRegister.setFont(fontstr2);btnExit.setFont(fontstr2);lblUserName.setForeground(Color.BLACK);lblPassword.setForeground(Color.BLACK);btnLogin.setBackground(new Color(56,198,238));btnRegister.setBackground(new Color(56,198,238));btnExit.setBackground(new Color(56,198,238));//pnlLogin.add(Banner);//pnlLogin.add(Banner2);pnlLogin.add(lblServer);pnlLogin.add(txtServer);pnlLogin.add(lblUserName);pnlLogin.add(txtUserName);pnlLogin.add(lblPassword);pnlLogin.add(pwdPassword);pnlLogin.add(btnLogin);pnlLogin.add(btnRegister);pnlLogin.add(btnExit);// 设置背景图片.URL imgURL =Login.class.getResource("/images/loginlogo.jpg");ImageIcon logo1 = new ImageIcon(imgURL);//Icon logo1 = new ImageIcon("images/loginlogo.jpg");lblLogo = new JLabel(logo1);lblLogo.setBounds(0, 0, 340, 66);pnlLogin.add(lblLogo);// 设置登录窗口setResizable(false);//此窗体不可调整大小的setSize(340, 260); //设置窗体的大小setVisible(true);//窗体可见scrnsize = toolkit.getScreenSize();//获取屏幕的大小setLocation(scrnsize.width / 2 - this.getWidth() / 2, scrnsize.height / 2 - this.getHeight() / 2);//设置窗体位置.URL imgURL2 =Login.class.getResource("/images/appico.jpg");Image img = toolkit.getImage(imgURL2);//返回图像//Image必须以特定于平台的方式获取图像,这也是和Icon不同的地方setIconImage(img);//设置要作为此窗口图标显示的图像// 三个按钮注册监听btnLogin.addActionListener(this);btnRegister.addActionListener(this);btnExit.addActionListener(this);} // 构造方法结束/*** 按钮监听响应*/@SuppressWarnings({ "deprecation", "static-access" })public void actionPerformed(ActionEvent ae) {Object source = ae.getSource();//取得事件源if (source.equals(btnLogin)) {// 判断用户名和密码是否为空if (txtUserName.getText().equals("")|| pwdPassword.getText().equals("")) {JOptionPane op1 = new JOptionPane();//声明一个弹出框op1.showMessageDialog(null, "用户名或密码不能为空");//告知用户某事已发生} else {strServerIp = txtServer.getText();//取得服务器ip地址login();}}if (source.equals(btnRegister)) {strServerIp = txtServer.getText();this.dispose();new Register(strServerIp);}if (source == btnExit) {System.exit(0);}} // actionPerformed()结束/*** 登录事件响应方法*/@SuppressWarnings("deprecation")public void login() {// 接受客户的详细资料Customer data = new Customer();data.custName = txtUserName.getText();data.custPassword = pwdPassword.getText();try {// 连接到服务器Socket toServer;//声明Socket对象//【第一步】创建套接字对象toServer = new Socket(strServerIp, 1001);//创建一个流套接字并将其连接到指定 IP 地址的指定端口号。

相关主题