📄 addoperater.java
字号:
package bookmanager;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*A public class
*/
public class addOperater extends JInternalFrame {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/
//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creaing the North Label
private JLabel northLabel = new JLabel("OPERATER INFORMATION");
//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationPanel = new JPanel();
//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[4];
//for creating an array of String
private String[] informationString = {
" Username: "," The Password: "," Rewrite the password: ",
" Level: "};
//for creating an array of JTextField
private JTextField informationTextField = new JTextField();
//for creating an array of JPasswordField
private JPasswordField[] informationPasswordField = new JPasswordField[2];
private JComboBox operaterTypes;
//for creating an Internal Panel in the center panel
private JPanel insertInformationButtonPanel = new JPanel();
//for creating a button
private JButton insertInformationButton = new JButton("Insert the Information");
//for creating the South Panel
private JPanel southPanel = new JPanel();
//for creating a button
private JButton OKButton = new JButton("Exit");
//create objects from another classes for using them in the ActionListener
private toDatabase dataToDatabase;
//for creating an array of string to store the data
private String[] data;
//for checking the password
public boolean isPasswordCorrect() {
String first = new String(informationPasswordField[0].getPassword());
String second = new String(informationPasswordField[1].getPassword());
if(first.equals(second))
data[1] = second;
else
return false;
return true;
}
//for checking the information from the text field
public boolean isCorrect() {
data = new String[3];
for(int i = 0; i < informationLabel.length; i++) {
if(i == 0) {
if(! informationTextField.getText().equals("")) {
data[i] = informationTextField.getText();
}
else
return false;
}
if(i == 1 || i == 2) {
if(new String(informationPasswordField[i-1].getPassword()).equals(""))
return false;
}
if(i == 3 ) {
if(! operaterTypes.getSelectedItem().equals("")) {
data[i-1] = operaterTypes.getSelectedItem().toString();
}
else
return false;
}
}
return true;
}
//constructor of addBooks
public addOperater() {
//for setting the title for the internal frame
super("Add Operater", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Add16.gif")));
//for getting the graphical user interface components display area
Container cp = getContentPane();
//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font
northLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
//for adding the label to the panel
northPanel.add(northLabel);
//for adding the panel to the container
cp.add("North", northPanel);
//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the border to the panel
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new Operater:"));
//for setting the layout
informationPanel.setLayout(new GridLayout(6,2,1,1));
/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for(int i = 0; i < informationLabel.length; i++) {
informationPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Tahoma", Font.BOLD, 11));
if(i == 1 || i == 2) {
informationPanel.add(informationPasswordField[i - 1] = new JPasswordField(20));
informationPasswordField[i - 1].setFont(new Font("Tahoma", Font.PLAIN, 11));
}
if(i == 0) {
informationPanel.add(informationTextField = new JTextField(20));
informationTextField.setFont(new Font("Tahoma", Font.PLAIN, 11));
}
if(i == 3 ) {
informationPanel.add(operaterTypes = new JComboBox());
operaterTypes.setModel(new DefaultComboBoxModel(new String[] {"Admin","Loan Operater"}));
operaterTypes.setFont(new Font("Tahoma", Font.PLAIN, 11));
}
}
centerPanel.add("Center", informationPanel);
/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* and adding the button to the panel. *
* finally adding the panel to the container *
***********************************************************************/
insertInformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
insertInformationButton.setFont(new Font("Tahoma", Font.BOLD, 11));
insertInformationButtonPanel.add(insertInformationButton);
centerPanel.add("South", insertInformationButtonPanel);
cp.add("Center", centerPanel);
/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* adding the button to the panel & setting the border. *
* finally adding the panel to the container *
***********************************************************************/
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
OKButton.setFont(new Font("Tahoma", Font.BOLD, 11));
southPanel.add(OKButton);
southPanel.setBorder(BorderFactory.createEtchedBorder());
cp.add("South", southPanel);
/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField[] and make the connection for database, *
* after that update the table in the database with the new value *
***********************************************************************/
insertInformationButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//for checking if there is a missing information
if(isCorrect()) {
if(isPasswordCorrect()) {
Thread runner =
new Thread() {
public void run() {
dataToDatabase = new toDatabase();
String temp="insert into Operater (username,password,level) values ('"+data[0]+"','"+data[1]+"','"+data[2]+"')";
//for checking if there is no same information in the database
if(!dataToDatabase.isDouble("SELECT ID FROM Operater WHERE Username ='" + data[0]+"'")) {
System.out.println(data[0]+""+data[1]+""+data[2]);
System.out.println(temp);
// dataToDatabase.update("insert into Operater (username,password,level) values ('"+data[0]+"','"+data[1]+"','"+data[2]+"')");
dataToDatabase.update(temp);
// dataToDatabase.update( "INSERT INTO Members (MemberID," +
//"Password, Name, EMail, Major) VALUES (" +
//data[0] + ", '" + data[1] + "','" + data[2] + "','" +
//data[3] + "','" + data[4] + "')" );
//for setting the array of JTextField & JPasswordField to null
for(int i = 0; i < informationLabel.length; i++) {
if(i == 0)
informationTextField.setText(null);
if(i == 1 || i == 2)
informationPasswordField[i-1].setText(null);
}
// JOptionPane.showMessageDialog(null, "Operater has added successly!");
}
else
JOptionPane.showMessageDialog(null,"Operater is in the Library","Error",JOptionPane.ERROR_MESSAGE);
}
};
runner.start();
}
//if the password is wrong
else
JOptionPane.showMessageDialog(null,"the passowrd is wrong","Error",JOptionPane.ERROR_MESSAGE);
}
//if there is a missing data, then display Message Dialog
else
JOptionPane.showMessageDialog(null,"Please, complete the information","Warning",JOptionPane.WARNING_MESSAGE);
}
});
//for adding the action listener for the button to dispose the frame
OKButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -