📄 addbooks.java
字号:
//import the packages for using the classes in them into the program
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*A public class
*/
public class addBooks 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("BOOK INFORMATION");
//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationLabelPanel = new JPanel();
//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[8];
//for creating an array of String
private String[] informationString = {
"Book's Subject: ","Book's Title: ",
"Author(s)'s Name: ","Publisher's Name: ",
"Book's Copyright: ","Edition number: ",
"Number of Pages: ","Book's ISBN"
};
//for creating an Internal Panel in the center panel
private JPanel informationTextFieldPanel = new JPanel();
//for creating an array of JTextField
private JTextField[] informationTextField = new JTextField[8];
//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 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 setting availble option to true
private boolean availble = true;
//for checking the information from the text field
public boolean isCorrect() {
data = new String[8];
for(int i = 0; i < informationLabel.length; i++) {
if(!informationTextField[i].getText().equals("")) {
data[i] = informationTextField[i].getText();
}
else
return false;
}
return true;
}
//constructor of addBooks
public addBooks() {
//for setting the title for the internal frame
super("Add New Books",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();
cp.setLayout(new BorderLayout(5,5));
//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font for the North Panel
northLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
//for adding the label in the North Panel
northPanel.add(northLabel);
//for adding the north panel to the container
cp.add(northPanel,BorderLayout.NORTH);
//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the border to the panel
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new book:"));
//for setting the layout
informationLabelPanel.setLayout(new GridLayout(8,1,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++) {
informationLabelPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Tahoma", Font.BOLD, 11));
}
centerPanel.add(informationLabelPanel,BorderLayout.WEST);
//for setting the layout
informationTextFieldPanel.setLayout(new GridLayout(8,1,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 < informationTextField.length; i++) {
informationTextFieldPanel.add(informationTextField[i] = new JTextField(25));
informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
}
centerPanel.add(informationTextFieldPanel,BorderLayout.EAST);
/***********************************************************************
* 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( insertInformationButtonPanel,BorderLayout.SOUTH);
cp.add(centerPanel,BorderLayout.CENTER);
/***********************************************************************
* 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(southPanel,BorderLayout.SOUTH);
/***********************************************************************
* 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()) {
Thread runner =
new Thread() {
public void run() {
dataToDatabase = new toDatabase();
//for checking if there is no same information in the database
if(!dataToDatabase.isDouble("SELECT BookID FROM Books WHERE ISBN = '" + data[7] + "'")) {
dataToDatabase.update("INSERT INTO Books (Subject,Title,Author,"+
"Publisher,Copyright,Edition,Pages,ISBN,Availble) VALUES ('"+
data[0] + "','" + data[1] + "','" + data[2] + "','"+
data[3] + "', " + data[4] + ", " + data[5] + ", " +
data[6] + ", '" + data[7] + "', " + availble+ ")");
//for setting the array of JTextField to empty
for(int i = 0; i < informationTextField.length; i++)
informationTextField[i].setText(null);
}
else
JOptionPane.showMessageDialog(null,"The book is in the library","Error",JOptionPane.ERROR_MESSAGE);
}
};
runner.start();
}
//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();
}
});
setSize(450,330);
setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -