📄 logindialog.java
字号:
package chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* <p>Title: 登录对话框</p>
* <p>Description:实现用户登陆 </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class LoginDialog extends JFrame implements ActionListener, Runnable {
Constant constant;
//static JFrame jframe;
JPanel jpanel;
JPanel loginInfoPanel;
JPanel loginPanel;
JPanel buttonPanel;
JPanel registerPanel;
JButton loginButton;
JButton cancelButton;
JButton submitButton;
JButton registerButton;
JTextField userIdTextField;
JTextField nameTextField;
JTextField emailTextField;
JPasswordField pdField;
JPasswordField pdCheckField;
JLabel userIdLabel;
JLabel pdLabel;
JLabel pdCheckLabel;
JLabel nameLabel;
JLabel emailLabel;
JLabel infoLabel;
static boolean isEnableChat=false;//判断是否可以聊天
String userId=null;//用户名
String pd=null;//登陆密码
String pdCheck=null;
String name=null;
String email=null;
Socket socket;
Thread thread=null;
DataInputStream in=null;
DataOutputStream out=null;
String toolbarImagePath = "/images/button/";
ImageIcon loginIcon;
ImageIcon loginOutIcon;
ImageIcon submitButtonIcon;
Font font=new Font("新宋体",Font.PLAIN,12);
String registerButtonText="<html><u><font size=4 style=font-family:宋体,background:#8CADD7>注册</font></u></html>";
OvalBorder ovalBorder=new OvalBorder(5,5,new Color(0,153,255),new Color(0,153,255));
public LoginDialog() {
constant=new Constant();
UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);
//super(f);
loginIcon=new ImageIcon(getClass().getResource(toolbarImagePath +
"login.png"));
loginOutIcon=new ImageIcon(getClass().getResource(toolbarImagePath +
"loginout.png"));
submitButtonIcon=new ImageIcon(getClass().getResource(toolbarImagePath +
"submit.png"));
jpanel=new JPanel();
jpanel.setBackground(new Color(190,212,255));
buttonPanel=new JPanel();
buttonPanel.setBackground(new Color(190,212,255));
loginPanel=new JPanel();
registerPanel=new JPanel();
registerPanel.setBackground(new Color(190,212,255));
loginInfoPanel=new JPanel();
loginInfoPanel.setBackground(new Color(190,212,255));
loginButton=new JButton(loginIcon);
loginButton.setContentAreaFilled(false);
loginButton.setContentAreaFilled(false);
loginButton.setRequestFocusEnabled(false);
loginButton.setBorder(null);
cancelButton=new JButton(loginOutIcon);
cancelButton.setContentAreaFilled(false);
cancelButton.setRequestFocusEnabled(false);
cancelButton.setBorder(null);
registerButton=new JButton(registerButtonText);
registerButton.setBorder(null);
registerButton.setContentAreaFilled(false);
registerButton.setBackground(new Color(190,212,255));
submitButton=new JButton(submitButtonIcon);
submitButton.setPreferredSize((new Dimension(50,30)));
//cancelButton.setPreferredSize(new Dimension(30,25));
//cancelButton.setBackground(Color.BLUE);
userIdTextField=new JTextField(10);
userIdTextField.setBorder(ovalBorder);
userIdTextField.setText(null);
//System.out.println(nameTextField.getText().trim());
nameTextField=new JTextField(10);
nameTextField.setBorder(ovalBorder);
nameTextField.setText(null);
pdField=new JPasswordField(10);
pdField.setBorder(ovalBorder);
pdField.setText(null);
emailTextField=new JTextField(10);
emailTextField.setBorder(ovalBorder);
emailTextField.setText(null);
pdCheckField=new JPasswordField(10);
pdCheckField.setBorder(ovalBorder);
pdCheckField.setText(null);
userIdLabel=new JLabel(constant.getLang("LD_userIdLabel"),JLabel.CENTER);
userIdLabel.setFont(font);
userIdLabel.setForeground(new Color(0,153,255));
pdLabel=new JLabel(constant.getLang("LD_pdLabel"),JLabel.CENTER);
pdLabel.setFont(font);
pdLabel.setForeground(new Color(0,153,255));
pdCheckLabel=new JLabel(constant.getLang("LD_pdCheckLabel"),JLabel.CENTER);
pdCheckLabel.setFont(font);
pdCheckLabel.setForeground(new Color(0,153,255));
nameLabel=new JLabel(constant.getLang("LD_nameLabel"),JLabel.CENTER);
nameLabel.setFont(font);
nameLabel.setForeground(new Color(0,153,255));
emailLabel=new JLabel(constant.getLang("LD_emailLabel"),JLabel.CENTER);
emailLabel.setFont(font);
emailLabel.setForeground(new Color(0,153,255));
infoLabel=new JLabel(constant.getLang("LD_infoLabel"),JLabel.LEFT);
infoLabel.setForeground(new Color(0,153,255));
infoLabel.setBackground(new Color(190,212,255));
//this.setBackground(new Color(190,212,255));
infoLabel.setFont(font);
setSize(250,160);
this.setResizable(false);
this.setTitle(constant.getLang("LD_dldhk"));
Container con=getContentPane();
// con.setLayout(new BorderLayout());
//jpanel.setLayout(new GridLayout(3,2));
loginInfoPanel.setLayout(new GridLayout(2,2));
loginInfoPanel.add(userIdLabel);
loginInfoPanel.add(userIdTextField);
loginInfoPanel.add(pdLabel);
loginInfoPanel.add(pdField);
//jpanel.add(userIdLabel);
//jpanel.add(userIdTextField);
//jpanel.add(pdLabel);
//jpanel.add(pdLabel);
//jpanel.add(pdField);
jpanel.add(loginInfoPanel);
jpanel.add(registerPanel);
jpanel.add(buttonPanel);
registerPanel.setLayout(new GridLayout(3,2));
//registerPanel.setLayout(new FlowLayout());
registerPanel.add(pdCheckLabel);
registerPanel.add(pdCheckField);
//registerPanel.add(new JLabel());
registerPanel.add(nameLabel);
registerPanel.add(nameTextField);
// registerPanel.add(new JLabel());
registerPanel.add(emailLabel);
registerPanel.add(emailTextField);
// registerPanel.add(new JLabel());
registerPanel.setVisible(false);
//buttonPanel.setLayout(new GridLayout(1,3));
buttonPanel.add(loginButton);
buttonPanel.add(cancelButton);
buttonPanel.add(registerButton);
// jpanel.add(loginButton);
// jpanel.add(cancelButton);
//jpanel.add(registerButton);
//jpanel.setBackground(Color.white);
loginPanel.setLayout(new BorderLayout());
loginPanel.add(jpanel,"Center");
loginPanel.add(infoLabel,"South");
//con.add(jpanel,"North");
//con.add(infoLabel,"South");
con.add(loginPanel);
pdField.addActionListener(this);
loginButton.addActionListener(this);
cancelButton.addActionListener(this);
registerButton.addActionListener(this);
submitButton.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//System.exit(0);
dispose();
}});
thread=new Thread(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==loginButton||e.getSource()==pdField){//登陆按钮事件和回车事件
this.setName(userIdTextField.getText().trim());//取得用户名
this.setPd(String.valueOf(pdField.getPassword()));//取得密码
this.userIdTextField.setText("");
this.pdField.setText("");
this.infoLabel.setText("");
//System.out.println((String.valueOf(pdField.getPassword())));
if (userId.length() != 0 && pd.length() != 0) {//判断用户名和密码均输入
if (getEnableChat()) {
this.infoLabel.setText(constant.getLang("LD_dqyhyjzlt"));
}
else {
//this.infoLabel.setText("");
if (socket != null) {
try {
out.writeUTF("LOGIN_NAME:" + userId+ "LOGIN_PD:" + pd);//提交给服务器
//this.loginButton.setEnabled(false);
}
catch (IOException ioe) {
this.infoLabel.setText(constant.getLang("LD_wlcscw"));
}
}
}
}
else {
this.infoLabel.setText(constant.getLang("LD_qsryhmmm"));
}
}
else if(e.getSource()==cancelButton){
System.exit(0);
}
else if(e.getSource()==registerButton){//注册按钮事件
//loginPanel.remove(jpanel);
registerPanel.setVisible(true);
//loginButton.setVisible(false);
//cancelButton.setVisible(false);
//registerButton.setVisible(false);
buttonPanel.remove(loginButton);
buttonPanel.remove(cancelButton);
buttonPanel.remove(registerButton);
//buttonPanel.setLayout(null);
buttonPanel.add(submitButton);
//registerButton.setVisible(false);
LoginDialog.this.setSize(250,230);
LoginDialog.this.repaint();
LoginDialog.this.validate();
}
else if(e.getSource()==submitButton){//提交按钮事件
this.setName(userIdTextField.getText().trim());
this.setPd(String.valueOf(pdField.getPassword()));
this.setPdCheck(String.valueOf(pdCheckField.getPassword()));
this.setTrueName(nameTextField.getText().trim());
this.setEamil(emailTextField.getText().trim());
if(userId.length()>0&pd.length()>0&pdCheck.length()>0&name.length()>0&email.length()>0){
if (! (pd.equalsIgnoreCase(pdCheck))) {
JOptionPane.showMessageDialog(this, constant.getLang("LD_mmbyy"), constant.getLang("LD_cwxxdhk"),
JOptionPane.WARNING_MESSAGE);
}
else if (email.indexOf("@") == -1) {
JOptionPane.showMessageDialog(this, constant.getLang("LD_egscw"), constant.getLang("LD_cwxxdhk"),
JOptionPane.WARNING_MESSAGE);
}
else {
if (socket != null) {
try {
out.writeUTF("REGISTER_NAME:" + userId + "#REGISTER_PD#" + pd+"#REGISTER_NAME#"+name+"#REGISTER_EMAIL#"+email);
//this.loginButton.setEnabled(false);
}
catch (IOException ioe) {
this.infoLabel.setText(constant.getLang("LD_wlcscw"));
}
}
}
}else{
JOptionPane.showMessageDialog(this,constant.getLang("LD_qrwqsr"),constant.getLang("LD_cwxxdhk"),JOptionPane.WARNING_MESSAGE);
}
}
}
public String getName(){
return userId;
}
public void setName(String userId){
this.userId=userId;
}
public void setPd(String pd){
this.pd=pd;
}
public void setPdCheck(String pdCheck){
this.pdCheck=pdCheck;
}
public void setTrueName(String name){
this.name=name;
}
public void setEamil(String email){
this.email=email;
}
public void setEnableChat(boolean enableChat){
this.isEnableChat=enableChat;
}
static public boolean getEnableChat(){
return isEnableChat;
}
/**
* 取得和服务器的连接
* @param socket Socket
* @param in DataInputStream
* @param out DataOutputStream
*/
public void setConnection(Socket socket,DataInputStream in,DataOutputStream out){
this.socket=socket;
this.in=in;
this.out=out;
//System.out.println("this socket"+this.socket.getLocalPort());
try{
thread.start();
}catch(Exception e){
//e.printStackTrace();
}
}
public void run(){
String message=null;
while(true){
if(in!=null){
try {
message = in.readUTF();
}
catch (IOException e) {
this.infoLabel.setText(constant.getLang("LD_wlcscw"));
}
}
try{
if (message.length() > 0) {
if (message.startsWith("LOGIN_SUCCEED")) {//登陆成功
this.setEnableChat(true);
this.setVisible(false);
break;
}
else if (message.startsWith("LOGIN_ERROR")) {//验证错误
this.infoLabel.setText(constant.getLang("LD_dlcw"));
}
else if(message.startsWith("REGISTER_SUCCEED:")){//注册成功
JOptionPane.showMessageDialog(this,constant.getLang("LD_yhdlcg"),constant.getLang("LD_xxtsk"),JOptionPane.PLAIN_MESSAGE);
registerPanel.setVisible(false);
pdField.setText("");
pdCheckField.setText("");
buttonPanel.add(loginButton);
buttonPanel.add(cancelButton);
buttonPanel.add(registerButton);
buttonPanel.remove(submitButton);
LoginDialog.this.setSize(250, 160);
LoginDialog.this.validate();
}
else if(message.startsWith("REGISTER_ERROR_EXIST")){//用户名存在
JOptionPane.showMessageDialog(this,constant.getLang("LD_yhcz"),constant.getLang("LD_xxtsk"),JOptionPane.ERROR_MESSAGE);
pdField.setText("");
pdCheckField.setText("");
}
else if(message.startsWith("REGISTER_ERROR")){//连接错误
JOptionPane.showMessageDialog(this,constant.getLang("LD_yhzccw "),constant.getLang("LD_xxtsk"),JOptionPane.ERROR_MESSAGE);
}
else if (message.startsWith("LOGIN_PEOPLE:")) {
String peopleName = message.substring(message.indexOf(":") + 1);
//System.out.println(peopleName + "login");
}
}
}catch(Exception e){
}
try{
Thread.sleep(100);
}catch(Exception e){
}
}
}
public static void main(String[] args) {
// JFrame f=new JFrame();
LoginDialog loginDialog = new LoginDialog();
loginDialog.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -