问题描述
我使用java做课程设计图书管理系统,建立一个主窗口,然后点击上面的书籍搜索按钮,弹出一个窗口进行图书搜索,然后关闭子窗口。再次打开这个窗口的时候,上面显示的还是上次我进行搜索过的信息。求高手帮助,因为我的代码实在太过于庞大,所以只能这么描述一下了。拜托大家了,真的有点着急了。
解决方案
解决方案二:
你所谓关闭子窗口是否只是setVisible(false)?之后你再次打开是setVisible(true)这样子肯定是保持上次状态嘛你关闭的时候直接把这个窗口dispose()掉之后打开的时候,重新new一个出来这样应该就不是保持上次的状态了
解决方案三:
没有初始化吧,new一下
解决方案四:
确实像楼上两位所说的,我在设置jfram的时候写的是this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);不过还是有一个问题,那就是我的在测试子窗口的搜索功能的时候,发现第一此搜索能很成功的搜索出来,第二次按搜索按钮的时候,界面显示的还是原来的查询数据,我重新书写的搜索关键字都写不进去。附上代码:一个是搜索界面的创建代码,一个是对里面的搜索实现方法的类的代码,两者之间使用public,static申明需要床底的参数:1:packagexxp;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JRadioButton;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JTextField;importFunction_SEARCH_BOOK_INTERFACE.search_book_function;publicclasssearch_bookextendsJFrameimplementsActionListener{staticJLabelad=newJLabel("图书检索系统");staticString[]Iterm={"序号","书籍ID","书籍名","作者","书籍状态"};publicstaticString[][]s=newString[20][Iterm.length];staticJTabletable=newJTable(s,Iterm);staticJScrollPanejs=newJScrollPane(table);publicstaticJTextFieldsearch_key=newJTextField();publicstaticJRadioButton[]search_condition={newJRadioButton("书名"),newJRadioButton("作者")};staticButtonGroupbg=newButtonGroup();staticJPaneljp=newJPanel();staticJButtonsubmit=newJButton("搜索");publicsearch_book(){jp.setLayout(null);jp.add(search_condition[0]);jp.add(search_condition[1]);bg.add(search_condition[1]);bg.add(search_condition[0]);search_condition[0].setBounds(250,50,60,28);search_condition[1].setBounds(310,50,60,28);table.setRowHeight(28);jp.add(submit);submit.setBounds(380,45,80,30);jp.add(ad);ad.setFont(newFont("隶书",Font.BOLD,26));ad.setBounds(170,5,250,35);jp.add(search_key);search_key.setBounds(50,50,180,28);jp.add(js);js.setBounds(50,90,400,160);this.add(jp);this.setTitle("书籍检索");this.setResizable(false);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);this.setVisible(true);this.setBounds(250,130,480,300);submit.addActionListener(this);}@OverridepublicvoidactionPerformed(ActionEvente){if(e.getSource()==submit)search_book_function.CONFIRM_SEARCH();}publicstaticvoidmain(String[]args){newsearch_book();}}2:packageFunction_SEARCH_BOOK_INTERFACE;importjava.util.List;importjavax.swing.JOptionPane;importBook_info_hibn.BookInfo;importBook_info_hibn.BookInfoDAO;importTOOLS.tools;importxxp.search_book;publicclasssearch_book_function{staticStringSEARCH_KEY=search_book.search_key.getText().trim();staticbooleanAUTHER_CHOOSE=search_book.search_condition[1].isSelected();staticbooleanBOOK_CHOOSE=search_book.search_condition[0].isSelected();publicstaticvoidSEARCH_(){BookInfoDAOb_dao=newBookInfoDAO();//hibernate中连接数据库的DAO文件BookInfobook;Listlist;if(AUTHER_CHOOSE==false)list=b_dao.findByBookName(SEARCH_KEY);//按书本名字搜索elselist=b_dao.findByBookAuther(SEARCH_KEY);//按书本作者搜索System.out.println(list.size());for(inti=0;i<list.size();i++)//把搜索到的数据写入table表格中{book=newBookInfo();book=(BookInfo)list.get(i);for(intj=0;j<5;j++){if(j==0)search_book.s[i][j]=String.valueOf(i);//把inti转换成string的序号值if(j==1)search_book.s[i][j]=book.getBookId();if(j==2)search_book.s[i][j]=book.getBookName();if(j==3)search_book.s[i][j]=book.getBookAuther();if(j==4)search_book.s[i][j]=tools.TRANSFORM_BOOK_STATUS(book.getBookStatus());}}}publicstaticbooleanCHECK_NO_EMPTY(){if(SEARCH_KEY.equals("")){JOptionPane.showMessageDialog(null,"搜索关键字不能为空","错误提示",JOptionPane.ERROR_MESSAGE);returnfalse;}elseif(AUTHER_CHOOSE==false&&BOOK_CHOOSE==false){JOptionPane.showMessageDialog(null,"请选择搜索条件","错误提示",JOptionPane.ERROR_MESSAGE);System.out.println(AUTHER_CHOOSE);System.out.println(BOOK_CHOOSE);returnfalse;}elsereturntrue;}publicstaticvoidCONFIRM_SEARCH(){booleanb=CHECK_NO_EMPTY();if(b==true)SEARCH_();}}真心求高手解答!
解决方案五:
staticStringSEARCH_KEY=search_book.search_key.getText().trim();staticbooleanAUTHER_CHOOSE=search_book.search_condition[1].isSelected();staticbooleanBOOK_CHOOSE=search_book.search_condition[0].isSelected();这3个值没有更新,只在类定义部分(初始化时赋值了)你每次弹出新对话框之前,把这3个值更新一下