Los ejemplos en este artículo comparten con usted el código específico de Java para implementar el sistema de gestión de la biblioteca para su referencia. El contenido específico es el siguiente
/* (Comenta el comentario del encabezado del programa)
* Declaración de derechos de autor y de la versión Parte del programa
* Copyright (c) 2011, Escuela de Informática de la Universidad de Yantai
* Reservados todos los derechos.
* Nombre del archivo: "Sistema de gestión de libros-Java"
* Autor: Liu Jiangbo
* Fecha de finalización: 1 de marzo de 2012
* Número de versión: v3.0
* Descripción de tareas y soluciones
* Descripción de la pregunta:
* El final de los comentarios a la cabeza del programa
*/
Estado de creación de paquetes de archivo:
Bookdao.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. */ paquete com.liu.dao; import com.liu.po.bookbean; import java.io.*; import java.util.hashmap; import java.util.map; import java.util.logging.level; import java.util.logging.logger; / ** * * * @author asus */ public class bookdao {// escribir public void WriteBook (map <integer, bookbean> bookmap) {// fileoutputstream fos = null; ObjectOutputStream oos = null; intente {fos = new FileOutputStream ("f: //cache//book.txt"); oos = nuevo ObjectOutputStream (FOS); oos.writeObject (bookmap); // borrar el caché oos.flush (); } Catch (FileNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } // El nivel de excepción está al final (ioexception ex) {logger.getlogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); } finalmente {try {// abre y cierre oos.close (); fos.close (); } catch (ioException ex) {logger.getLogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); }}} // Leer mapa público <Integer, bookbean> readbook () {fileInputStream fis = null; ObjectInputStream ois = null; MAP <Integer, BookBean> map = null; intente {fis = new FileInputStream ("f: //cache//book.txt"); ois = nuevo ObjectInputStream (FIS); map = (map <integer, bookbean>) ois.readObject (); // La excepción ocurre y entra en Catch} Catch (ClassNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } Catch (FileNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } catch (ioException ex) {//logger.getlogger(typetao.class.getName()).log(Level.severe, null, ex); //ex.printstacktrace (); map = new HashMap <Integer, BookBean> (); // Crear mapa} Finalmente {try {if (ois! = null) {ois.close (); } if (fis! = null) {fis.close (); }} catch (ioException ex) {logger.getLogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); }} mapa de retorno; }}Typedao.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. */ paquete com.liu.dao; import com.liu.po.typebean; import java.io.*; import java.util.*; import java.util.logging.*; / **** Lea y escriba archivos*/ public class typedAo {// escribe public void writeType (map <integer, typeBean> typemap) {// fileoutputstream fos = null; ObjectOutputStream oos = null; intente {fos = new FileOutputStream ("f: //cache//type.txt"); oos = nuevo ObjectOutputStream (FOS); oos.writeObject (typemap); // borrar el caché oos.flush (); } Catch (FileNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } // El nivel de excepción está al final (ioexception ex) {logger.getLogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); } Finalmente {try {// abre primero y luego cierre oos.close (); fos.close (); } catch (ioException ex) {logger.getLogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); }}} // Leer mapa público <Integer, typeBean> readType () {fileInputStream fis = null; ObjectInputStream ois = null; Map <integer, typeBean> map = null; intente {fis = new FileInputStream ("f: //cache//type.txt"); ois = nuevo ObjectInputStream (FIS); map = (map <integer, typeBean>) ois.readObject (); // La excepción ocurre y entra en Catch} Catch (ClassNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } Catch (FileNotFoundException ex) {logger.getLogger (typedao.class.getName ()). Log (nivel.severe, null, ex); } catch (ioException ex) {//logger.getlogger(typetao.class.getName()).log(Level.severe, null, ex); //ex.printstacktrace (); MAP = new HashMap <Integer, typeBean> (); // crea map} finalmente {try {if (ois! = null) {ois.close (); } if (fis! = null) {fis.close (); }} catch (ioException ex) {logger.getLogger (typedAo.class.getName ()). Log (nivel.severe, null, ex); }} mapa de retorno; }}}Bookbean.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. */ paquete com.liu.po; import java.io.serializable; / ** * * @author asus */ public class bookbean implementa serializable {private int id; Nombre de libro de cadena privada; Cadena privada Booktype; Memo de cadena privada; dinero de cadena privada; / ** * @return el id */ public int getId () {return id; } / ** * @param id El id para establecer * / public void setid (int id) {this.id = id; } / ** * @return the BookName * / public String getBookName () {return BookName; } / ** * @param BookName the BookName para establecer * / public void setBookName (String BookName) {this.bookName = BookName; } / ** * @return the booktype * / public string getBookType () {return BookType; } / ** * @param booktype the booktype para establecer * / public void setbookType (string booktype) {this.bookType = booktype; } / ** * @return The Memo * / public String getMemo () {return Memo; } / ** * @param Memo La nota para establecer * / public void setMemo (string memo) {this.memo = memo; } / ** * @return el dinero * / public string getMoney () {return Money; } / ** * @param dinero el dinero para establecer * / public void setMoney (string dinero) {this.money = dinero; }}TipoBean.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. */ paquete com.liu.po; / ** * * @author asus */ import java.io.Serializable; public class tipoBean implementa serializable {private int id; String privado Typename; Memo de cadena privada; / ** * @return el id */ public int getId () {return id; } / ** * @param id El id para establecer * / public void setid (int id) {this.id = id; } / ** * @return the typename * / public string getTyPeName () {return typename; } / ** * @param typename typename para establecer * / public void settyPename (string typename) {this.typename = typename; } / ** * @return The Memo * / public String getMemo () {return Memo; } / ** * @param Memo La nota para establecer * / public void setMemo (string memo) {this.memo = memo; }}Loginform.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. *// * * Loginform.java * * creado el 2013-2-26, 18:33:36 */ paquete com.liu.view; import java.awt.event.keyAdapter; import java.awt.event.keyevent; import javax.swing.JOptionPane; / ** * * * @author asus */ public class LoginForm extiende javax.swing.jframe {/ ** Crea nueva forma Loginform */ public loginform () {initComponents (); } /** Este método se llama desde dentro del constructor para* inicializar el formulario. * Advertencia: no modifique este código. El contenido de este método es * siempre regenerado por el editor de formulario. */ @SupplesSwarnings ("Unchecked") // <Editor-Fold DefaultState = "colapsed" desc = "Código generado"> privado void initComponents () {jlabel1 = new javax.swing.jlabel (); jlabel2 = new javax.swing.jlabel (); jlabel3 = new javax.swing.jlabel (); Loginname = new javax.swing.jTextField (); Loginpwd = new javax.swing.jpasswordfield (); jButton1 = new javax.swing.jbutton (); setDefaultCloseOperation (javax.swing.windowconstants.exit_on_close); settitle ("Interfaz de inicio de sesión"); SetBounds (nuevo Java.awt.rectangle (300, 200, 0, 0)); setiConImage (nuevo javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/logo.jpg")). getImage ()); addkeylistener (new java.awt.event.keyAdapter () {public void keyPressed (java.awt.event.keyevent evt) {FormKeyPissed (evt);}}); jlabel1.setFont (nuevo java.awt.font ("宋体", 0, 36)); jlabel1.setForeground (nuevo java.awt.color (204, 0, 0)); jlabel1.settext ("宋体"); jlabel2.setFont (new Java.awt.font ("宋体", 0, 24)); jlabel2.settext ("Nombre de usuario:"); jlabel3.setFont (nuevo java.awt.font ("宋体", 0, 24)); jlabel3.settext ("Password:"); LoginName.SetName (""); // noi18n loginname.addactionListener (new java.awt.event.ActionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {loginnameactionperformed (evt);}}); LoginName.AddkeyListener (nuevo java.awt.event.keyAdapter () {public void keyPressed (java.awt.event.keyevent evt) {loginnameKeyPissed (evt);}}); Loginpwd.addkeylistener (new java.awt.event.keyAdapter () {public void keyPressed (java.awt.event.keyevent evt) {loginpwdkeyPressed (evt);}}); JButton1.setFont (nuevo Java.awt.font ("安一", 0, 24)); // NOI18N JBUTTON1.SETTEXT ("Login"); jButton1.AdDactionListener (new java.awt.event.actionListener () {public void ActionperFormed (java.awt.event.actionEvent evt) {jbutton1actionperformed (evt);}}); javax.swing.grouplayout diseño = new javax.swing.grouplayout (getContentPane ()); getContentPane (). setLayout (diseño); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(97, 97, 97) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent (jlabel2) .AddComponent (jlabel3)) .addpreferredGap (javax.swing.layoutstyle.componentPlacement.unrelated) .addgroup (licout.createparallelgroup (javax.swing.grouplauplayout.alignment.leading, falso). .addComponent (LoginName, javax.swing.grouplayout.default_size, 215, short.max_value)) .addcontainergap (88, short.max_value)) .addgroup (javax.swing.grouplayout.alignment.trailing, lectout.createsequentencialgroup () .addContainGap (130, short.max_value) .addComponent (jlabel1, javax.swing.grouplayout.preferred_size, 263, javax.swing.grouplayout.preferred_size) .addgap (113, 113, 113))) .addgroup (javax.swing.grouplayout.alignment.trailing, layout.createSequentialGroup () .addContainGap (299, short.max_value) .AddComponent (jButton1, javax.swing.grouplayout.preferred_size, 97, javax.swing.grouplayout.preferred_size) .addgap (110, 110, 110))); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(50, 50, 50) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.grouplayout.preferred_size) .addgap (38, 38, 38) .AddGroup (Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.baseline) .addcomponent (jlabel2) .addcomponent (loginname,, javax.swing.grouplayout.preferred_size, 29, javax.swing.grouplayout.preferred_size)) .addgap (26, 26, 26) .addgroup (Layout.CreateParallelgroup (javax.swing.grouplayout.alignment.baseline) .addcomponent (jLabel3) .addComponent(LoginPwd, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.grouplayout.preferred_size) .addContainGap (21, short.max_value))); embalar(); } // </editor-fold> private void loginnameactionperformed (java.awt.event.actionEvent evt) {// para agregar su código de manejo aquí:} private void jbutton1actionperformed (java.awt.event.actionEvent evt) {// 1. Obtenga el nombre de usuario y la contraseña First String Name = LoginName.gettext (); String contraseña = new String (loginpwd.getPassword ()); // 2. Verifique if ("admin" .equals (name) && "admin" .equals (contraseña)) {// Iniciar sesión con éxito // ocultar la forma de inicio de sesión y mostrar mainform este.setVisible (falso); nuevo MainForm (). SetVisible (True); } else {// Iniciar sesión fallido JOptionPane.ShowMessEdialog (esto, "¡Error de nombre de usuario o contraseña!"); }} private void formkeyPissed (java.awt.event.keyevent evt) {// toque en el teclado para registrarse} private void loginNameKeyPissed (java.awt.event.keyevent evt) {// toque en el teclado a lo que JButton1.doClick (); }} private void loginpwdkeyPissed (java.awt.event.keyevent evt) {// toque el teclado para iniciar sesión if (evt.getKeyText (evt.getKeyCode ()). CompareToEnignReCase ("Enter") == 0) {jbutton1.doclick (); }} / ** * @param args los argumentos de la línea de comandos * / public static void main (string args []) {java.awt.eventqueue.invokelater (new runnable () {public void run () {new Loginform (). SetVisible (true);}}); } // Declaración de variables: no modifique javax.swing.jtextfield loginname; privado javax.swing.jpasswordfield loginpwd; privado javax.swing.jbutton JButton1; privado javax.swing.jlabel jlabel1; privado javax.swing.jlabel jlabel2; privado javax.swing.jlabel jlabel3; // fin de la declaración variable}Mainform.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. *// * * Mainform.java * * creado el 2013-2-26, 18:35:25 */ paquete com.liu.view; / ** * * * @author asus */ public class MainForm extiende javax.swing.jframe {/ ** Crea nueva forma mainform */ public mainform () {initComponents (); } /** Este método se llama desde dentro del constructor para* inicializar el formulario. * Advertencia: no modifique este código. El contenido de este método es * siempre regenerado por el editor de formulario. */ @SupplesSwarnings ("Unchecked") // <Editor-Fold DefaultState = "colapsed" desc = "Código generado"> privado void initComponents () {jlabel1 = new javax.swing.jlabel (); jpanel1 = new javax.swing.jpanel (); jlabel2 = new javax.swing.jlabel (); jMenubar1 = new javax.swing.jmenubar (); Configuration Management = new javax.swing.jmenuitem1 = new javax.swing.jmenuitem (); jMenuitem2 = new Javax.swing.jmenuitem (); jMenu2 = new javax.swing.jmenuitem (); jMenuitem3 = new Javax.swing.jmenuitem (); setDefaultCloseOperation (javax.swing.windowconstants.exit_on_close); settitle ("Sistema de gestión de libros"); setBounds (nuevo Java.awt.rectangle (80, 60, 0, 0)); setiConImage (nuevo javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/logo.jpg")). getImage ()); jlabel1.setFont (nuevo java.awt.font ("安体", 0, 48)); jlabel1.setForeground (nuevo java.awt.color (0, 204, 51)); jlabel1.Seticon (nuevo javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/main.jpg"))); // noi18n jlabel2.setFont (nuevo java.awt.font ("安一", 0, 48)); jlabel2.setForeground (nuevo java.awt.color (0, 0, 255)); jlabel2.settext ("Bienvenido a usar el sistema de gestión de préstamos de libros"); javax.swing.grouplaYout jpanel1Layout = new javax.swing.grouplayout (jpanel1); jpanel1.setLayout (JPanel1Layout); JPanel1Layout.SethorizontalGroup (jPanel1Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (jpanel1layout.createSequentialgroup () .addgap (38, 38, 38) .addcomponent (jlabel2) .addContainGap (63, short.max_value))); jPanel1Layout.SetVerticalGroup (jPanel1Layout.CreateParAllelGroup (javax.swing.grouplaYout.alignment.leading) .addgroup (javax.swing.grouplayOut.alignment.Trailing, jpanel1Layout.createRequentGroup () .AddContainGaP () .addComponent (jlabel2, javax.swing.grouplayout.default_size, 78, short.max_value))); Configuration Management.setText ("Gestión de configuración"); JMenuitem1.SetAccelerator (javax.swing.keystroke.getKeyStroke (java.awt.event.keyevent.vk_l, java.awt.event.inputevent.ctrl_mask)); JMenuitem1.settext ("Gestión de categorías"); jMenuitem1.AdDactionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jMenuitem1actionperformed (evt);}}); Configuration Management.Add (JMenuitem1); jMenuitem2.SetAccelerator (javax.swing.keystroke.getKeyStroke (java.awt.event.keyevent.vk_t, java.awt.event.inputevent.ctrl_mask)); JMenuitem2.settext ("Gestión de libros"); jMenuitem2.AddActionListener (new java.awt.event.actionListener () {public void ActionperFormed (java.awt.event.actionEvent evt) {jMenuitEm2ActionPerformed (evt);}}); Configuration Management.Add (JMenuitem2); JMenubar1.Add (Gestión de configuración); JMenu2.settext ("préstamos"); JMenuitem4.SetAccelerator (javax.swing.keystroke.getKeyStroke (java.awt.event.keyevent.vk_j, java.awt.event.inputevent.ctrl_mask)); JMenuitem4.settext ("arrendamiento"); JMenu2.Add (JMenuitem4); JMenubar1.add (JMenu2); JMenu1.settext ("Regreso al libro"); JMenuitem3.SetAccelerator (javax.swing.keystroke.getKeyStroke (java.awt.event.keyevent.vk_h, java.awt.event.inputevent.ctrl_mask)); JMenuitem3.settext ("Regreso al libro"); JMenu1.add (JMenuitem3); JMenubar1.add (JMenu1); setJMenubar (JMenubar1); javax.swing.grouplayout diseño = new javax.swing.grouplayout (getContentPane ()); getContentPane (). setLayout (diseño); Layout.SethorizontalGroup (Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .addComponent (jlabel1, 0, 0, short.max_value) .ddgroup (Layout.createSeQuencialentSeatial () .addgap (22, 22). javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addContainGap ())); Layout.SetVerticalGroup (Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .AddGroup (javax.swing.grouplayout.alignment.Trailing, Layout.CreateSequentSgroup (). javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.grouplayout.preferred_size))); embalar(); } // </editor-fold> private void jMenuitem1actionperformed (java.awt.event.actionEvent evt) {// type gestión nueva typeform (). setVisible (true); } private void jMenuitem2ActionPerformed (java.awt.event.actionEvent evt) {// book gestión nueva bookform (). setVisible (true); } / ** * @param args los argumentos de la línea de comandos * / public static void main (string args []) {java.awt.eventqueue.invokelater (new runnable () {public void run () {new MainForm (). SetVisible (true);}}); } // Declaración de variables: no modifique el privado javax.swing.jlabel jlabel1; privado javax.swing.jlabel jlabel2; privado javax.swing.jmenu jmenu1; privado javax.swing.jmenuitem JMenuitem2; privado javax.swing.jmenuitem JMenuitem2; Javax.swing privado.jmenuitem JMenuitem3; Javax.swing privado.jmenuitem JMenuitem3; privado javax.swing.jmenuitem JMenuitem4; privado javax.swing.jpanel jpanel1; Javax.swing.jmenu de gestión de configuración de JMenu; // Declaración de finales de variables}Bookform.java
/ * * Para cambiar esta plantilla, elija Herramientas | Plantillas * y abrir la plantilla en el editor. *// * * BookForm.java * * creado el 2013-2-28, 8:23:01 */ paquete com.liu.view; import com.liu.dao.bookdao; import com.liu.dao.typetao; import com.liu.po.bookbean; import com.liu.po.typebean; import java.util.map; import java.util.set; import java.util.vector; import javax.swing.defaultcomboBoxmodel; import javax.swing.JOptionPane; import javax.swing.table.defaultTableModel; / ** * * * @author asus */ public class BookForm extiende javax.swing.jframe {/ ** Crea una nueva forma de bookform */ private map <Integer, bookbean> map; mapa privado <Integer, tipoBean> MAP1; bookdao privado bookdao; Typedao privado typedao; public bookForm () {initComponents (); bookdao = nuevo bookdao (); typedAo = new typedAo (); map = bookdao.readbook (); map1 = typedAo.ReadType (); inittype (); initData (); } /** Este método se llama desde dentro del constructor para* inicializar el formulario. * Advertencia: no modifique este código. El contenido de este método es * siempre regenerado por el editor de formulario. */ @SupplesSwarnings ("sin verificar") // <editor-fold DefaultState = "colapsed" desc = "código generado"> privado void initComponents () {jPanel1 = new javax.swing.jpanel (); jscrollPane1 = new javax.swing.jscrollPane (); booktable = new javax.swing.jtable (); jpanel2 = new javax.swing.jpanel (); bmemo = new javax.swing.jTextField (); jButton1 = new javax.swing.jbutton (); jButton2 = new javax.swing.jbutton (); jButton3 = new javax.swing.jbutton (); jButton4 = new javax.swing.jbutton (); jlabel1 = new javax.swing.jlabel (); jlabel2 = new javax.swing.jlabel (); jlabel3 = new javax.swing.jlabel (); jlabel4 = new javax.swing.jlabel (); bid = new javax.swing.jTextField (); bname = new javax.swing.jTextField (); btype = new javax.swing.jcomboBox (); jlabel5 = new javax.swing.jlabel (); bMoney = new javax.swing.jTextField (); settitle ("gestión de libros"); setBounds (nuevo java.awt.rectangle (100, 50, 0, 0)); setiConImage (nuevo javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/logo.jpg")). getImage ()); booktable.setFont (nuevo java.awt.font ("安一", 0, 18)); // NOI18N bookTable.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null, null}, {null, null, null, null, null}, {null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null, null}, {null, null, null, null, null, null}, {null, null, null, null, null, null}, {null, null, null, null, null, null} }, new String [] { "Serial Number", "Book Name", "Tipo de libro", "Rent", "Observaciones"}) {class [] types = new class [] {java.lang.integer.class, java.lang.string.class, java.lang.string.class, java.lang.string.class, java.lang.class.class. falso, falso, falso}; booktable.addMousElistener (nuevo java.awt.event.Mouseadapter () {public void MouseClicked (java.awt.event.MouseEvent Evt) {booktableMousEclicked (evt);}}); JSCrollPane1.SetViewPortView (Booktable); jpanel2.setborder (javax.swing.borderfactory.createTitledBorder (nulo, "detalles", javax.swing.border.titledborderder.default_justification, javax.swing.border.titledborder.default_position, new java.awt.font ("宋体", 0, 18))); // noi18n bmemo.setFont (nuevo java.awt.font ("宋体", 0, 18)); JButton1.settext ("nuevo"); jButton1.AdDactionListener (new java.awt.event.actionListener () {public void ActionperFormed (java.awt.event.actionEvent evt) {jbutton1actionperformed (evt);}}); jButton2.settext ("Guardar"); jButton2.AddActionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jButton2ActionPerformed (evt);}}); jButton3.settext ("actualización"); jButton3.AddActionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jButton3Actionperformed (evt);}}); JButton4.Settext ("Eliminar"); jButton4.AddActionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jButton4ActionPerformed (evt);}}); jlabel1.setFont (nuevo java.awt.font ("宋体", 0, 18)); jlabel1.settext ("宋体", 0, 18)); jlabel2.settext ("宋体", 0, 18)); jlabel2.settext ("宋体" .font ("宋体", 0, 18)); jlabel3.settext ("宋体", 0, 18)); jlabel3.settext ("宋体", 0, 18)); jlabel4.settext ("宋体"); jlabel4.settext ("宋体"); bid.setFont (new Java.awt.font ("宋体", 0, 18)); bname.setFont (nuevo java.awt.font ("宋体", 0, 18)); btype.setFont (nuevo java.awt.font ("宋体", 0, 18)); btype.setModel (new javax.swing.defaultcomboBoBoxModel (nueva cadena [] {"literatura", "educación", "tecnología", "literatura"})); btype.addactionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {btypeActionperformed (evt);}}); jlabel5.setFont (nuevo java.awt.font ("宋体", 0, 18)); jlabel5.settext ("Rent:"); javax.swing.grouplaYout JPanel2Layout = new Javax.Swing.GrouplaYout (JPanel2); jpanel2.setLayout (JPanel2Layout); JPanel2Layout.SethorizontalGroup (jPanel2Layout.CreateParAllelGroup (javax.swing.grouplayOut.alignment.leading) .addGroup (jPanel2Layout.CreateSequentialGroup () .AddGap (33, 33, 33) .addgroup (jPanel2Layout.CreateParallelgroup (javax.swing.grouplayout.alignment.leading) .addgroup (jpanel2Layout.createSequentialGroup () .addgroup (jPanel2Layout.ceateParallelGroup (javax.swing.greuPlayout.alignment .addComponent (jlabel2, javax.swing.grouplayout.default_size, 83, short.max_value) .addgroup (jpanel2Layout.createparAllelgroup (javax.swing.grouplayout.alignment.trailing) .addcomponent (jlabel3). .addComponent (jlabel4))) .addpreferredgap (javax.swing.layoutstyle.componentPlacone.related)) .addGroup (jpanel2Layout.createSequentialGroup () .addComponent (jlabel1) .addgap (33, 33, 33)))))))))))))))))))))))) .addgroup (jPanel2Layout.CreateParallelgroup (javax.swing.grouplayout.alignment.leading) .addComponent (bmemo, javax.swing.grouplayout.default_size, 375, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) .addComponent(bmoney, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE) .addComponent(bmoney, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.max_value) .addComponent (bMoney, javax.swing.grouplayout.default_size, 375, short.max_value) .addgroup (jPanel2Layout.CreateSequentialGroup () .addComponent (jbutton1, javax.swing.grauplauPlauPlauPreperred, 73333333333333 javax.swing.grouplayout.preferred_size) .addgap (28, 28, 28) .AddComponent (jButton2, javax.swing.grouplayout.preferred_size, 69, javax.swing.grouplayout.preferred_size) .addGap (30, 30, 30). javax.swing.grouplayout.preferred_size, 73, javax.swing.grouplayout.preferred_size) .addpreferredgap (javax.swing.layoutstyle.componentplacation.related, 28, short.max_value) .addcomponent (jbutton4,, plazo, javax.swing.grouplayout.preferred_size, 74, javax.swing.grouplayout.preferred_size)) .addcomponent (btype, 0, 375, short.max_value)) .addgap (65, 65, 65)); jPanel2Layout.SetVerticalGroup (jPanel2Layout.CreateParAllelGroup (javax.swing.grouplaYout.alignment.leading) .addgroup (javax.swing.grouplayout.alignment.trailing, jPanel2Layout.createRequentGroup () .AddContainGaP () .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(bid, javax.swing.grouplayout.preferred_size, 37, javax.swing.grouplayout.preferred_size)) .addgap (13, 13, 13) .addgroup (jpanel2layout.createparaLelgroup (javax.swing.grouplaugleout.alignment.baseline) .addcompenent) .addComponent (Bname, javax.swing.grouplayOut.preferred_size, 37, javax.swing.grouplayOut.preferred_size)) .addgap (18, 18, 18) .addgroup (jpanel2Layout.createparallelgroup (javax.swing.grouPlauPlauPlautment.aleline.aleline) .addComponent (JLabel3) .addComponent (btype, javax.swing.grouplayout.preferred_size, 30, javax.swing.grouplayout.preferred_size)) .addgap (26, 26, 26) .addGroup (jPanel2Layout.CreateParallelGroup (javax.swing.grouplayout.alignment.baseline) .addComponent (jlabel5) .addcomponent (bmoney, javax.swing.grouplaYout.prefered_size, 35, javax.swing.grauPlauPrefers) .addgap (32, 32, 32) .addGroup (jPanel2Layout.CreateParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (jpanel2layout.createSequentalgroup () .addcomponent (bmemo, javax.swing.grouPlauteut.preferRed.preFerRed.preReT 58, javax.swing.grouplayout.preferred_size) .addgap (64, 64, 64) .addGroup (jPanel2Layout.CreateParallelGroup (javax.swing.grouplayout.alignment.baseline) .addcomponent (jbutton1, javax.swing.greguredseline). 32, javax.swing.grouplayout.preferred_size) .addComponent (jButton2, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size) .addcomponent (jbutton3, javax.swing.gregured_size). 31, javax.swing.grouplayout.preferred_size) .addComponent (jButton4, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size))) .addcomponent (jlabel4)) .addgap (22, 22, 22); javax.swing.grouplaYout jpanel1Layout = new javax.swing.grouplayout (jpanel1); jpanel1.setLayout (JPanel1Layout); jPanel1Layout.SethorizontalGroup (jPanel1Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .addcomponent (jscrollpane1, javax.swing.grouplayout.alignment.trailing, javax.swing.grauPlayout.DeFaul, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, 795. Short.max_value) .addgroup (jpanel1Layout.createSequentialGroup () .addgap (50, 50, 50) .addcomponent (jpanel2, javax.swing.grouplayOut.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addContainGap (173, short.max_value))); jPanel1Layout.SetVerticalGroup (jPanel1Layout.CreateParAllelGroup (javax.swing.grouplaYout.alignment.leading) .addgroup (jpanel1Layout.createSequentialGroup () .AddComponent (jscrollpane1, javax.swing.grauPlayout.prepreperder javax.swing.grouplayout.preferred_size) .addgap (10, 10, 10) .addcomponent (jpanel2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value))); javax.swing.grouplayout diseño = new javax.swing.grouplayout (getContentPane ()); getContentPane (). setLayout (diseño); Layout.SethorizontalGroup (Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .addComponent (jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value); Layout.SetVerticalGroup (Layout.CreateParAllelGroup (javax.swing.grouplayout.alignment.leading) .addComponent (jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)); embalar(); } // </editor-fold> // Inicialización de los datos de tabla public void initData () {// Cargando datos defaultTableModel dtm = (defaultTableModel) booktable.getModel (); // Borrar tabla while (dtm.getrowCount ()> 0) {dtm.removerow (0); } // Cargue el conjunto de datos <Integer> set = map.KeySet (); for (integer i: set) {bookbean bean = map.get (i); Vector v = nuevo vector (); V.Add (bean.getId ()); V.Add (bean.getBookName ()); V.Add (bean.getBookType ()); V.Add (bean.getMoney ()); V.Add (bean.getMemo ()); dtm.addrow (v); }} // Obtenga todas las categorías de gestión de categorías public void inittype () {set <integer> set = map1.keyset (); DefaultComboBoxModel dcm = (defaultComboBoxModel) btype.getModel (); dcm.removeAllelements (); for (integer i: set) {typeBean Bean = map1.get (i); dcm.addelement (bean.gettypename ()); }} private void jbutton2actionperformed (java.awt.event.actionEvent evt) {// guardar función // Guardar el valor en el cuadro de texto First String id = bid.gettext (); String BookName = bname.gettext (); String booktype = (string) btype.getSelectedItem (); String Memo = bmemo.gettext (); String Money = bMoney.gettext (); // bordado en un objeto bookbean bean = new BookBean (); bean.setID (integer.parseInt (id)); Bean.setBookName (BookName); Bean.SetBookType (BookType); frijoles.setmemo (memo); Bean.setMoney (dinero); // colocar el bean en el mapa // map <integer, typeBean> map = new Hashmap <Integer, typeBean> (); map.put (integer.parseInt (id), bean); // colocar el mapa en el archivo bookdao.writeBook (mapa); // Actualizar la tabla initData (); } private void jbutton1actionperformed (java.awt.event.actionEvent evt) {// Cuando se agrega, borre la información en el cuadro de texto y establezca el número de serie en editable. bid.SetEnabled (verdadero); bid.settext (""); bname.settext (""); btype.setselectedItem (""); bmemo.settext (""); bMoney.settext (""); } private void booktableMouseClicked (java.awt.event.mouseEvent evt) {// obtenga el número de línea seleccionado y el número de serie int cententRow = bookTable.getSelectedRow (); // bookbean bean = map.get (currentRow); // Mostrar la línea seleccionada a la barra de información Bid.Settext ((Integer) BookTable.getValueat (CurrentRow, 0)+""); bname.settext ((string) bookTable.getValueat (CurrentRow, 1)); btype.setselectedItem ((string) booktable.getValueat (CurrentRow, 2)); bMoney.settext ((String) Booktable.getValueat (CurrentRow, 3)); bmemo.settext ((string) booktable.getValueat (CurrentRow, 4)); //bmemo.settext (bean.getMemo ()); bid.SetEnabled (falso); //The serial number box cannot be edited} private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // Delete operation// Get the selected row number and serial number int currentRow = bookTable.getSelectedRow(); int id = (Integer)bookTable.getValueAt(currentRow, 0); map.remove(id); bookDao.writeBook(map); JOptionPane.showMessageDialog(this,"Category delete successful"); initData(); } private void btypeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // Update operation// Save the value in the text box first int currentRow = bookTable.getSelectedRow(); int id = (Integer) bookTable.getValueAt(currentRow, 0); String bookName = bname.getText(); String bookType = (String) btype.getSelectedItem(); String memo = bmemo.getText(); String money = bmoney.getText(); //Embroidered into an object BookBean bean = new BookBean(); bean.setId(id); bean.setBookName(bookName); bean.setBookType(bookType); bean.setMemo(memo); bean.setMoney(money); //Put the bean into the map// Map<Integer,TypeBean>map = new HashMap<Integer,TypeBean>(); map.put(id, bean); //Put the map into the file bookDao.writeBook(map); //Refresh table JOptionPane.showMessageDialog(this,"Category update succeeded"); initData(); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new BookForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField bid; private javax.swing.JTextField bmemo; private javax.swing.JTextField bmoney; private javax.swing.JTextField bname; private javax.swing.JTable bookTable; private javax.swing.JComboBox btype; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }TypeForm.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * TypeForm.java * * Created on 2013-2-26, 19:07:51 */ package com.liu.view; import com.liu.dao.TypeDAO; import com.liu.po.TypeBean; import java.util.hashmap; import java.util.Map; import java.util.Set; import java.util.Vector; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; /** * * @author asus */ public class TypeForm extends javax.swing.JFrame { private TypeDAO typeDao; private Map<Integer,TypeBean> map; /** Creates new form TypeForm */ public TypeForm() { initComponents(); typeDao = new TypeDAO(); map = typeDao.readType(); initData(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); typeTable = new javax.swing.JTable(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); tid = new javax.swing.JTextField(); tname = new javax.swing.JTextField(); jScrollPane2 = new javax.swing.JScrollPane(); tmemo = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); setTitle("Type Management"); setBounds(new java.awt.Rectangle(100, 50, 0, 0)); setIconImage(new javax.swing.ImageIcon(getClass().getResource("/com/liu/resouce/logo.jpg")).getImage()); typeTable.setFont(new java.awt.Font("安体", 0, 18)); typeTable.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null} }, new String [] { "Serial Number", "Category Name", "Remarks" } ) { Class[] types = new Class [] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class }; boolean[] canEdit = new boolean [] { false, false, false }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); typeTable.setColumnSelectionAllowed(true); typeTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { typeTableMouseClicked(evt); } }); typeTable.addContainerListener(new java.awt.event.ContainerAdapter() { public void componentAdded(java.awt.event.ContainerEvent evt) { typeTableComponentAdded(evt); } }); jScrollPane1.setViewportView(typeTable); typeTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); typeTable.getColumnModel().getColumn(0).setResizable(false); typeTable.getColumnModel().getColumn(2).setResizable(false); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Category Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 18))); // NOI18N jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); jLabel1.setText("宋体:"); jLabel2.setFont(new java.awt.Font("宋体", 0, 18)); jLabel2.setText("Category name:"); jLabel3.setFont(new java.awt.Font("宋体", 0, 18)); jLabel3.setText("宋体".tid.setFont(new java.awt.Font("宋体", 0, 18)); tid.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tidActionPerformed(evt); } }); tname.setFont(new java.awt.Font("宋体", 0, 18)); tmemo.setColumns(20); tmemo.setFont(new java.awt.Font("Monospaced", 0, 18)); tmemo.setRows(5); jScrollPane2.setViewportView(tmemo); jButton1.setText("Save"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton3.setText("Update"); jButton3.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton3MouseClicked(evt); } }); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Delete"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("New"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tname, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)) .addGroup(jPanel2Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(tid, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))))) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(70, 70, 70) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(41, 41, 41) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(37, 37, 37) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20))) .addGap(83, 83, 83)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(31, 31, 31) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tid, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tname, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(45, 45, 45) .addComponent(jLabel3)) .addGroup(jPanel2Layout.createSequentialGroup() .addGap(24, 24, 24) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(35, 35, 35) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18)) ); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(66, 66, 66) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(55, Short.MAX_VALUE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); embalar(); }// </editor-fold> // Initialization of table data public void initData(){ //Loading data DefaultTableModel dtm = (DefaultTableModel)typeTable.getModel(); //Clear table while(dtm.getRowCount()>0){ dtm.removeRow(0); } //Load data Set<Integer>set = map.keySet(); for(Integer i:set){ TypeBean bean = map.get(i); Vector v = new Vector(); v.add(bean.getId()); v.add(bean.getTypeName()); v.add(bean.getMemo()); dtm.addRow(v); } } private void tidActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //Save type operation//Save the value in the text box first String id = tid.getText(); String typeName = tname.getText(); String memo = tmemo.getText(); //Encapsulate it into an object TypeBean bean = new TypeBean(); bean.setId(Integer.parseInt(id)); bean.setMemo(memo); bean.setTypeName(typeName); //Put the bean into the map// Map<Integer,TypeBean>map = new HashMap<Integer,TypeBean>(); map.put(Integer.parseInt(id), bean); //Put the map into the file typeDao.writeType(map); //Refresh table initData(); } private void typeTableComponentAdded(java.awt.event.ContainerEvent evt) { // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // Delete operation// Get the selected row number and sequence number int currentRow = typeTable.getSelectedRow(); int id = (Integer)typeTable.getValueAt(currentRow, 0); map.remove(id); typeDao.writeType(map); JOptionPane.showMessageDialog(this,"Category delete successful"); initData(); } private void jButton3MouseClicked(java.awt.event.MouseEvent evt) { } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // Update operation // Save the value in the text box first int currentRow = typeTable.getSelectedRow(); int id = (Integer) typeTable.getValueAt(currentRow, 0); String typeName = tname.getText(); String memo = tmemo.getText(); // Encapsulate it into an object TypeBean bean = new TypeBean(); bean.setId(id); bean.setMemo(memo); bean.setTypeName(typeName); //Put the bean into the map// Map<Integer,TypeBean>map = new HashMap<Integer,TypeBean>(); map.put(id, bean); //Put the map into the file typeDao.writeType(map); //Refresh table JOptionPane.showMessageDialog(this,"Category update successful"); initData(); } private void typeTableMouseClicked(java.awt.event.MouseEvent evt) { //Get selected line number and serial number int currentRow = typeTable.getSelectedRow(); // Display the selected row in the information bar tid.setText((Integer) typeTable.getValueAt(currentRow, 0)+""); tname.setText((String) typeTable.getValueAt(currentRow, 1)); tmemo.setText((String) typeTable.getValueAt(currentRow, 2)); tid.setEnabled(false); // Serial number box cannot be edited} private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { // When added, clear the information in the text box and set the serial number to editable. tid.setEnabled(true); tid.setText(""); tname.setText(""); tmemo.setText(""); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TypeForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextField tid; private javax.swing.JTextArea tmemo; private javax.swing.JTextField tname; private javax.swing.JTable typeTable; // End of variables declaration }Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.