📄 logindialog.java~134~
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class LoginDialog extends JFrame implements ActionListener, Runnable {
//static JFrame jframe;
JPanel jpanel;
JButton loginButton;
JButton cancelButton;
JTextField nameTextField;
JPasswordField pdField;
JLabel nameLabel;
JLabel pdLabel;
JLabel infoLabel;
static boolean isEnableChat=false;//判断是否可以聊天
String name=null;//用户名
String pd=null;//登陆密码
Socket socket;
Thread thread=null;
DataInputStream in=null;
DataOutputStream out=null;
public LoginDialog() {
//super(f);
jpanel=new JPanel();
loginButton=new JButton("登陆");
cancelButton=new JButton("退出");
nameTextField=new JTextField(10);
nameTextField.setText(null);
//System.out.println(nameTextField.getText().trim());
pdField=new JPasswordField(10);
pdField.setText(null);
nameTextField.setBackground(new Color(140,173,215));
pdField.setBackground(new Color(140,173,215));
nameLabel=new JLabel("用户名",JLabel.CENTER);
pdLabel=new JLabel("密码",JLabel.CENTER);
infoLabel=new JLabel("信息",JLabel.LEFT);
setSize(200,140);
this.setResizable(false);
this.setTitle("登陆对话框");
Container con=getContentPane();
con.setLayout(new BorderLayout());
jpanel.setLayout(new GridLayout(3,2));
jpanel.add(nameLabel);
jpanel.add(nameTextField);
jpanel.add(pdLabel);
jpanel.add(pdLabel);
jpanel.add(pdField);
jpanel.add(loginButton);
jpanel.add(cancelButton);
con.add(jpanel,"North");
con.add(infoLabel,"South");
loginButton.addActionListener(this);
cancelButton.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){
this.setName(nameTextField.getText().trim());
this.setPd(String.valueOf(pdField.getPassword()));
this.nameTextField.setText("");
this.pdField.setText("");
this.infoLabel.setText("");
//System.out.println((String.valueOf(pdField.getPassword())));
if (name.length() != 0 && pd.length() != 0) {
if (getEnableChat()) {
this.infoLabel.setText("当前用户已经在聊天,请先退出");
}
else {
//this.infoLabel.setText("");
if (socket != null) {
try {
out.writeUTF("LOGIN_NAME:" + name + "LOGIN_PD:" + pd);
//this.loginButton.setEnabled(false);
}
catch (IOException ioe) {
this.infoLabel.setText("网络传输错误!");
}
}
}
}
else {
this.infoLabel.setText("请输入用户名和密码!");
}
}
else if(e.getSource()==cancelButton){
System.exit(0);
}
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public void setPd(String pd){
this.pd=pd;
}
public void setEnableChat(boolean enableChat){
this.isEnableChat=enableChat;
}
static public boolean getEnableChat(){
return isEnableChat;
}
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("网络传输发生错误!");
}
}
try{
if (message.length() > 0) {
if (message.startsWith("LOGIN_SUCCEED")) {
this.setEnableChat(true);
this.setVisible(false);
break;
}
else if (message.startsWith("LOGIN_ERROR")) {
//this.loginButton.setEnabled(true);
//this.loginButton.repaint();
this.infoLabel.setText("登陆错误!");
}
else if (message.startsWith("LOGIN_PEOPLE:")) {
//System.out.println("login");
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 + -