?? logindialog.java~127~
字號:
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);
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 + -