📄 administratorgui.java
字号:
//started from ServerControlGUI to deal with adm
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
public class AdministratorGUI extends WindowAdapter implements Runnable,ActionListener{
private static Administrator currentAdm;
private JFrame frame;
private JLabel admNumLabel,pwLabel,statusLabel;
private JFormattedTextField idField;
private JPasswordField pwField;
private JButton pwButton;
private final static String ADD="add";
//private final static String CONFIRM="confirm";
private final static String DEL="del";
private final static String REPW="repassword";
private final static String OK="ok";
private final static String EXIT="exit";
private static String pw1,pw2;
private boolean repassword=false;
public AdministratorGUI(Administrator adm){
currentAdm=adm;
}
public void run(){
frame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("Administrator Management");
frame.addWindowListener(this);
frame.setContentPane(makeMainPane());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String type=ae.getActionCommand();
//System.out.println(type);
if(type.equals(ADD)){
statusLabel.setText("");
pwButton.setEnabled(true);
pwField.setEnabled(true);
pwLabel.setText("Input a password:");
pwLabel.setForeground(Color.RED);
}
else if(type.equals(OK)){
if(repassword){ //want to repassword
if(pw1==null){ //if enter firstly
pw1=new String(pwField.getPassword());
pwLabel.setText("INPUT AGAIN:");
pwField.setText("");
pwField.requestFocusInWindow();
return;
}
else{ //enter secondly
pw2=new String(pwField.getPassword());
if(pw1.equals(pw2)){
Server.getServerDatabase().findAdm(currentAdm.getID()).setPassword(pw1);
JOptionPane.showMessageDialog(frame,"ID:"+currentAdm.getID()+
" Password has been changed! Please secure it!",
"Change Successfull",JOptionPane.INFORMATION_MESSAGE);
}
else{ //passwords aren't equal
JOptionPane.showMessageDialog(frame,"Two passwords doesn't equal!",
"Change failed",JOptionPane.ERROR_MESSAGE);
}
pwLabel.setText("");
pwField.setText("");
pwField.setEnabled(false);
pwButton.setEnabled(false);
statusLabel.setText("");
pw1=pw2=null;
repassword=false;
return;
}
}
//want to add new adm
if(pw1==null){ //if enter firstly
pw1=new String(pwField.getPassword());
pwLabel.setText("INPUT AGAIN:");
pwField.setText("");
pwField.requestFocusInWindow();
}
else{ //enter secondly
pw2=new String(pwField.getPassword());
if(pw1.equals(pw2)){ //if passwords equal
//System.out.println("id"+id);
Server.getServerDatabase().addAdm(new Administrator(pw1));
int id=Server.getServerDatabase().getNewAdmID();
JOptionPane.showMessageDialog(frame,"New administrator added. ID:"+
Server.getServerDatabase().getNewAdmID()+
" Please secure your password.",
"Add Successful",JOptionPane.INFORMATION_MESSAGE);
}
else{ //passwords aren't equal
JOptionPane.showMessageDialog(frame,"Two passwords doesn't equal!",
"Add failed",JOptionPane.ERROR_MESSAGE);
}
pwLabel.setText("");
pwField.setText("");
pwField.setEnabled(false);
pwButton.setEnabled(false);
statusLabel.setText("");
resetNumber();
pw1=pw2=null;
}
}
else if(type.equals(DEL)){
int admID=Integer.parseInt(idField.getText());
if(!checkIDField(admID))
return;
int choice=JOptionPane.showConfirmDialog(frame,
"Are you sure to delete this administrator, ID:"+idField.getText()+"?",
"Delete Confirm",
JOptionPane.YES_NO_OPTION);
//System.out.println("m type"+choice);
if(choice==1){ //do not want to del
statusLabel.setText("");
return;
}
Server.getServerDatabase().delAdm(admID);
resetNumber();
statusLabel.setText("");
}
else if(type.equals(REPW)){
if(!repassword){
repassword=true;
pwLabel.setText("Input a new password:");
pwLabel.setForeground(Color.RED);
pwField.setEnabled(true);
pwButton.setEnabled(true);
}
}
else{
frame.dispose();
ServerControlGUI.admGUIExit();
}
}
public void windowClosing(WindowEvent we){
ServerControlGUI.admGUIExit();
}
private Container makeMainPane(){
Container original=new JPanel();
original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
JPanel numPane=new JPanel(new GridLayout(3,1));
JLabel currentAdmLabel;
if(currentAdm==null)
currentAdmLabel=new JLabel();
else
currentAdmLabel=new JLabel("Current Administrator's ID:"+currentAdm.getID());
admNumLabel=new JLabel("Number of Administrators:"+Server.getServerDatabase().getAdmNum());
admNumLabel.setHorizontalAlignment(SwingConstants.CENTER);
currentAdmLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusLabel=new JLabel();
numPane.add(currentAdmLabel);
numPane.add(admNumLabel);
numPane.add(statusLabel);
JPanel idPane=new JPanel(new GridLayout(1,2));
JLabel idLabel=new JLabel("Administrator's ID:");
idField=new JFormattedTextField();
idField.setValue(new Integer(0));
idPane.add(idLabel);
idPane.add(idField);
JPanel buttonPane=new JPanel(new GridLayout(1,4));
JButton button=new JButton("Add New"); //button add
button.setMnemonic(KeyEvent.VK_A);
button.setActionCommand(ADD);
button.addActionListener(this);
button.setToolTipText("To add a new administrator.");
buttonPane.add(button);
button=new JButton("Delete"); //del button
button.setMnemonic(KeyEvent.VK_D);
button.setToolTipText("To delete an administrator from database.");
button.setActionCommand(DEL);
button.addActionListener(this);
buttonPane.add(button);
button=new JButton("Repassword"); //repassword button
button.setMnemonic(KeyEvent.VK_R);
button.setToolTipText("To change current administrator's password.");
button.setActionCommand(REPW);
button.addActionListener(this);
buttonPane.add(button);
button=new JButton("Exit"); //exit buton
button.setMnemonic(KeyEvent.VK_E);
button.setActionCommand(EXIT);
button.addActionListener(this);
buttonPane.add(button);
JPanel pwPane=new JPanel(new GridLayout(1,3));
pwLabel=new JLabel();
pwField=new JPasswordField();
pwField.setActionCommand(OK);
pwField.addActionListener(this);
pwField.setEchoChar('●');
pwField.setEnabled(false);
pwButton=new JButton("OK");
pwButton.setEnabled(false);
pwButton.setActionCommand(OK);
pwButton.addActionListener(this);
pwPane.add(pwLabel);
pwPane.add(pwField);
pwPane.add(pwButton);
original.add(numPane);
original.add(idPane);
original.add(buttonPane);
original.add(pwPane);
return original;
}
private boolean checkIDField(int id){
if(id<=0){ //if id is negative
statusLabel.setText("ID: "+idField.getText()+" is not valid");
statusLabel.setForeground(Color.RED);
//table.setModel(new DefaultTableModel());
return false;
}
if(!Server.getServerDatabase().containsAdm(id)) { //if id dosen't exist
statusLabel.setText("ID: "+idField.getText()+" doesn't exsit!");
statusLabel.setForeground(Color.RED);
//table.setModel(new DefaultTableModel());
return false;
}
return true;
}
private void resetNumber(){
//System.out.println("called");
admNumLabel.setText("Administrator Number:"+Server.getServerDatabase().getAdmNum());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -