📄 logindialog.java~2~
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//import java.lang.Thread;
public class LoginDialog extends JDialog implements ActionListener, Runnable {
static JFrame jframe;
JPanel jpanel;
JButton loginButton;
JButton cancelButton;
JTextField nameTextField;
JPasswordField pdField;
JLabel nameLabel;
JLabel pdLabel;
JLabel infoLabel;
boolean isEnableChat=false;//判断是否可以聊天
String name;//用户名
String pd;//登陆密码
Socket socket;
Thread thread=null;
DataInputStream in;
DataOutputStream out;
public LoginDialog() {
super(jframe);
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);
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);
}});*/
thread=new Thread(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==loginButton){
if((nameTextField.getText().trim())==null){//&(pdField.getPassword().toString().trim())!=null){
// System.out.println(nameTextField.getText().trim());
this.loginButton.setEnabled(false);
this.setName(nameTextField.getText().trim());
this.setPd(pdField.getPassword().toString().trim());
try {
out.writeUTF("LOGIN_NAME:" + name + "LOGIN_PD:" + pd);
}
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;
}
public boolean enableChat(){
return isEnableChat;
}
public void setConnection(Socket socket,DataInputStream in,DataOutputStream out){
this.socket=socket;
this.in=in;
this.out=out;
try{
//thread.start();
}catch(Exception e){
}
}
public void run(){
String message=null;
while(true){
if(in!=null){
try {
message = in.readUTF();
}
catch (IOException e) {
this.infoLabel.setText("网络传输发生错误!");
}
}
if(message.startsWith("LOGIN_SUCCED")){
this.setEnableChat(true);
this.setVisible(false);
}
else if(message.startsWith("LOGIN_ERROR")){
this.infoLabel.setText("登陆错误!");
this.loginButton.setEnabled(true);
}
}
}
public static void main(String[] args) {
LoginDialog loginDialog = new LoginDialog();
loginDialog.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -