Os exemplos deste artigo compartilham com você o código específico de Java para implementar o sistema de gerenciamento da biblioteca para sua referência. O conteúdo específico é o seguinte
/* (O comentário do cabeçalho do programa começa)
* Declaração de direitos autorais e versão parte do programa
* Copyright (c) 2011, Escola de Ciência da Computação da Universidade de Yantai
* Todos os direitos reservados.
* Nome do arquivo: "Sistema de gerenciamento de livros-Java"
* Autor: Liu Jiangbo
* Data de conclusão: 1 de março de 2012
* Número da versão: v3.0
* Descrição de tarefas e soluções
* Descrição da pergunta:
* O fim dos comentários à frente do programa
*/
Status da criação do pacote de arquivos:
Bookdao.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. */ pacote com.liu.dao; importação com.liu.po.bookbean; importar java.io.*; importar java.util.hashmap; importar java.util.map; importar java.util.logging.level; importar java.util.logging.logger; / ** * * @Author Asus */ public class BookDao {// Escreva public void Writebook (mapa <Integer, bookbean> bookmap) {// fileOutputStream fos = null; ObjectOutputStream ooS = null; tente {fos = new FileOutputStream ("f: //cache//book.txt"); OOS = new ObjectOutputStream (Fos); OOS.WriteObject (BookMap); // limpe o cache oos.flush (); } catch (filenotfoundException ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); } // O nível de exceção está no final da captura (ioException ex) {Logger.getLogger (typeDao.class.getName ()). Log (Level.severe, null, ex); } finalmente {tente {// Abra e feche ooS.Close (); fos.close (); } catch (ioexception ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); }}} // Leia o mapa público <Inteiro, BookBean> readbook () {FileInputStream fis = null; ObjectInputStream ois = null; Mapa <inteiro, bookbean> map = null; tente {fis = new FileInputStream ("f: //cache//book.txt"); OIS = new ObjectInputStream (fis); map = (mapa <inteiro, bookbean>) ois.readObject (); // A exceção ocorre e entra Catch} Catch (classNotFoundException ex) {Logger.getLogger (typeDao.class.getName ()). log (Level.sevee, null, ex); } catch (filenotfoundException ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); } catch (ioexception ex) {//logger.getLogger(typedao.class.getname().Log(Level.severe, null, ex); //ex.printStackTrace (); map = novo hashmap <inteiro, bookbean> (); // crie map} finalmente {try {if (ois! = null) {ois.close (); } if (fis! = null) {fis.close (); }} catch (ioexception ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); }} retornar mapa; }}Typedao.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. */ pacote com.liu.dao; importação com.liu.po.typebean; importar java.io.*; importar java.util.*; importar java.util.logging.*; / **** Leia e escreva arquivos*/ public class typeDao {// grava public void writeType (map <Integer, typeBean> typeMap) {// fileOutputStream fos = null; ObjectOutputStream ooS = null; tente {fos = new FileOutputStream ("f: //cache//type.txt"); OOS = new ObjectOutputStream (Fos); OOS.WriteObject (TypeMap); // limpe o cache oos.flush (); } catch (filenotfoundException ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); } // O nível de exceção está no final (ioexception ex) {Logger.getLogger (typeDao.class.getName ()). Log (Level.severe, null, ex); } finalmente {tente {// Abra primeiro e depois feche ooS.Close (); fos.close (); } catch (ioexception ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); }}} // Leia o mapa público <Inteiro, typeBean> readType () {FileInputStream fis = null; ObjectInputStream ois = null; Mapa <inteiro, typeBean> map = null; tente {fis = new FileInputStream ("f: //cache//type.txt"); OIS = new ObjectInputStream (fis); map = (mapa <inteiro, typeBean>) ois.readObject (); // A exceção ocorre e entra Catch} Catch (classNotFoundException ex) {Logger.getLogger (typeDao.class.getName ()). Log (Level.sevee, null, ex); } catch (filenotfoundException ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); } catch (ioexception ex) {//logger.getLogger(typedao.class.getname().Log(Level.severe, null, ex); //ex.printStackTrace (); map = new Hashmap <Inteiro, TypeBean> (); // Crie mapa} finalmente {try {if (ois! = null) {ois.close (); } if (fis! = null) {fis.close (); }} catch (ioexception ex) {logger.getLogger (typeDao.class.getName ()). log (Level.severe, null, ex); }} retornar mapa; }}}Bookbean.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. */ pacote com.liu.po; importar java.io.serializable; / ** * * @author asus */ public classe bookbean implementa serializável {private int id; nome de bookname de string privado; String Private Booktype; Memorando Private String; dinheiro privado de cordas; / ** * @return the id */ public int getId () {return id; } / ** * @param id o id para definir * / public void setId (int id) {this.id = id; } / ** * @return the bookname * / public string getBookName () {return bookName; } / ** * @param bookName o bookname para set * / public void SetBookName (string bookName) {this.BookName = bookName; } / ** * @return the booktype * / public string getBooktype () {return booktype; } / ** * @param booktype the booktype para definir * / public void SetbookType (String booktype) {this.booktype = booktype; } / ** * @RETURN O memorando * / public String getMemo () {return memorando; } / ** * @param memorando o memorando para definir * / public void setMemo (string memorando) {this.memo = memorando; } / ** * @return the Money * / public String getMoney () {Return Money; } / ** * @param dinheiro o dinheiro para definir * / public void setMoney (String Money) {this.money = Money; }}TypeBean.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. */ pacote com.liu.po; / ** * * @Author Asus */ importar java.io.Serializable; classe pública TypeBean implementa serializável {private int id; nome de string privado; Memorando Private String; / ** * @return the id */ public int getId () {return id; } / ** * @param id o id para definir * / public void setId (int id) {this.id = id; } / ** * @RETURN O nome do tipo * / public String getTypeName () {return typename; } / ** * @param typename o nome do tipo de tipo * / public void setTypeName (string typename) {this.typeName = typename; } / ** * @RETURN O memorando * / public String getMemo () {return memorando; } / ** * @param memorando o memorando para definir * / public void setMemo (string memorando) {this.memo = memorando; }}Loginform.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. *// * * Loginform.java * * criado em 2013-2-26, 18:33:36 */ package com.liu.view; importar java.awt.event.keyadapter; importar java.awt.event.keyevent; importar javax.swing.JOptionPane; / ** * * @Author Asus */ public class Loginform estende javax.swing.jframe {/ ** cria novo formulário loginform */ public Loginform () {initComponents (); } /** Este método é chamado de dentro do construtor para* inicializar o formulário. * Aviso: não modifique este código. O conteúdo deste método é * sempre regenerado pelo editor de formulários. */ @Suppresswarnings ("desmarcado") // <editor-Fold defaultState = "colapsed" desc = "generated code"> private 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 ("Interface de login"); Setbounds (New Java.awt.rectangle (300, 200, 0, 0)); seticonImage (new 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) {FormkeyPressed (evt);}}); jlabel1.setFont (New java.awt.font ("宋体", 0, 36)); jlabel1.setforeground (New java.awt.color (204, 0, 0)); jlabel1.settext ("宋体"); jlabel2.setFont (new java.awt.font ("宋体", 0, 24)); jlabel2.settext ("nome de usuário:"); jlabel3.setFont (New Java.awt.font ("宋体", 0, 24)); jlabel3.settext ("senha:"); Loginname.setName (""); // noi18n loginname.addactionListener (new java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {loginnameactionPeformed (evt);}}); Loginname.addkeyListener (new java.awt.event.keyadapter () {public void KeyPressed (java.awt.event.keyevent evt) {loginNameKeyPressed (EVT);}}); Loginpwd.addkeyListener (novo java.awt.event.keyadapter () {public void KeyPressed (java.awt.event.keyevent evt) {loginpwdkeypressed (evt);}}); jbutton1.setFont (New 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 layout = new javax.swing.grouplayout (getContentPane ()); getContentPane (). setLayout (layout); Layout.SethorizontalGroup (layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (layout.createsequentialgroup () .AdLellaGap (97, 97, 97) .Addroup (layout.retParallelGap (javrax (97). .AddComponent (Jlabel2) .AddComponent (Jlabel3)) .AddPreferredGap (javax.swing.layoutStyle.comPonsPlacement.UnRelated) .AddGroup (layout.CreateParallelGroup (javax.swing.GrouPlayout.alignment.LeAng.LeAt.ComEngouling (Javax). .addComponent(LoginName, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)) .addContainerGap(88, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(130, Short.max_value) .addComponent (jlabel1, javax.swing.grouplayout.preferred_size, 263, javax.swing.GrouPlayout.Preferred_Size) .addgap (113, 113)) .addGroup (javaxing). layout.createsequentialgroup () .addContAineRgap (299, short.max_value) .addComponent (jbutton1, javax.swing.grouplayout.preferred_size, 97, javax.swing.GrouPlayout.Prered_size) .DGAP (97, 11, 11, 11, 11, 11, 11, 11, 11). 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) .adddcomponent (LoginLame) .addComponent (jlabel2) .AddConspon (Loginame), javax.swing.grouplayout.preferred_size, 29, javax.swing.grouplayout.preferred_size)) .addgap (26, 26, 26) .addgroup (Layout.createParallelGroup (javax.doming.grouplayout.alignment.basel). .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) .addContAineRergap (21, short.max_value))); pacote(); } // </editor-Fold> private void LoginnameActionPelemed (java.awt.event.actionEvent evt) {// TODO Adicione seu código de manuseio aqui:} private void jbutton1ActionPerformed (java.awt.event.actionEvent Evt) {// 1. Obtenha o nome de usuário e a senha primeiro nome da string = loginName.getText (); String senha = new String (loginpwd.getpassword ()); // 2. Verifique se ("admin" .equals (nome) && "admin" .equals (senha)) {// login com sucesso // Ocultar Loginform e mostre Mainform this.setVisible (false); new mainform (). setvisible (true); } else {// Login falhou JoptionPane.ShowMessAgedialog (isto, "Nome de usuário ou erro de senha!"); }} private void formKeyPressed (java.awt.event.keyevent evt) {// Toque no teclado para fazer login} private void loginNameKeyPressed (java.awt.event.keyevent evt) {// toque no teclado para login (evTeyAtETeTxT (evt.get.get.get.get.GeT.GeT.GeT.GeT.Teet (eVTETETET) (eVTET.GeTeet.EnkeT.Teet.GeTeet (eVTET) {// Toque no teclado para login if (eVTeTeTeTeTT (evt.get.get.get.get.get.GeT.GeT.TeT.EnTeet. jbutton1.doclick (); }} private void loginpwDeyPressed (java.awt.event.keyevent evt) {// Toque no teclado para fazer login if (evt.getKeyText (evt.getKeyCode ()). CompareToignorecase ("Enter") == 0) {jbutton1.doclick (); }} / ** * @param args os argumentos da linha de comando * / public static void main (string args []) {java.awt.eventqueue.invokelater (new runnable () {public void run () {new Loginform (). Setvisible (true);}}); } // Declaração de variáveis - não modifique o Javax.swing.jtextfield LoginName privado javax.swing.jtextfield; private javax.swing.jpasswordfield Loginpwd; private javax.swing.jbutton jbutton1; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jlabel jlabel3; // Fim da Declaração Variável}Mainform.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. *// * * Mainform.java * * criado em 2013-2-26, 18:35:25 */ package com.liu.view; / ** * * @author asus */ public class Mainform estende Javax.swing.jframe {/ ** cria novo formulário MainForm */ public MainForm () {initComponents (); } /** Este método é chamado de dentro do construtor para* inicializar o formulário. * Aviso: não modifique este código. O conteúdo deste método é * sempre regenerado pelo editor de formulários. */ @Suppresswarnings ("desmarcado") // <editor-Fold defaultState = "colapsed" desc = "generated code"> private void initComponents () {jlabel1 = new javax.swing.jlabel (); jpanel1 = new javax.swing.jpanel (); jlabel2 = new javax.swing.jlabel (); jmenubar1 = new javax.swing.jmenubar (); Gerenciamento de configuração = 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 Gerenciamento de Livros"); Setbounds (New Java.awt.rectangle (80, 60, 0, 0)); seticonImage (new javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/logo.jpg"). getImage ()); jlabel1.setFont (New Java.awt.font ("安体", 0, 48)); jlabel1.setforeground (New java.awt.color (0, 204, 51)); jlabel1.seticon (new javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/main.jpg"))); // noi18n jlabel2.setFont (new java.awt.font ("安一", 0, 48)); jlabel2.setforeground (New java.awt.color (0, 0, 255)); jlabel2.settext ("Bem -vindo a usar o sistema de gerenciamento de empréstimos de livros"); javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout (jpanel1); jpanel1.setLayout (jpanel1layout); JPanel1layout.SethorizontalGroup (jpanel1layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addGroup (jpanel1layout.CreatEsequentRoup () .AddGAP (38, 38, 38) .Ad. .AddContAineRergap (63, Short.max_value))); JPanel1layout.SetverticalGroup (jpanel1layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (javax.swing.grouplayout.alignment.trailing, jpaNel1layout.createntyGrearPere (). .AddComponent (jlabel2, javax.swing.grouplayout.default_size, 78, short.max_value))); Configuration Management.settext ("Gerenciamento de configuração"); jmenuitem1.setaccelerator (javax.swing.keyystroke.getKeyTroke (java.awt.event.keyevent.vk_l, java.awt.event.inputevent.ctrl_mask)); jmenuitem1.settext ("gerenciamento de categorias"); jmenuitem1.addactionListener (New java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jmenuitem1ActionPerformed (evt);}}); Gerenciamento de configuração.add (jmenuitem1); jmenuitem2.setAccelerator (javax.swing.keystroke.getKeyTroke (java.awt.event.keyevent.vk_t, java.awt.event.inputevent.ctrl_mask)); jmenuitem2.settext ("gerenciamento de livros"); jmenuitem2.addactionListener (New java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jmenuitem2ActionPerformed (evt);}}); Gerenciamento de configuração.add (jmenuitem2); jmenubar1.add (gerenciamento de configuração); jmenu2.settext ("empréstimo"); jmenuitem4.setaccelerator (javax.swing.keystroke.getKeyTroke (java.awt.event.keyevent.vk_j, java.awt.event.inputevent.ctrl_mask)); jmenuitem4.settext ("leasing"); jmenu2.add (jmenuitem4); jmenubar1.add (jmenu2); jmenu1.settext ("retornar ao livro"); jmenuitem3.setAccelerator (javax.swing.keyystroke.getKeyTroke (java.awt.event.keyevent.vk_h, java.awt.event.inputevent.ctrl_mask)); jmenuitem3.settext ("retornar ao livro"); jmenu1.add (jmenuitem3); jmenubar1.add (jmenu1); setjmenubar (Jmenubar1); javax.swing.grouplayout layout = new javax.swing.grouplayout (getContentPane ()); getContentPane (). setLayout (layout); Layout.SethorizontalGroup (Layout.CreateParallelGroup (javax.swing.grouplayout.alignment.leading) .addcomponent (jlabel1, 0, 0, short.max_value) .addgroup (layout.createsenedEver. javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addContAineRergap ())); layout.setverticalGroup (layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (javax.swing.grouplayout.alignment.Trailing, layout.createSquentialGroup () .addcontAinGap (). 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))); pacote(); } // </editor-Fold> private void jmenuitem1ActionPerformed (java.awt.event.actionEvent evt) {// Tipo de gerenciamento new tipeForm (). setvisible (true); } private void jmenuitem2ActionPerformed (java.awt.event.actionEvent evt) {// gerenciamento de livros new bookform (). setvisible (true); } / ** * @param args os argumentos da linha de comando * / public static void main (string args []) {java.awt.eventqueue.invokelater (new runnable () {public void run () {new mainform (). setvisible (true);}}); } // Declaração de variáveis - não modifique Javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jmenu jmenu1; private javax.swing.jmenuitem jmenuitem2; private javax.swing.jmenuitem jmenuitem2; private javax.swing.jmenuitem jmenuitem3; private javax.swing.jmenuitem jmenuitem3; private javax.swing.jmenuitem jmenuitem4; Private Javax.swing.jpanel JPanel1; private javax.swing.jmenu Gerenciamento de configuração; // Declaração do fim das variáveis}Bookform.java
/ * * Para alterar este modelo, escolha Ferramentas | Modelos * e abra o modelo no editor. *// * * Bookform.java * * criado em 2013-2-28, 8:23:01 */ package com.liu.view; importação com.liu.dao.bookdao; importação com.liu.dao.typedao; importação com.liu.po.bookbean; importação com.liu.po.typebean; importar java.util.map; importar java.util.set; importar java.util.Vector; importar javax.swing.defaultComBoboxModel; importar javax.swing.JOptionPane; importar javax.swing.table.defaultTableModel; / ** * * @author asus */ public class BookForm estende javax.swing.jframe {/ ** cria nova forma bookform */ mapa privado <inteiro, bookbean> mapa; Mapa privado <Inteiro, TypeBean> map1; Bookdao Private Bookdao; TypeDao privado typeDao; public bookForm () {initComponents (); bookdao = novo bookdao (); typeDao = new typeDao (); map = bookdao.readbook (); map1 = typeDao.readType (); inittype (); initdata (); } /** Este método é chamado de dentro do construtor para* inicializar o formulário. * Aviso: não modifique este código. O conteúdo deste método é * sempre regenerado pelo editor de formulários. */ @Suppresswarnings ("desmarcado") // <editor-Fold defaultState = "colapsed" desc = "code gerado"> private 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 ("Gerenciamento de livros"); Setbounds (New Java.awt.rectangle (100, 50, 0, 0)); seticonImage (new javax.swing.imageicon (getClass (). getResource ("/com/liu/resouce/logo.jpg"). getImage ()); bookTable.setFont (New 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 livro", "Rent", "Observações"}) {classe [] Types = nova classe [] {java.lang.integer.class, java.lang.string.class, java.lang.string.class, java.lang.string.class, java.lang.string.class, FALSO, FALSO, FALSO}; bookTable.addmouselistener (New java.awt.event.mouseAdapter () {public void mouseclicked (java.awt.event.mouseevent evt) {booktableMouseClicked (evt);}}); jscrollPane1.setViewportView (BookTable); jpanel2.setborder (javax.swing.borderfactory.createtitledborder (null, "detalhes", javax.swing.border.titledborder.default_justification, javax.swing.border.titledborder.Default_position, New java.awt.font.font ("IFAUSTDDORDER.DEFAULT_POSIÇÃO, Java.Wing.Font.Font.n; // noi18n bmemo.setFont (New Java.awt.font ("宋体", 0, 18)); jbutton1.settext ("new"); jbutton1.addactionListener (New java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jbutton1ActionPerformed (Evt);}}); jbutton2.settext ("salvar"); jbutton2.addactionListener (New java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {jbutton2ActionPerformed (evt);}}); jbutton3.settext ("update"); 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);}}); jlabel1.setFont (New 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 (New Java.awt.font ("宋体", 0, 18)); btype.setFont (New Java.awt.font ("宋体", 0, 18)); btype.setModel (new javax.swing.defaultComboBoxModel (new String [] {"literatura", "educação", "tecnologia", "literatura"})); btype.addactionListener (New java.awt.event.actionListener () {public void ActionPerformed (java.awt.event.actionEvent evt) {btypeActionPormed (evt);}}); jlabel5.setFont (New 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.createsequential () .addgap (33, 33, 33) .Addgroup (jpanel2layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (jpanel2layout.createLalelalGealToup () .addgroup (jpanel2layout.createparalelGroup (javrax.). .AddComponent (jlabel2, javax.swing.grouplayout.default_size, 83, short.max_value) .addgroup (jpanel2layout.createParallelGroup (javax.swing.grouplaout.alignment.Trailing) .adDomponent (jAbcomMcom5 (jAbcomMom3) (jAbcomMom3 (jAbcomMom3) (jAbcomCom3) (jAbcomCom3) (jAbcomCom3) (jAbcomCom3) (jAbcomCom3) (jAbcomM3) (jAbcomM3) (jAbcomM3). .addComponent(jLabel4))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.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, short.max_value) .addcomponior (BMONENCENCONGENSE (375, short.max_value). 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.GroupLayout.PREFERRED_SIZE, 73, javax.swing.grouplayout.preferred_size) .addgap (28, 28, 28) .addComponent (jbutton2, javax.swing.grouplayout.preferred_size, 69, javax.swing.GrouPlayout.preferred_size). javax.swing.grouplayout.preferred_size, 73, javax.swing.grouplayout.preferred_size) .addPreferredGap (javax.swing.layoutstyle.comPentpling.related, short.max_value). javax.swing.grouplayout.preferred_size, 74, javax.swing.grouplayout.preferred_size)) .addComponent (btype, 0, 375, curto.max_value)) .addgap (65, 65, 65)); jpanel2layout.setverticalGroup (jpanel2layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (javax.swing.grouplayout.alignment.trailing, jpanel2layout.createntalGeROGROPRET. .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.createparloup (javax.swing.GROPLAYOUT.ALIGLENT.CREATEPARLOUPROUP ( .AddComponent (bname, javax.swing.grouplayout.preferred_size, 37, javax.swing.grouplayout.preferred_size)) .addGap (18, 18, 18) .addgroup (jpanel2layout.createparalelgroux (javoux). .AddComponent (Jlabel3) .AddComponent (btype, javax.swing.grouplayout.preferred_size, 30, javax.swing.grouplayout.preferred_size)) .addgap (26, 26) .Addgroup (jpanel2layout.createParallelGroup (javax.swing.grouplayout.alignment.baseline) .addcomponent (jlabel5) .addComponent (bmoney, javax.swing.grouplayout.re )eoud_size, 35, .Addgap (32, 32, 32) .Addgroup (jpanel2layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addGroup (JPanel2layout.creatSlup () .AddComponent (BMMOMO,, JPanel2layout.creatSlup (). 58, javax.swing.grouplayout.preferred_size) .addgap (64, 64, 64) .addgroup (jpanel2layout.createParallelGroup (javax.swing.grouplayout.align.ment.baseline) .addcomponent ( 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, Javax.swing.grouplayout.preferred_size) .addComponent (jbutton4, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size)) .addComponent (Jlabel4)). 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.GroupLayout.DEFAULT_SIZE, 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) .addContainergap (173, curto.max_value))); jpanel1layout.setverticalGroup (jpanel1layout.createParallelGroup (javax.swing.grouplayout.alignment.leading) .addgroup (jpanel1layout.createncialGroup () .addCoMON (jscrollPray1, javax.swing.grouplayout.preferred_size) .addgap (10, 10, 10) .addComponent (jpanel2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_ue); 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.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.Default.Default_Size, javax.swing.GrouPlayout.Default.Default. pacote(); } // </editor-fold> // inicialização de dados da tabela public void initData () {// carregando dados defaultTableModEl dtm = (defaultTableModel) bookTable.getModel (); // Limpe a tabela while (dtm.getRowCount ()> 0) {dtm.Removerow (0); } // Carregar conjunto de dados <Teger> set = map.keyset (); para (Inteiro I: set) {bookbean bean = map.get (i); Vetor V = new Vector (); v.add (bean.getId ()); v.add (bean.getbookName ()); v.add (bean.getbookType ()); v.add (bean.getMoney ()); v.add (bean.getMemo ()); dtm.addrow (v); }} // obtenha todas as categorias de gestão de categoria public void inittype () {set <Teger> set = map1.keySet (); DefaultComBOBoxModel dcm = (defaultComBOBoxModel) btype.getModel (); DCM.Removeallements (); para (número inteiro i: set) {typeBean bean = map1.get (i); dcm.adDelement (bean.getTypeName ()); }} private void jbutton2ActionPerformed (java.awt.event.actionEvent evt) {// salvar função // salvar o valor na caixa de texto primeiro string id = bid.getText (); String bookName = bname.getText (); String booktype = (string) btype.getSelectedItem (); String memorando = bmemo.getText (); String money = bmoney.getText (); // bordado em um bookbean bean = new bookbean (); bean.setId (Integer.parseint (id)); bean.setbookName (bookname); Bean.setBooktype (Booktype); bean.setMemo (memorando); bean.setMoney (dinheiro); // Coloque o feijão no mapa // mapa <inteiro, typeBean> map = new hashmap <Integer, typeBean> (); map.put (Integer.parseint (id), feijão); // coloque o mapa no arquivo bookdao.writebook (mapa); // tabela de atualização initData (); } private void jbutton1ActionPerformed (java.awt.event.actionEvent evt) {// Quando adicionado, limpe as informações na caixa de texto e defina o número de série como editável. bid.setEnabled (true); bid.settext (""); bname.settext (""); btype.setSelectedItem (""); bMemo.settext (""); bmoney.settext (""); } private void bookTableMouseClicked (java.awt.event.mouseevent evt) {// Obtenha o número da linha selecionado e o número de série int currentRow = bookTable.getSelectReectedRow (); // bookbean bean = map.get (currentRow); // Mostra a linha selecionada para a barra de informações 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 (false); //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; importar java.util.hashmap; importar 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)) ); pacote(); }// </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 }O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.