private JTextField writer; private JTextField ISBN;
private JTextField bookName; private JComboBox bookType; private JButton buttonadd; private JButton buttonclose;
DefaultComboBoxModel bookTypeModel;
Map map=new HashMap(); public BookAddIFrame() { super();
final BorderLayout borderLayout = new BorderLayout(); getContentPane().setLayout(borderLayout);
setIconifiable(true); // 设置窗体可最小化---必须
setClosable(true); // 设置窗体可关闭---必须
setTitle(\图书信息添加\); // 设置窗体标题---必须
setBounds(100, 100, 396, 260); // 设置窗体位置和大小---必须
final JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 10, 5, 10));
final GridLayout gridLayout = new GridLayout(0, 4); gridLayout.setVgap(5); gridLayout.setHgap(5);
panel.setLayout(gridLayout); getContentPane().add(panel);
final JLabel label_2 = new JLabel();
label_2.setText(\图书编号:\); panel.add(label_2);
ISBN = new JTextField(\请输入13位书号\,13); ISBN.setDocument(new MyDocument(13)); //设置书号文本框最大输入值为13
ISBN.setColumns(13);
ISBN.addKeyListener(new ISBNkeyListener());
ISBN.addFocusListener(new ISBNFocusListener()); panel.add(ISBN);
final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.CENTER); label.setText(\类别:\); panel.add(label);
bookType = new JComboBox(); bookTypeModel= (DefaultComboBoxModel)bookType.getModel();
//从数据库中取出图书类别
List list=Dao.selectBookCategory(); for(int i=0;i BookType booktype=(BookType)list.get(i); Item item=new Item(); item.setId((String)booktype.getId()); item.setName((String)booktype.getTypeName()); bookTypeModel.addElement(item); } panel.add(bookType); final JLabel label_1 = new JLabel(); label_1.setText(\书名:\); panel.add(label_1); bookName = new JTextField(); panel.add(bookName); final JLabel label_3 = new JLabel(); label_3.setHorizontalAlignment(SwingConstants.CENTER); label_3.setText(\作者:\); panel.add(label_3); writer = new JTextField(); writer.setDocument(new MyDocument(10)); panel.add(writer); final JLabel label_2_1 = new JLabel(); label_2_1.setText(\出版社:\); panel.add(label_2_1); publisher = new JComboBox(); String[]array=new String[]{\出版社\,\信息出版社\,\大型出版社\,\小型出版社\}; publisher.setModel(new DefaultComboBoxModel(array)); panel.add(publisher); final JLabel label_4 = new JLabel(); label_4.setHorizontalAlignment(SwingConstants.CENTER); label_4.setText(\译者:\); panel.add(label_4); translator = new JTextField(); translator.setDocument(new MyDocument(10)); panel.add(translator); final JLabel label_1_1 = new JLabel(); label_1_1.setText(\出版日期:\); panel.add(label_1_1); SimpleDateFormat myfmt=new SimpleDateFormat(\); pubDate= new JFormattedTextField(myfmt.getDateInstance()); pubDate.setValue(new java.util.Date()); panel.add(pubDate); final JLabel label_3_1 = new JLabel(); label_3_1.setHorizontalAlignment(SwingConstants.CENTER); label_3_1.setText(\单价:\); panel.add(label_3_1); price= new JTextField(); price.setDocument(new MyDocument(5)); price.addKeyListener(new NumberListener()); panel.add(price); final JPanel panel_1 = new JPanel(); panel_1.setBorder(new LineBorder(SystemColor.activeCaptionBorder, 1, false)); getContentPane().add(panel_1, BorderLayout.SOUTH); final FlowLayout flowLayout = new FlowLayout(); flowLayout.setVgap(2); flowLayout.setHgap(30); flowLayout.setAlignment(FlowLayout.RIGHT); panel_1.setLayout(flowLayout); buttonadd= new JButton(); buttonadd.addActionListener(new addBookActionListener()); buttonadd.setText(\添加\); panel_1.add(buttonadd); buttonclose = new JButton(); buttonclose.addActionListener(new CloseActionListener()); buttonclose.setText(\关闭\); panel_1.add(buttonclose); final JLabel label_5 = new JLabel(); ImageIcon bookAddIcon=CreatecdIcon.add(\); label_5.setIcon(bookAddIcon); label_5.setPreferredSize(new Dimension(400, 80)); label_5.setBorder(new LineBorder(SystemColor.activeCaptionBorder, 1, false)); getContentPane().add(label_5, BorderLayout.NORTH); label_5.setText(\新书定购(LOGO图片)\); // 显示窗 setVisible(true); 体可关闭---必须在添加所有控件之后执行该语句 } 2:在图书馆信息添加窗体中添加按钮监听事件,在事件中的actionPerformed()方法中进行图书信息添加操作,在Dao类中编写,关键代买如下: public static int Insertbook(String ISBN,String typeId,String bookname,String writer,String translator,String publisher,Date date,Double price){ int i=0; try{ String sql=\into tb_bookInfo(ISBN,typeId,bookname,writer,translator,publisher,date,price) values('\+ISBN+\+typeId+\+bookname+\+writer+\+translator+\+publisher+\+date+\+price+\; //System.out.println(sql); i=Dao.executeUpdate(sql); }catch(Exception e){ System.out.println(e.getMessage()); } Dao.close(); return i; } 3.除了“添加”按钮监听事件之外,还要控制图书调戏吗文本框只能输入数字字符串的键盘监听事件,关键代码如下: class ISBNkeyListener extends KeyAdapter { public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == 13){ buttonadd.doClick(); } } } 4:为“关闭”按钮添加按钮监听事件,主要讲当前窗口关闭。关键代码如下: class CloseActionListener implements ActionListener { // 添加关闭按钮的事件监听器 public void actionPerformed(final ActionEvent e) { doDefaultCloseAction(); } } 图书借阅,归还模块设计: 图书借阅管理窗体