📄 librarysystem.java
字号:
userName = user;
//If Current User not Administrator of Program.
if (!(userName.equals ("Admin"))) {
newBook.setEnabled (false);
book.setEnabled (false);
btnNewBook.setEnabled (false);
newMember.setEnabled (false);
member.setEnabled (false);
btnNewMember.setEnabled (false);
issueBook.setEnabled (false);
issue.setEnabled (false);
btnIssue.setEnabled (false);
makeUser.setEnabled (false);
}
//Creating the StatusBar of Program.
lbStatus = new JLabel (" " + "Muhammad Wasif's LibrarySystem.", Label.LEFT);
lbStatus.setForeground (Color.black);
lbStatus.setToolTipText ("Program's Title");
lbWelcome = new JLabel ("Welcome " + userName + " Today is " + d + " ", JLabel.RIGHT);
lbWelcome.setForeground (Color.black);
lbWelcome.setToolTipText ("Welcome User : System Current Date");
statusBar.setLayout (new BorderLayout());
statusBar.add (lbStatus, BorderLayout.WEST);
statusBar.add (lbWelcome, BorderLayout.EAST);
//Setting the Contents of Programs.
getContentPane().add (toolBar, BorderLayout.NORTH);
getContentPane().add (desktop, BorderLayout.CENTER);
getContentPane().add (statusBar, BorderLayout.SOUTH);
//Getting the Database.
con = conn;
//Showing The Main Form of Application.
setVisible (true);
}
//Function For Performing different Actions By Menus of Program.
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == newBook || obj == book || obj == btnNewBook) {
boolean b = openChildWindow ("Add New Book");
if (b == false) {
AddBook adBook = new AddBook (con);
desktop.add (adBook); //Adding Child Window to DesktopPane.
adBook.show (); //Showing the Child Window.
}
}
else if (obj == newMember || obj == member || obj == btnNewMember) {
boolean b = openChildWindow ("Add New Member");
if (b == false) {
AddMember adMember = new AddMember (con);
desktop.add (adMember);
adMember.show ();
}
}
else if (obj == printBook || obj == print) {
getBookId ();
}
else if (obj == printMember) {
getMemberId ();
}
else if (obj == printIssueBook || obj == btnPrintIssue) {
getIssueBook ();
}
else if (obj == end) {
quitApp (); //Calling the Function to Quit the Program.
}
else if (obj == issueBook || obj == issue || obj == btnIssue) {
boolean b = openChildWindow ("Issue Book");
if (b == false) {
IssueBook isBook = new IssueBook (con);
desktop.add (isBook);
isBook.show ();
}
}
else if (obj == returnBook || obj == bookReturn || obj == btnReturn) {
boolean b = openChildWindow ("Return Book");
if (b == false) {
ReturnBook rtBook = new ReturnBook (con);
desktop.add (rtBook);
rtBook.show ();
}
}
else if (obj == delBook || obj == btnDelBook) {
boolean b = openChildWindow ("Delete Book");
if (b == false) {
DeleteBook dlBook = new DeleteBook (con);
desktop.add (dlBook);
dlBook.show ();
}
}
else if (obj == delMember || obj == btnDelMember) {
boolean b = openChildWindow ("Delete Member");
if (b == false) {
DeleteMember dlMember = new DeleteMember (con);
desktop.add (dlMember);
dlMember.show ();
}
}
else if (obj == findBook || obj == find || obj == btnFindBook) {
boolean b = openChildWindow ("Search Books");
if (b == false) {
SearchBook srBook = new SearchBook (con);
desktop.add (srBook);
srBook.show ();
}
}
else if (obj == findMember || obj == btnFindMember) {
boolean b = openChildWindow ("Search Members");
if (b == false) {
SearchMember srMember = new SearchMember (con);
desktop.add (srMember);
srMember.show ();
}
}
else if (obj == viewAllBook) {
boolean b = openChildWindow ("View All Books");
if (b == false) {
ViewAllBooks vwAllBook = new ViewAllBooks (con);
desktop.add (vwAllBook);
vwAllBook.show ();
}
}
else if (obj == viewAllMember || obj == view) {
boolean b = openChildWindow ("View All Members");
if (b == false) {
ViewAllMembers vwAllMember = new ViewAllMembers (con);
desktop.add (vwAllMember);
vwAllMember.show ();
}
}
else if (obj == viewIssueBook) {
boolean b = openChildWindow ("View All Issued Books");
if (b == false) {
ViewIssuedBooks vwIssue = new ViewIssuedBooks (con);
desktop.add (vwIssue);
vwIssue.show ();
}
}
else if (obj == change) {
Color cl = desktop.getBackground (); //Getting the Current Background Color.
//Showing the Color Dialog Box to Change Background Color.
cl = JColorChooser.showDialog (this, "Choose Background Color", cl);
if (cl == null) { } //If No Color is Selected.
else {
desktop.setBackground (cl); //Aplying Selected Color for Background Color.
desktop.repaint (); //Repaint the DesktopPane.
}
}
else if (obj == changePassword || obj == btnChange) {
boolean b = openChildWindow ("Change Password");
if (b == false) {
ChangePassword chPass = new ChangePassword (userName, con);
desktop.add (chPass);
chPass.show ();
}
}
else if (obj == makeUser) {
boolean b = openChildWindow ("Create New User");
if (b == false) {
NewUser mkUser = new NewUser (con);
desktop.add (mkUser);
mkUser.show ();
}
}
else if (obj == close) {
JInternalFrame Frames[] = desktop.getAllFrames (); //Getting all Open Frames.
for (int getFrameLoop = 0; getFrameLoop < Frames.length; getFrameLoop++) {
try {
Frames[getFrameLoop].setClosed (true); //Close the Active Frame.
break; //Exit From Loop.
}
catch (Exception CloseExc) { } //if Error then Do Nothing.
}
/*
//Following Lines also Work Nice If You Trying to Close the Active Form in Current Program
//But it only Works then If You've JDK 1.3 or Higher Installed on Your System.
try {
desktop.getSelectedFrame().setClosed(true); //Closing the Active Form.
}
catch (Exception CloseExc) { }*/
}
else if (obj == closeAll) {
JInternalFrame Frames[] = desktop.getAllFrames (); //Getting all Open Frames.
for (int getFrameLoop = 0; getFrameLoop < Frames.length; getFrameLoop++) {
try {
Frames[getFrameLoop].setClosed (true); //Close the Active Frame One By One.
}
catch (Exception CloseExc) { } //if Error then Do Nothing.
}
}
else if (obj == content || obj == btnHelp) {
//Showing Program's Help so User can Understand the Program & Work Easily in it.
boolean b = openChildWindow ("LibrarySystem Help");
if (b == false) {
LibraryHelp hlpLib = new LibraryHelp ("LibrarySystem Help", "Help/Library.htm");
desktop.add (hlpLib);
hlpLib.show ();
}
}
else if (obj == keyHelp || obj == btnKey) {
//Showing the Shortcut Keys of Program to Guide User.
boolean b = openChildWindow ("LibrarySystem Keys");
if (b == false) {
LibraryHelp hlpKey = new LibraryHelp ("LibrarySystem Keys", "Help/Keys.htm");
desktop.add (hlpKey);
hlpKey.show ();
}
}
else if (obj == about) {
//Showing the MessageBox Containig the Muhammad Wasif's LibrarySystem Credits.
String msg = "Muhammad Wasif's LibrarySystem.\n\n" + "Created & Designed By:\n" +
"Muhammad Wasif Javed\n\n" + "E-mail me:\n wasijaved@yahoo.com";
JOptionPane.showMessageDialog (this, msg, "About LibrarySystem", JOptionPane.PLAIN_MESSAGE);
}
}
//Function Perform By LookAndFeel MenuItem (Style MenuItem).
public void itemStateChanged (ItemEvent e) {
for( int i = 0; i < radio.length; i++ )
if(radio[i].isSelected()) { //Getting Selected Look & Feel Option.
changeLookAndFeel (i); //Change the Program's Look & Feel.
}
}
//Function for Changing the Program's Look & Feel.
public void changeLookAndFeel (int val) {
try {
UIManager.setLookAndFeel (looks[val].getClassName()); //Getting the Look & Feel Name.
SwingUtilities.updateComponentTreeUI (this); //Changing Look & Feel of Program.
}
catch (Exception e) { }
}
//Loop Through All the Opened JInternalFrame to Perform the Task.
private boolean openChildWindow (String title) {
JInternalFrame[] childs = desktop.getAllFrames (); //Get All Open Child Windows.
for (int i = 0; i < childs.length; i++) {
if (childs[i].getTitle().equalsIgnoreCase (title)) { //Getting the Title of Child Window.
childs[i].show (); //Setting Focus on the Child Window.
return true;
}
}
return false;
}
//Function For Closing the Program.
private void quitApp () {
try {
//Show a Confirmation Dialog.
int reply = JOptionPane.showConfirmDialog (this,
"Are you really want to exit From\nMuhammad Wasif's LibrarySystem?",
"LibrarySystem - Exit", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
//Check the User Selection.
if (reply == JOptionPane.YES_OPTION) { //If User's Choice Yes then.
setVisible (false); //Hide the Frame.
System.out.println ("\n\nThanks For Using Muhammad " + //Displaying Message on Screen.
"Wasif's LibrarySystem.\nAuthor : " +
"Muhammad Wasif Javed\n");
dispose(); //Free the System Resources.
System.exit (0); //Close the Application.
}
else if (reply == JOptionPane.NO_OPTION) { //If User's Choice No then.
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //Do Nothing Return to Program.
}
}
catch (Exception e) {}
}
//Following Functions use for Printing Book Record.
void getBookId () {
String printing;
try { //Getting User's Answer.
printing = JOptionPane.showInputDialog (this, "Enter Book Id to Print Book Detail.\n" +
"(Tip: Book Id Contains only Digits)", "LibrarySystem - PrintRecord", JOptionPane.PLAIN_MESSAGE);
if (printing == null) { } //If Cancelling the Printing.
if (printing.equals ("")) { //If No Value Provided.
JOptionPane.showMessageDialog (this, "Provide Book Id to Print.",
"LibrarySystem - EmptyField", JOptionPane.PLAIN_MESSAGE);
getBookId (); //Calling Same Function Again.
}
else {
findBookRec (printing, con); //Finding Book Record.
}
}
catch (Exception e) { }
}
//Function use to Find Book Record.
void findBookRec (String rec, Connection con) {
long id = Integer.parseInt (rec); //Converting String to Numeric.
long bookNo; //Use for Comparing the Book's Id.
boolean found = false; //To Confirm the Book's Id Existance.
try { //SELECT Query to Retrieved the Record.
String q = "SELECT * FROM Books WHERE BookId = " + id + "";
st = con.createStatement (); //Creating Statement Object.
ResultSet rs = st.executeQuery (q); //Executing the Query.
rs.next (); //Moving towards the Record.
bookNo = rs.getLong ("BookId"); //Storing the Record.
if (bookNo == id) { //If Record Found then Display Records.
found = true;
String bookName = rs.getString ("BookName");
String author = rs.getString ("BookAuthor");
long price = rs.getLong ("BookPrice");
String cat = rs.getString ("Category");
//Printing the Book Record.
printRecord (makeBookPrint ("" + id, bookName, author, "" + price, cat));
}
}
catch(SQLException sqlex) {
if (found == false) {
JOptionPane.showMessageDialog (this, "Record not Found.");
getBookId ();
}
}
}
//Function use to make Current Record ready for Print.
String makeBookPrint (String id, String name, String author, String price, String category) {
String data = "";
String data0 = "\n Muhammad Wasif's LibrarySystem \n"; //Page Title.
String data1 = " Book Detail \n\n"; //Page Header.
String data2 = " Book ID: " + id + "\n";
String data3 = " Book Name: " + name + "\n";
String data4 = " Book Author: " + author + "\n";
String data5 = " Book Price: " + price + "\n";
String data6 = " Book Category: " + category + "\n";
String data7 = " Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -