1. In-place instantiation, table, table scroll bar, scroll bar automatically appear, table Model 2. New window, upper and lower adjustment of the input box, the data acquisition method in the input box, the input box assembly settings, reset the input box contents
Note that a bookmanager is common with the main window ... In the beginning because of this window new a manager kill add not to go in ... You won't get an error ....
1 Code first the main window can show all the library buttons 2 additions and deletions are only made to the new
Package Com.lovo.ui;import Java.awt.borderlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.list;import Javax.swing.jbutton;import Javax.swing.JFrame;import Javax.swing.jpanel;import Javax.swing.jscrollpane;import Javax.swing.jtable;import Javax.swing.UIManager;import Javax.swing.unsupportedlookandfeelexception;import Javax.swing.table.defaulttablemodel;import Com.lovo.bookmanager.book;import com.lovo.bookmanager.bookmanager;/** * Rental Book viewing interface * @author Abe */@SuppressWarnings (" Serial ") public class MyTable extends JFrame {private Bookmanager manager = new Bookmanager ();p rivate JTable Booktabel;priv Ate string[] ColumnNames = {"Number", "title", "Price", "status", "Loan Date", "Number of loans"};p rivate JPanel mypanel = new JPanel ();p rivate Jbutto n newbutton;private JButton resectbutton;/** * constructor, initialize */public MyTable () {this.setsize (+); This.setresizable ( FALSE); This.setdefaultcloseoperation (exit_on_close); This.setlocationrelativeto (null); Initcomponents ();} /** * Method: Get the classUsed Manager * @return */public bookmanager Getbookmanager () {return manager;} /** * Method: Get the attribute that the manager says has a book, save in object[][] */public void Refreshtablemodel () {if (Booktabel! = null) {list<book> List = Manager.findall (); object[][] Date = new Object[list.size ()][columnnames.length];for (int i = 0; i < list.size (); i++) {Book temp = List.get (i);d ate[i][0] = TEMP.GETISBN ();d ate[i][1] = temp.getname ();d ate[i][2] = Temp.getprice ();d ate[i][3 ] = temp.islended ()? "Lent": "Not Lent";d ate[i][4] = temp.getlenddate ();d ate[i][5] = Temp.getcounter ();} Create an anonymous inner class in-place instantiation inside the method to set the tabular model and put the data into the table Booktabel.setmodel (new DefaultTableModel (date, columnnames) {@Overridepublic Boolean iscelleditable (int row, int colum) {return false;}});}} public void Initcomponents () {Booktabel = new JTable ();//JTable Table Booktabel.gettableheader (). setreorderingallowed ( FALSE);//Set header does not allow rearranging refreshtablemodel (); JScrollPane jsp = new JScrollPane (Booktabel);//Not adding a new item, add the table to be decorated directly in new//Save the table and scroll bar in the Jspthis.add (JSP, Borderlayout.center);//WillJSP into the window middle area Newbutton = new JButton ("new"); Newbutton.addactionlistener (new ActionListener () {// In-place instantiated listener @overridepublic void actionperformed (ActionEvent e) {if (e.getsource () = = Newbutton) {New Addbookdialog ( mytable.this). setvisible (true);//pass yourself to the new window, the new window is set to visible}}); Resectbutton = new JButton ("delete"); Mypanel.add (Newbutton); Mypanel.add (Resectbutton); This.add (Mypanel, Borderlayout.south);} Main Method Primary window visible setting UI is the same as the current system throws automatically generated for the system ... public static void main (string[] args) throws Classnotfoundexception,instan Tiationexception, illegalaccessexception,unsupportedlookandfeelexception {uimanager.setlookandfeel ( Uimanager.getsystemlookandfeelclassname ()); new MyTable (). setvisible (True);}}
2. New Window Code
Package Com.lovo.ui;import Java.awt.component;import Java.awt.font;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jdialog;import Javax.swing.JLabel; Import Javax.swing.jspinner;import Javax.swing.jtextfield;import Javax.swing.spinnermodel;import Javax.swing.spinnernumbermodel;import Javax.swing.uimanager;import com.lovo.bookmanager.book;/** * class: New window * @author Abe */@SuppressWarnings ("Serial") public class Addbookdialog extends JDialog {//JDialog not JFrame You can set modal fixed focus in this window private jlabel[] hint = new jlabel[3];p rivate static string[] str = {"Number", "title", "Day Rent"};p rivate JTextField Newisdn;private JTextField newname;private JSpinner newprice;private JButton addbutton;private JButton Resetbutton; Private MyTable table;/** * constructor * @param table */public addbookdialog (MyTable table) {this.table = Table;this.setmodal (True ); This.setsize (400,400); this.setresizable (false); This.setlocationrelativeto (null); This.setlayout (null); This.setdeFaultcloseoperation (Dispose_on_close); iniscomponents ();} /** * Method: Initialize widget */private void iniscomponents () {for (int i = 0; i < hint.length; i++) {Hint[i] = new JLabel (str[i]); hi Nt[i].setbounds (+ +), This.add (Hint[i]);} NEWISDN = new JTextField (); Newisdn.setbounds (A. This.add (NEWISDN); newName = new JTextField (); Newname.setbounds (This.add (newName),; Spinnermodel Smodel = new Spinnernumbermodel (1.0, 0.1, 5.0, 0.1); newprice = new JSpinner (Smodel); Newprice.setbounds (130, 200,30) This.add (newprice) AddButton = new JButton ("new"); Addbutton.setbounds (100, 300, 70, 30); Addbutton.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {Object obj = E.getsource (); if (obj = = AddButton) {//Price adjustment label String ISDN = Newisdn.gettext (); String name = Newname.gettext (); String pricestr = String.Format ("%.1f", Newprice.getmodel (). GetValue ());d ouble price =double.parsedouble (PRICESTR); Table.getbookmanager (). Add (new book(Isdn,name,price)); Table.refreshtablemodel (); AddBookDialog.this.dispose ();}}); This.add (AddButton); Resetbutton = new JButton ("reset"); Resetbutton.setbounds (230, 300, 70, 30); Resetbutton.addactionlistener (new ActionListener () {///in-place instantiation of key trigger event reset @overridepublic void actionperformed ( ActionEvent e) {Object obj = E.getsource (); if (obj = = Resetbutton) {for (Component C:getcontentpane (). getcomponents ()) {// The final placement Kit com.lovo.MyUtil.resetComponent (c);//Call the Reset Tool}}}); This.add (Resetbutton);//Unified format container with container, does not change font fonts = new Font ("Microsoft Jas Black", font.plain, +); for (Component C:this.getcontentpane (). getcomponents ()) {C.setfont (Font);}} public static void Main (string[] args) throws Exception{uimanager.setinstalledlookandfeels ( Uimanager.getinstalledlookandfeels ()); new Addbookdialog (null). SetVisible (True);}}
3.WOOO Tool Pack
Package Com.lovo;import Java.awt.component;import Javax.swing.jcheckbox;import javax.swing.jcombobox;import Javax.swing.jpasswordfield;import javax.swing.jspinner;import javax.swing.jtextfield;/** * wooo Plan Package * @author ABE * * public class Myutil {private Myutil () {}/** * method: Auto-match type initialization input box * @param c */public static void Resetcomponent (Component c) {if (c instanceof JTextField) {//text box ((JTextField) c). SetText (""); else if (c instanceof JPasswordField) {//Password box ((JPasswordField) c). SetText (""); else if (c instanceof jcheckbox) {//check box ((Jcheckbox) c). SetSelected (false);} else if (c instanceof jcombobox<?>) {//drop-down box ((jcombobox<?>) c). setSelectedIndex (0);} else if (c instanceof JSpinner) {//Do not know what box, there are upper and lower adjustment arrows ((JSpinner) c). SetValue (0);}}
Java Programming (14.3)-----Library Management System Preliminary Design interface page ~ Library directory view window, new book Add window