📄 logingui.java
字号:
package smoker.client;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import smoker.tools.*;
public class LoginGUI extends JFrame implements ActionListener{
public static final long serialVersionUID = 0;
private JComboBox comboLoginID;
private JPasswordField tfPassword;
private JButton btnLogin, btnExit, btnSet;
private JComboBox combIP;
private JTextField tfPort;
private JPanel configPanel, loginPanel;
private Container container;
private IClientCenter iclientCenter;
private Vector<String> jICCList;
private JButton btnClear;
public LoginGUI(IClientCenter iclientCenter) {
jICCList = new Vector<String>();
this.iclientCenter = iclientCenter;
this.setTitle("JICC 登陆器");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUIManager.CenterWindow(this);
init();
}
private void init() {
try {
this.setCursor(Cursor.getSystemCustomCursor("kamet.cur"));
} catch (Exception e) {
e.printStackTrace();
}
btnClear = new JButton("清除");
btnClear.addActionListener(this);
btnClear.setActionCommand("Clear");
container = (JPanel) getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
configPanel = new JPanel();
configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.Y_AXIS));
configPanel.setBorder(BorderFactory.createTitledBorder("服务器配置"));
comboLoginID = new JComboBox(iclientCenter.getJICC());
comboLoginID.setEditable(true);
tfPassword = new JPasswordField("");
tfPassword.setEchoChar('*');
btnLogin = GUIManager.createJButton("登录", null, this, "btnLogin");
btnExit = GUIManager.createJButton("退出", null, this, "btnExit");
btnSet = GUIManager.createJButton("设置↓", null, this, "btnSet");
combIP = new JComboBox();
combIP.addItem(iclientCenter.getIP());
combIP.setEditable(true);
tfPort = GUIManager.createJTextField("7579");
getLoginPanel();
this.getRootPane().setDefaultButton(btnLogin);
}
private void getLoginPanel() {
JPanel tempPanel = new JPanel();
tempPanel.setBorder(BorderFactory.createTitledBorder("输入登录信息"));
tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.Y_AXIS));
JPanel idPanel = new JPanel();
idPanel.setLayout(new BoxLayout(idPanel, BoxLayout.X_AXIS));
JLabel labLoginID = new JLabel("帐号:");
idPanel.add(createWidthArea(25));
idPanel.add(labLoginID);
idPanel.add(createWidthArea(15));
idPanel.add(comboLoginID);
idPanel.add(createWidthArea(5));
idPanel.add(btnClear);
JPanel pwdPanel = new JPanel();
pwdPanel.setLayout(new BoxLayout(pwdPanel, BoxLayout.X_AXIS));
JLabel labPwd = new JLabel("口令:");
pwdPanel.add(createWidthArea(25));
pwdPanel.add(labPwd);
pwdPanel.add(createWidthArea(15));
pwdPanel.add(tfPassword);
pwdPanel.add(createWidthArea(65));
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
btnPanel.add(createWidthArea(20));
btnPanel.add(btnSet);
btnPanel.add(createWidthArea(20));
btnPanel.add(btnLogin);
btnPanel.add(createWidthArea(20));
btnPanel.add(btnExit);
btnPanel.add(createWidthArea(20));
Border bor = BorderFactory.createLineBorder(Color.green);
TitledBorder loginTitle = BorderFactory.createTitledBorder(bor, "正在登录",
TitledBorder.CENTER, TitledBorder.BELOW_TOP);
loginPanel = new JPanel();
loginPanel.setVisible(false);
loginPanel.setBorder(loginTitle);
loginPanel.setLayout(new BorderLayout());
JLabel labLogining = new JLabel(Files.LOGINNING_ICON);
loginPanel.add(labLogining);
JPanel ipPanel = new JPanel();
ipPanel.setLayout(new BoxLayout(ipPanel, BoxLayout.X_AXIS));
JLabel labIP = new JLabel("服务器地址:");
ipPanel.add(createWidthArea(45));
ipPanel.add(labIP);
ipPanel.add(createWidthArea(10));
ipPanel.add(combIP);
ipPanel.add(createWidthArea(50));
JPanel portPanel = new JPanel();
portPanel.setLayout(new BoxLayout(portPanel, BoxLayout.X_AXIS));
JLabel labPort = new JLabel("端口号:");
portPanel.add(createWidthArea(70));
portPanel.add(labPort);
portPanel.add(createWidthArea(10));
portPanel.add(tfPort);
portPanel.add(createWidthArea(45));
configPanel.add(createHeightArea(20));
configPanel.add(ipPanel);
configPanel.add(createHeightArea(20));
configPanel.add(portPanel);
configPanel.add(createHeightArea(30));
tempPanel.add(createHeightArea(10));
tempPanel.add(idPanel);
tempPanel.add(createHeightArea(20));
tempPanel.add(pwdPanel);
tempPanel.add(createHeightArea(15));
container.add(tempPanel);
container.add(loginPanel);
container.add(createHeightArea(15));
container.add(btnPanel);
container.add(createHeightArea(20));
}
private Component createWidthArea(int width) {
return Box.createHorizontalStrut(width);
}
private Component createHeightArea(int height) {
return Box.createVerticalStrut(height);
}
public void actionPerformed(ActionEvent e) {
String src = e.getActionCommand();
if (src.equals("btnSet")) {
showAndConfigInfo();
} else if (src.equals("btnLogin")) {
if (!dataVlidate()) {
setState(false);
return;
}
if (btnSet.getText().equals("设置↓")) {
this.setSize(300, 200);
} else {
this.setSize(300, 400);
}
this.validate();
setState(true);
startLogin();
} else if(src.equals("Clear")){
iclientCenter.clearInfo();
comboLoginID.removeAllItems();
}else{
System.exit(0);
}
}
private void startLogin() {
setServerInfo();
if (!iclientCenter.connServer()) {
setState(false);
if (btnSet.getText().equals("设置↓")) {
this.setSize(300, 200);
} else {
this.setSize(300, 355);
}
this.validate();
Msg.show("服务器不存在 或 没有开启!\n请检查您的服务器设置!");
return;
}
String loginName = comboLoginID.getSelectedItem().toString().trim();
String password = new String(tfPassword.getPassword());
iclientCenter.login(loginName, password);
addJICC();
iclientCenter.saveIP(combIP.getSelectedItem().toString());
iclientCenter.saveJICC(jICCList);
}
private void addJICC() {
for(int i = 0; i<iclientCenter.getJICC().length; i++) {
if(!jICCList.contains(iclientCenter.getJICC()[i])) {
jICCList.add(iclientCenter.getJICC()[i]);
}
}
String jicc = comboLoginID.getSelectedItem().toString();
if(!jICCList.contains(jicc)) {
jICCList.add(jicc);
}
}
public void onLoginResult(boolean flag) {
if (flag) {
this.dispose();
} else {
setState(false);
if (btnSet.getText().equals("设置↓")) {
this.setSize(300, 200);
} else {
this.setSize(300, 355);
}
this.validate();
Msg.show("登录名 或 密码 输入错误!\n请确认后再试。");
}
}
public void theUserIsExist() {
Msg.show("登录失败:\n该JICC已在别处登录,无法再次登录!");
setState(false);
if (btnSet.getText().equals("设置↓")) {
this.setSize(300, 200);
} else {
this.setSize(300, 355);
}
this.validate();
}
private void setState(boolean flag) {
loginPanel.setVisible(flag);
btnLogin.setEnabled(!flag);
btnSet.setEnabled(!flag);
comboLoginID.setEnabled(!flag);
combIP.setEnabled(!flag);
tfPort.setEditable(!flag);
tfPassword.setEditable(!flag);
}
private void showAndConfigInfo() {
if (btnSet.getText().equals("设置↓")) {
btnSet.setText("设置↑");
this.setSize(300, 355);
container.add(configPanel);
} else {
btnSet.setText("设置↓");
this.setSize(300, 200);
container.remove(configPanel);
}
this.validate();
}
private boolean dataVlidate() {
Object loginID = comboLoginID.getSelectedItem();
String password = new String(tfPassword.getPassword());
Object serverIp = combIP.getSelectedItem();
String serverPort = tfPort.getText().trim();
if (loginID == null) {
Msg.show("请输入登录名!");
return false;
} else if (password.length() == 0) {
Msg.show("请输入密码!");
return false;
} else if (serverIp == null) {
Msg.show("请输入服务器地址!");
return false;
} else if (serverPort.length() == 0) {
Msg.show("请输入服务器端口号!");
return false;
} else if (password.length() < 6) {
Msg.show("密码长度不能小于 6 位!");
return false;
} else if (!DataManager.isLegitimacyIP(serverIp.toString().trim())) {
Msg.show("输入的IP地址不合法!");
return false;
} else if (!DataManager.isNumeric(serverPort)) {
Msg.show("端口号应为纯数字!");
return false;
} else if (Long.parseLong(serverPort) < 1025
|| Long.parseLong(serverPort) > 65535) {
Msg.show("端口号范围应在:1025 - 65535 之间!");
return false;
} else {
return true;
}
}
private void setServerInfo() {
String serverIp = combIP.getSelectedItem().toString().trim();
String serverPort = tfPort.getText().trim();
iclientCenter.setServerInfo(serverIp, Integer.parseInt(serverPort));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -