⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 returnbooks.java

📁 用java编写的图书馆书籍借阅管理系统,后台采用access数据库,运行前需要配置.具有书籍添加,借阅登记打印等功能
💻 JAVA
字号:
//import the packages for using the classes in them into the program
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

/**
 *A public class
 */
public class returnBooks extends JInternalFrame {
	/***************************************************************************
	 ***      declaration of the private variables used in the program       ***
	 ***************************************************************************/
	
	//for creating the North Panel
	private JPanel northPanel = new JPanel();
	//for creating the label
	private JLabel title = 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 informationPanel = new JPanel();
	//for creating an array of JLabel
	private JLabel[] informationLabel = new JLabel[2];
	//for creating an array of String
	private String[] informationString = {" Write the Book ID:", " Write the Member ID:"};
	//for creating an array of JTextField
	private JTextField[] informationTextField = new JTextField[2];
	//for creating an array of string to store the data
	private String[] data;
	
	//for creating an Internal Panel in the center panel
	private JPanel returnButtonPanel = new JPanel();
	//for creating the buton
	private JButton returnButton = new JButton("Return");
	
	//for creating the panel
	private JPanel southPanel = new JPanel();
	//for creating the button
	private JButton cancelButton = new JButton("Cancel");
	
	//for creating an object from toDatabase() class
	private toDatabase dataToDatabase;
	//for checking the information from the text field
	public boolean isCorrect() {
		data = new String[2];
		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 returnBooks
	public returnBooks() {
		//for setting the title for the internal frame
		super("Return books", false, true, false, true);
		//for setting the icon
		setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Import16.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
		title.setFont(new Font("Tahoma", Font.BOLD, 14));
		//for adding the label
		northPanel.add(title);
		//for adding the north panel to the container
		cp.add("North", northPanel);
		
		//for setting the layout
		centerPanel.setLayout(new BorderLayout());
		//for setting the layout for the internal panel
		informationPanel.setLayout(new GridLayout(2,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));
			informationPanel.add(informationTextField[i] = new JTextField());
			informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
		}	
		centerPanel.add("Center", informationPanel);
		//for setting the layout
		returnButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		//for adding the button
		returnButtonPanel.add(returnButton);
		//for setting the font to the button
		returnButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		//for adding the internal panel to the panel
		centerPanel.add("South", returnButtonPanel);
		//for setting the border
		centerPanel.setBorder(BorderFactory.createTitledBorder("Return a book:"));
		//for adding the center panel to the container
		cp.add("Center", centerPanel);
		
		//for setting the layout
		southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		//for adding the button
		southPanel.add(cancelButton);
		//for setting the font to the button
		cancelButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		//for setting the border
		southPanel.setBorder(BorderFactory.createEtchedBorder());
		//for adding the south panel to the container
		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      *
		 ***********************************************************************/
		returnButton.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.memberID("SELECT MemberID FROM Books WHERE BookID = " + data[0])) != 0) {
								dataToDatabase.update("UPDATE Books SET Availble = true," +
								"DateOfBorrowed = null," +
								"DateOfReturn   = null," +
								"MemberID       = null " +
								"WHERE BookID   = " + data[0]);
								int numberOfBooks = dataToDatabase.numberOfBooks("SELECT numberOfBooks FROM " +
								"Members WHERE MemberID = " + data[1]) - 1;
								dataToDatabase.update("UPDATE Members SET numberOfBooks = " +
								numberOfBooks + " WHERE MemberID = " + data[1]);
								//for setting the array of JTextField to null
								for(int i = 0; i < informationTextField.length; i++)
									if(i != 2)
										informationTextField[i].setText(null);
							}
							else
								JOptionPane.showMessageDialog(null,"The book is not borrowed","Warning",JOptionPane.WARNING_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
		cancelButton.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 + -