package User;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class UserClient extends JFrame implements ActionListener,KeyListener{
JLabel label, JLname, JLpassword, JLIP;
JTextField Username,JLIPtext;
JPasswordField Password;
JButton login, cancel;
Container container;
SocketServer a = null;
static UserClient userclient = null;
String IP = null;
public UserClient() {
super("User-Login...");
setBounds(500, 300, 320, 270);
setResizable(false);
setAlwaysOnTop(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (a != null) {
try {
a.closeAll();
a.stop();
} catch (IOException e1) {
e1.printStackTrace();
}
}
quit();
}
});
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public void setLayout() {
container = this.getContentPane();
container.setLayout(null);
label = new JLabel("用户登录窗口");
label.setBounds(80, 10, 180, 30);
label.setFont(new Font(null, Font.BOLD, 25));
label.setForeground(Color.red);
container.add(label);
JLname = new JLabel("用户名:");
JLname.setBounds(35, 50, 100, 30);
JLname.setFont(new Font(null, Font.BOLD, 18));
container.add(JLname);
Username = new JTextField();
Username.setBounds(108, 50, 150, 30);
Username.addKeyListener(this);
container.add(Username);
JLpassword = new JLabel("密码:");
JLpassword.setBounds(55, 97, 80, 30);
JLpassword.setFont(new Font(null, Font.BOLD, 18));
container.add(JLpassword);
Password = new JPasswordField();
Password.setBounds(108, 97, 150, 30);
Password.addKeyListener(this);
Password.setEchoChar('☆');
container.add(Password);
JLIP=new JLabel("对方IP:");
JLIP.setFont(new Font(null, Font.BOLD, 16));
JLIP.setBounds(45, 144, 150, 30);
container.add(JLIP);
JLIPtext=new JTextField();
JLIPtext.setBounds(108, 144, 150, 30);
JLIPtext.addKeyListener(this);
container.add(JLIPtext);
login = new JButton("登录");
login.setMnemonic('\n');
login.setBounds(155, 190, 100, 30);
login.addActionListener(this);
container.add(login);
cancel = new JButton("取消");
cancel.setBounds(40, 190, 100, 30);
container.add(cancel);
}
public static void main(String[] args) {
userclient = new UserClient();
userclient.setLayout();
userclient.show();
}
public void quit() {
System.exit(0);
}
public void actionPerformed(ActionEvent e) {
panduan();
}
public void panduan() {
String name = Username.getText();
String password = Password.getText();
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "用户名不能为空", "警告",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) == ' ') {
JOptionPane.showMessageDialog(this, "用户名不能包含空格", "警告",
JOptionPane.ERROR_MESSAGE);
return;
}
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不能为空", "警告",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) == ' ') {
JOptionPane.showMessageDialog(this, "密码不能包含空格", "警告",
JOptionPane.ERROR_MESSAGE);
return;
}
}
if(JLIPtext.getText().equals("")){
JOptionPane.showMessageDialog(this, "IP地址不能为空", "警告",
JOptionPane.ERROR_MESSAGE);
return;
}
try {
a = new SocketServer(userclient, Username.getText(), JLIPtext.getText());
} catch (UnknownHostException e1) {
JOptionPane.showMessageDialog(this, "IP错误,请输入正确的IP地址", "警告",
JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(this, "服务器处于关闭状态", "警告",
JOptionPane.ERROR_MESSAGE);
}
}
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
switch(keyCode){
case KeyEvent.VK_ENTER:
if(e.getSource()==Username){
Password.requestFocus();
}else if(e.getSource()==Password){
JLIPtext.requestFocus();
}else if(e.getSource()==JLIPtext){
panduan();
}
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}