📄 serverlogindiag.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ServerLoginDiag extends JDialog{
private ServerFrame serverframe;
private Server server;
private boolean Advance=false; //"高级"(true)或者普通(false)
private boolean Register=false; //登录(fasle)或者注册(true)
//开始用按钮上显示的文字"Advanced","Normal","Reg","Log in"来判断当前窗口状态
//这个就局限了按钮上的文字只能是以上几个,若改动,则代码的其他部分也要修改
//容易出错,而且降低了通用性
private UserInfo myself;
private String IPofServer;
private int port;
private boolean connected=false; //为了减少开销,在正确已经连接到服务器而仅因为用户基本信息填写错误
//造成的登录错误不必重新连接,重新输入再登录。暂时没用到。
private String RegName; //注册名--显示的是用户名
private String UserName; //真实姓名
private String Password; //密码
private boolean Sex; //性别
private String Job; //职位
/****************************************************以下组件****************************************************/
private Container contentPane; //顶层容器
private JLabel LabRegName; //注册名--显示的是用户名
private JLabel LabUserName; //真实姓名
private JLabel LabPassword; //密码
private JLabel LabPasswordAgain; //密码确认,注册时用
private JLabel LabIPAddr; //加边框
private JLabel LabPort; //加边框
private JLabel LabSex; //性别
private JLabel LabJob; //职位
private JTextField RegNameText;
private JTextField UserNameText;
private JPasswordField PasswordText;
private JPasswordField PasswordAgainText;
private JTextField IPAddrText;
private JTextField PortText;
private JTextField JobText;
private JCheckBox register; //标识"注册"和"登录"的复选按钮
private JRadioButton male;
private JRadioButton female;
private ButtonGroup GroupSex;
private JButton OKBtn; //"登陆"或"注册"按钮
private JButton CancelBtn; //"取消"按钮
private JButton AdvBtn; //"高级"/"普通"交替按钮
/****************************************************以上组件****************************************************/
public ServerLoginDiag(Server server,ServerFrame serverframe){
this();
this.serverframe=serverframe;
this.server=server;
}//public ServerLoginDiag(Server server)
public ServerLoginDiag(){
//创建容器
contentPane=this.getContentPane();
contentPane.setLayout(new GridBagLayout());
setTitle("Log in");
setModal(false); //不挂起其他窗口
LabRegName=new JLabel("RegName"); //注册名
LabUserName=new JLabel("True Name"); //真实姓名
LabPassword=new JLabel("Password"); //密码
LabPasswordAgain=new JLabel("PasswordAgain"); //密码确认,注册时用
LabIPAddr=new JLabel("ServerIP"); //加边框
LabPort=new JLabel("ServerPort:"); //加边框
LabSex=new JLabel("Sex"); //性别
LabJob=new JLabel("Job"); //职位
RegNameText=new JTextField();
//RegNameText.setColumns(15);
UserNameText=new JTextField();
//UserNameText.setColumns(15);
PasswordText=new JPasswordField();
//PasswordText.setColumns(15);
PasswordAgainText=new JPasswordField();
//PasswordAgainText.setColumns(15);
IPAddrText=new JTextField();
//IPAddrText.setColumns(15);
PortText=new JTextField();
//PortText.setColumns(15);
JobText=new JTextField("职员");
//JobText.setColumns(15);
male=new JRadioButton("male",true);
female=new JRadioButton("female",false);
GroupSex=new ButtonGroup();
GroupSex.add(male);
GroupSex.add(female);
register=new JCheckBox("Reg",false); //"登陆"与"注册",复选按钮
register.setVisible(false);
OKBtn=new JButton("Log in"); //"登陆"与"注册"按钮
CancelBtn=new JButton("Cancel");
DefaultLayout();
setSize(400,400);
setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}//public void windowClosing(WindowEvent e)
});//addWindowListener(new WindowAdapter(){
OKBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
RegName=RegNameText.getText().trim();
UserName=UserNameText.getText().trim();
Password=PasswordText.getText().trim();
if(!(Password.equals(ServerLoginDiag.this.server.getSerAdminPassWord())
&& RegName.equals(ServerLoginDiag.this.server.getSerAdminName()))){
//ServerLoginDiag.this.server.getSerAdminPassWord().equals(Password)
JOptionPane.showMessageDialog(ServerLoginDiag.this,
"UserName or password error!",
"Error",
JOptionPane.ERROR_MESSAGE);
}else{
serverframe.setVisible(true);
ServerLoginDiag.this.dispose();
}
}//public void actionPerformed(ActionEvent e)
});//OKBtn.addActionListener(new ActionListener()
CancelBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// 居中设置
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}//public ServerLoginDiag()
public void DefaultLayout(){
//clear();
LayoutUtil.add(contentPane,LabRegName,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,0,0,1,1);
LayoutUtil.add(contentPane,LabPassword,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,0,1,1,1);
LayoutUtil.add(contentPane,RegNameText,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,1,1,0,1,1);
LayoutUtil.add(contentPane,PasswordText,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,1,1,1,1,1);
LayoutUtil.add(contentPane,OKBtn,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,0,2,1,1);
LayoutUtil.add(contentPane,register,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,1,2,1,1);
LayoutUtil.add(contentPane,CancelBtn,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,2,2,1,1);
//LayoutUtil.add(contentPane,AdvBtn,GridBagConstraints.NONE,GridBagConstraints.CENTER,1,1,3,2,1,1);
}//public void DefaultLayout()
/*********************下面方法用于调试**************************/
public static void main(String args[]){
new ServerLoginDiag().setVisible(true);
}
}//class ServerLoginDiag
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -