📄 jlibrary.java
字号:
package lib;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.plaf.metal.*; //For Change Look & Feel and Themes Options.
import javax.swing.event.*;
import java.io.*;
import java.util.Date;
public class JLibrary extends JFrame implements ActionListener,ItemListener{
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/
//for creating the JPanel
private JPanel searchPanel = new JPanel();
//for creating the JToolBar for the program
private JToolBar searchToolBar = new JToolBar();
//for creating the label
private JLabel searchLabel = new JLabel("按书名搜索: ",new ImageIcon(ClassLoader.getSystemResource("images/search.gif")),0);
//for creating the JTextField to use it on the searchToolBar
private JTextField searchTextField = new JTextField(15);
//for creating the JButton to use it on the searchToolBar
private JButton goButton = new JButton("Go");
//for creating JTabbedPane
//private JTabbedPane tabbedPane = new JTabbedPane();
//for creating JDeskTopPane for using JInternalFrame on the desktop
private JDesktopPane desktop = new JDesktopPane();
//private JDesktopPane desktop;
//for creating JSplitPane
private JSplitPane splitPane;
//for creating JScrollPane for JDesktopPane
private JScrollPane desktopScrollPane;
private JScrollPane treeScrollPane;
//for creating the background
//private ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("images/Logo.JPG"));
//for creating JLabel
//private JLabel background = new JLabel(icon);
/***************************************************************************
*create objects from another classes for using them in the ActionListener *
***************************************************************************/
private Menubar menu;
private Toolbar toolbar;
private StatusBar statusbar = new StatusBar();
private ListBooks listBooks;
private AddBooks addBooks;
private ListAvailbleBooks listAvailble;
private ListBorrowedBooks listBorrowed;
private EditBooks editBooks;
private RemoveBooks removeBooks;
private BorrowBooks borrowBooks;
private ReturnBooks returnBooks;
private BooksInformation booksInformation;
private Admin admin;
private AddMembers addMembers;
private ListMembers listMembers;
private EditMembers editMembers;
private RemoveMembers removeMembers;
private MembersInformation membersInformation;
private SearchBooksAndMembers search;
private static final String DEFAULT_QUERY = "SELECT BookID, Subject, Title, Author," +
"Publisher, Copyright, Edition, Pages, NumberOfBooks, ISBN, Library, Availble FROM Books";
private String name;
//constructor of JLibrary
public JLibrary(boolean type,String name) {
//for setting the title for the frame
super("图书管理系统");
this.name=name;
Date d = new Date();
Log log = new Log("*********************************************"+
"\n"+" Log in:"+ name +"\n"+" Log Time:"+d);
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//for setting the size
setSize(screenSize.width, screenSize.height - 30);
//for setting resizable to false
setResizable(false);
/**
*for settting an icon for the program
*/
Toolkit kit = Toolkit.getDefaultToolkit();
Image image = kit.getImage(ClassLoader.getSystemResource("images/Host16.gif"));
this.setIconImage(image);
menu = new Menubar();
toolbar = new Toolbar();
//for setting the menu bar
this.setJMenuBar(menu);
//for adding the actionListener
menu.log.addActionListener(this);
menu.printBook.addActionListener(this);
menu.exit.addActionListener(this);
menu.addBook.addActionListener(this);
menu.listBook.addActionListener(this);
menu.listAvailbleBook.addActionListener(this);
menu.listBorrowedBook.addActionListener(this);
menu.editBook.addActionListener(this);
menu.removeBook.addActionListener(this);
menu.bookInformation.addActionListener(this);
menu.addMember.addActionListener(this);
menu.listMember.addActionListener(this);
menu.editMember.addActionListener(this);
menu.removeMember.addActionListener(this);
menu.memberInformation.addActionListener(this);
menu.admin.addActionListener(this);
menu.searchBooksAndMembers.addActionListener(this);
menu.borrowBook.addActionListener(this);
menu.returnBook.addActionListener(this);
menu.help.addActionListener(this);
menu.about.addActionListener(this);
menu.change.addActionListener (this);
for( int i = 0; i < menu.radio.length ; i++)
menu.radio[i].addItemListener (this);
//Disable funtions for the User
if(type){
menu.admin.setEnabled(false);
menu.addBook.setEnabled(false);
toolbar.button[0].setEnabled(false);
menu.editBook.setEnabled(false);
toolbar.button[4].setEnabled(false);
menu.removeBook.setEnabled(false);
toolbar.button[5].setEnabled(false);
menu.addMember.setEnabled(false);
toolbar.button[7].setEnabled(false);
menu.editMember.setEnabled(false);
toolbar.button[9].setEnabled(false);
menu.removeMember.setEnabled(false);
toolbar.button[10].setEnabled(false);
menu.borrowBook.setEnabled(false);
toolbar.button[13].setEnabled(false);
menu.returnBook.setEnabled(false);
toolbar.button[14].setEnabled(false);
};
goButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if( !(searchTextField.getText()).equals("")){
Books book = new Books();
String title = "'%" + searchTextField.getText() + "%'";
String bookQuery = "SELECT BookID, Subject, Title, Author, Publisher," +
"Copyright, Edition, Pages, NumberOfBooks,ISBN,Library,Availble FROM Books" +
" WHERE " + "Title" + " LIKE " + title;
book.connection(bookQuery);
int bookID = book.getBookID();
if (bookID != 0) {
ListSearchBooks listBooks = new ListSearchBooks(bookQuery);
desktop.add(listBooks);
try {
listBooks.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
}
else {
JOptionPane.showMessageDialog(null, "没有匹配的图书", "出错", JOptionPane.ERROR_MESSAGE);
searchTextField.setText(null);
}
}
else JOptionPane.showMessageDialog(null, "请填写书名",
"警告", JOptionPane.WARNING_MESSAGE);
}
});
//StyleMenu For Changing the Program's Layout.
//SubMenu of ThemeMenu For Applying different Themes to Program By Building an Array of Themes to Apply.
//Attaching the PropertyChangeListener to Program For Applying the Themes.
UIManager.addPropertyChangeListener (new UISwitchListener ((JComponent)getRootPane()));
//get the graphical user interface components display the desktop
Container cp = getContentPane();
desktop.setBackground(Color.GRAY);
cp.add("Center", desktop);
//for setting the font
searchLabel.setFont(new Font("Default", Font.BOLD, 11));
//for setting the font
searchTextField.setFont(new Font("Default", Font.PLAIN, 12));
goButton.setFont(new Font("Default", Font.BOLD, 9));
//for adding the searchLable to the searchToolBar
searchToolBar.add(searchLabel);
//for adding the searchTextField to searchToolBar
searchToolBar.add(searchTextField);
//for adding the goButton to searchToolBar
searchToolBar.add(goButton);
//for adding listenerAction for the button
goButton.addActionListener(this);
//for setting the layout
searchPanel.setLayout(new BorderLayout());
//for adding the toolBar to the searchPanel
searchPanel.add("Center", toolbar);
//for adding the searchToolBar to the searchPanel
searchPanel.add("South", searchToolBar);
//for adding the searchPanel to the Container
cp.add("North", searchPanel);
//for adding the statusbar to the Container
cp.add("South", statusbar);
for (int i = 0; i < toolbar.imageName24.length; i++) {
//for adding the action to the button
toolbar.button[i].addActionListener(this);
}
//for adding WindowListener to the program
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Date d =new Date();
Log log =new Log("\n"+"Exit time:"+d+"\n"+"*********************************************");
System.exit(0);
}
});
//show the program
show();
}
/**
*this method is invoked when an action occurs.
*@param ae the action event.
*/
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == menu.addBook || ae.getSource() == toolbar.button[0]) {
Thread runner = new Thread() {
public void run() {
addBooks = new AddBooks();
desktop.add(addBooks);
try {
addBooks.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listBook || ae.getSource() == toolbar.button[1]) {
Thread runner = new Thread() {
public void run() {
listBooks = new ListBooks();
desktop.add(listBooks);
try {
listBooks.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listAvailbleBook || ae.getSource() == toolbar.button[2]) {
Thread runner = new Thread() {
public void run() {
listAvailble = new ListAvailbleBooks();
desktop.add(listAvailble);
try {
listAvailble.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listBorrowedBook || ae.getSource() == toolbar.button[3]) {
Thread runner = new Thread() {
public void run() {
listBorrowed = new ListBorrowedBooks();
desktop.add(listBorrowed);
try {
listBorrowed.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -