📄 jlibrary.java
字号:
package bookmanager;
//import the packages for using the classes in them into the program
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.KeyStroke;
/**
*A public class
*/
public class JLibrary extends JFrame implements ActionListener {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/
//for creating the JMenuBar that contains the JMenu
private JMenuBar menubar = new JMenuBar();
//for creating the JMenu for the program
private JMenu fileMenu, bookMenu, memberMenu, searchMenu, loanMenu, helpMenu;
//for creating the JMenuItem for JMenu
private JMenuItem login, addOperater, exit, addBook, listBook, listAvailbleBook,
listBorrowedBook, addMember, removeMember, listMember,
searchBooksAndMembers, borrowBook, returnBook, help, about;
//for creating an imageIcon
private ImageIcon[] icons;
//for creating the name of the image file 16*16
private String[] imageName16 = {"images/Export16.gif","images/Add16.gif","images/Exit16.gif",
"images/Add16.gif","images/List16.gif",
"images/Delete16.gif","images/Find16.gif",
"images/Import16.gif",
//"images/Export16.gif",
"images/Help16.gif","images/About16.gif"};
//for creating ToolBar for the program
private JToolBar toolbar = new JToolBar("Tool Bar");
//for creating the buttons to use them in ToolBar
private JButton[] button;
//for creating the name of the image file 24*24
private String[] imageName24 = {"images/Add24.gif", "images/List24.gif",
"images/List24.gif","images/List24.gif",
"images/Add24.gif", "images/List24.gif",
"images/Delete24.gif","images/Find24.gif",
"images/Export24.gif","images/Import24.gif",
"images/Help24.gif","images/About24.gif",
"images/Export24.gif","images/Add24.gif",
"images/Exit24.gif"};
//for creating the tipText for the toolbar
private String[] tipText = {"Add Books","List All Books","List Availble Books",
"List Borrowed Books","Add Members","List Members",
"Remove Members","Search","Borrow Books",
"Return Books","Help","About","Login","Add Operater","Exit"};
//for creating JDeskTopPane for using JInternalFrame on the desktop
public JDesktopPane desktop = new JDesktopPane();
/***************************************************************************
*create objects from another classes for using them in the ActionListener *
***************************************************************************/
private Login logined;
private addOperater addOperaterd;
private listBooks listBooK;
private addBooks addBooK;
private listAvailbleBooks listAvailble;
private listBorrowedBooks listBorrowed;
private borrowBooks borrowBooK;
private returnBooks returnBooK;
private addMembers addMembeR;
private listMembers listMembeR;
private removeMembers removeMembeR;
private searchBooksAndMembers search;
//constructor of JLibrary
public JLibrary() {
//for setting the title for the frame
super("JAVA Library System");
//for settting an icon for the program
Toolkit kit = Toolkit.getDefaultToolkit();
Image image = kit.getImage(ClassLoader.getSystemResource("images/Host16.gif"));
setIconImage(image);
//for setting the menu bar
setJMenuBar(menubar = new JMenuBar());
//for adding book, member, search, loan & help Menus to the menu bar
menubar.add(fileMenu = new JMenu("File"));
menubar.add(bookMenu = new JMenu("Books"));
menubar.add(memberMenu = new JMenu("Members"));
menubar.add(searchMenu = new JMenu("Search"));
menubar.add(loanMenu = new JMenu("Loan"));
menubar.add(helpMenu = new JMenu("Help"));
//for setting the mnemonic
fileMenu.setMnemonic('f');
bookMenu.setMnemonic('b');
memberMenu.setMnemonic('m');
searchMenu.setMnemonic('s');
loanMenu.setMnemonic('l');
helpMenu.setMnemonic('h');
//for setting the image icons
icons = new ImageIcon[10];
for(int i = 0; i < 10; i++) {
icons[i] = new ImageIcon(ClassLoader.getSystemResource(imageName16[i]));
}
//for adding add, list, listAvailble & listBorrowed Books to the bookMenu
fileMenu.add(login = new JMenuItem("Login", icons[0]));
fileMenu.add(addOperater= new JMenuItem("Add Operater",icons[1]));
fileMenu.add(exit = new JMenuItem("Exit", icons[2]));
//for adding the actionListener
login.addActionListener(this);
addOperater.addActionListener(this);
exit.addActionListener(this);
//for adding add, list, listAvailble & listBorrowed Books to the bookMenu
bookMenu.add(addBook = new JMenuItem("Add Book", icons[1]));
bookMenu.add(listBook = new JMenuItem("List All Books", icons[4]));
bookMenu.add(listAvailbleBook = new JMenuItem("List Availble Books", icons[4]));
bookMenu.add(listBorrowedBook = new JMenuItem("List Borrowed Books", icons[4]));
//for adding the actionListener
addBook.addActionListener(this);
listBook.addActionListener(this);
listAvailbleBook.addActionListener(this);
listBorrowedBook.addActionListener(this);
//for adding add, list & remove Members to the memberMenu
memberMenu.add(addMember = new JMenuItem("Add Member", icons[1]));
memberMenu.add(listMember = new JMenuItem("List All Members", icons[4]));
memberMenu.add(removeMember= new JMenuItem("Remove Member", icons[5]));
//for adding the actionListener
addMember.addActionListener(this);
listMember.addActionListener(this);
removeMember.addActionListener(this);
//for adding add, list & remove Members to the memberMenu
searchMenu.add(searchBooksAndMembers = new JMenuItem("Search", icons[6]));
//for adding the actionListener
searchBooksAndMembers.addActionListener(this);
//for adding borrow & return books to the loanMenu
loanMenu.add(borrowBook = new JMenuItem("Borrow a Book", icons[0]));
loanMenu.add(returnBook = new JMenuItem("Return a Book", icons[7]));
//for adding the actionListener
borrowBook.addActionListener(this);
returnBook.addActionListener(this);
//for adding help & about to the helpMenu
helpMenu.add(help = new JMenuItem("Help", icons[8]));
helpMenu.add(about= new JMenuItem("About", icons[9]));
//for adding the actionListener
help.addActionListener(this);
about.addActionListener(this);
//get the graphical user interface components display area
Container cp = getContentPane();
//for setting the ContentPane
cp.add(desktop);
//for setting the background
///desktop.setBackground(Color.GRAY);
//for Making dragging faster
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
//for adding the toolbar to the container
cp.add("North", toolbar);
button = new JButton[15];
for(int i = 0; i < 15; i++) {
if(i == 4 || i == 7 || i == 10 || i == 12)
toolbar.addSeparator();
toolbar.add(button[i] = new JButton(new ImageIcon(ClassLoader.getSystemResource(imageName24[i]))));
button[i].setToolTipText(tipText[i]);
button[i].addActionListener(this);
}
login.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.ALT_MASK));
addOperater.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.ALT_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, ActionEvent.CTRL_MASK));
searchBooksAndMembers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
addBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
listBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
addMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
listMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
removeMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
borrowBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
returnBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
help.setAccelerator(KeyStroke.getKeyStroke("F1"));
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowOpened(WindowEvent arg0) {
handleThisWindowOpened(arg0);
}
});
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = 750, height = 550;
if(screenSize.width >800)
width = 950;
if (screenSize.height >600)
height = 650;
setSize(width, height);
setLocation((screenSize.width - width)/2,(screenSize.height-height) / 2);
//for setting the JFrame Visible
setVisible(true);
}
/**
*this method is invoked when an action occurs.
*@param e the action event.
*/
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == addBook || ae.getSource() == button[0]) {
Thread runner =
new Thread() {
public void run() {
addBooK = new addBooks();
desktop.add(addBooK);
try {
addBooK.setSelected(true);
}
catch(java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if(ae.getSource() == listBook || ae.getSource() == button[1]) {
Thread runner =
new Thread() {
public void run() {
listBooK = new listBooks();
desktop.add(listBooK);
try {
listBooK.setSelected(true);
}
catch(java.beans.PropertyVetoException e) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -