java需要登录的聊天_java在线聊天项目 客户端登陆窗口LoginDialog的注册用户功能...

packagecom.swift.frame;importjava.awt.EventQueue;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.ConnectException;importjava.net.Socket;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.SQLException;importjavax.swing.JButton;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JPasswordField;importjavax.swing.JTextField;importjavax.swing.UIManager;importjavax.swing.UnsupportedLookAndFeelException;importjavax.swing.border.TitledBorder;importcom.swift.jdbc.DBAdd;importcom.swift.jdbc.DBUtil;importcom.swift.jdbc.User;importcom.swift.util.Center;public class LoginDialog extendsJDialog {privateJPasswordField passwordField_2;privateJPasswordField passwordField_1;privateJTextField textField_2;privateJTextField textField;

Socket s;

DataOutputStream dos;

DataInputStream dis;public static voidmain(String args[]) {

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);try{

UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

}catch(ClassNotFoundException e1) {

e1.printStackTrace();

}catch(InstantiationException e1) {

e1.printStackTrace();

}catch(IllegalAccessException e1) {

e1.printStackTrace();

}catch(UnsupportedLookAndFeelException e1) {

e1.printStackTrace();

}

EventQueue.invokeLater(newRunnable() {public voidrun() {try{

LoginDialog dialog= newLoginDialog();

dialog.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

System.exit(0);

}

});

dialog.setVisible(true);

}catch(Exception e) {

e.printStackTrace();

}

}

});

}publicLoginDialog() {super();

setResizable(false);

setTitle("在线聊天登录框");

getContentPane().setLayout(null);

setBounds(100, 100, 427, 301);//注册时高度为578,不注册是301//设置窗口居中

this.setLocation(Center.getPoint(this.getSize()));final JButton button_1 = newJButton();

button_1.setText("登录");

button_1.setBounds(230, 222, 106, 36);

getContentPane().add(button_1);final JTextField textField_1 = newJTextField();

textField_1.setBounds(148, 90, 192, 42);

getContentPane().add(textField_1);final JLabel label = newJLabel();

label.setText("帐 号");

label.setBounds(76, 102, 66, 18);

getContentPane().add(label);final JLabel label_1 = newJLabel();

label_1.setText("密 码");

label_1.setBounds(76, 167, 66, 18);

getContentPane().add(label_1);final JPasswordField passwordField = newJPasswordField();

passwordField.setBounds(148, 155, 192, 42);

getContentPane().add(passwordField);final JButton button = newJButton();

button.addActionListener(newActionListener() {public void actionPerformed(finalActionEvent e) {if (LoginDialog.this.getHeight() == 301) {

LoginDialog.this.setSize(427, 578);

}else{

LoginDialog.this.setSize(427, 301);

}//设置窗口不断居中

LoginDialog.this.setLocation(Center.getPoint(LoginDialog.this.getSize()));

}

});

button.setText("注册");

button.setBounds(76, 222, 106, 36);

getContentPane().add(button);final JPanel panel = newJPanel();

panel.setLayout(null);

panel.setBorder(new TitledBorder(null, "注册用户", TitledBorder.DEFAULT_JUSTIFICATION,

TitledBorder.DEFAULT_POSITION,null, null));

panel.setBounds(10, 278, 401, 226);

getContentPane().add(panel);final JLabel label_2 = newJLabel();

label_2.setBounds(44, 41, 65, 18);

label_2.setText("手机号:");

panel.add(label_2);

textField= newJTextField();

textField.setBounds(115, 35, 225, 30);

panel.add(textField);final JButton button_2 = newJButton();

button_2.setText("发送验证");

button_2.setBounds(243, 75, 97, 30);

panel.add(button_2);

textField_2= newJTextField();

textField_2.setBounds(115, 104, 95, 30);

panel.add(textField_2);final JLabel label_3 = newJLabel();

label_3.setText("验证码:");

label_3.setBounds(44, 110, 65, 18);

panel.add(label_3);

passwordField_1= newJPasswordField();

passwordField_1.setBounds(115, 143, 231, 30);

panel.add(passwordField_1);

passwordField_2= newJPasswordField();

passwordField_2.setBounds(115, 175, 231, 30);

panel.add(passwordField_2);final JLabel label_4 = newJLabel();

label_4.setText("密 码:");

label_4.setBounds(44, 149, 65, 18);

panel.add(label_4);final JLabel label_5 = newJLabel();

label_5.setText("验证密码:");

label_5.setBounds(44, 181, 65, 18);

panel.add(label_5);final JButton button_3 = newJButton();

button_3.setBounds(47, 510, 97, 30);

getContentPane().add(button_3);

button_3.setText("放弃");final JButton button_4 = newJButton();

button_4.addActionListener(newActionListener() {public void actionPerformed(finalActionEvent e) {

String phone=textField.getText();

String password= null;

String str1=newString(passwordField_1.getPassword()).trim();

String str2=newString(passwordField_2.getPassword()).trim();if(str1.equals(str2)) {

password= newString(passwordField_2.getPassword()).trim();

}else{

System.out.println("输入密码不一致...");

System.exit(0);

}try{

dos.writeUTF(phone);

dos.writeUTF(password);

}catch(IOException e1) {

e1.printStackTrace();

}

}

});

button_4.setBounds(262, 510, 97, 30);

getContentPane().add(button_4);

button_4.setText("注册用户");

connect();this.addWindowListener(newWindowAdapter() {

@Overridepublic voidwindowClosed(WindowEvent e) {

disconnect();

}

});

}public voidconnect() {try{

s= new Socket("127.0.0.1", 8888);

System.out.println("一个客户端登陆中....!");

dos= newDataOutputStream(s.getOutputStream());

dis= newDataInputStream(s.getInputStream());

}catch(ConnectException e) {

System.out.println("服务端异常.........");

System.out.println("请确认服务端是否开启.........");

}catch(IOException e) {

e.printStackTrace();

}

}public voiddisconnect() {try{if (dos != null)

dos.close();if (s != null)

s.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值