⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookaddiframe.java

📁 图书管理系统java+swing+SQL Server实现 包含了图书管理系统的所有功能 纯JAVA实现
💻 JAVA
字号:
package com.lishan.iframe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import com.lishan.JComPz.Item;
import com.lishan.dao.Dao;
import com.lishan.model.BookType;
import com.lishan.util.CreatedIcon;
import com.lishan.util.MyDocument;

/**
 * 图书添加窗体
 * @author Administrator
 *
 */
public class BookAddIFrame extends JInternalFrame{
	private JComboBox publisher;            //下拉选项卡
	private JTextField price;
	private JFormattedTextField pubDate;
	private JTextField translator;
	private JTextField writer;
	private JTextField ISBN;
	private JTextField bookName;
	private JComboBox bookTypeBox;
	private JButton buttonadd;
	private JButton buttonclose;
	DefaultComboBoxModel bookTypeModel;
	
	//Map map = new HashMap();
	public BookAddIFrame(){
		super();
		final BorderLayout borderLayout = new BorderLayout();
		getContentPane().setLayout(borderLayout);
		setMaximizable(true); 
		setIconifiable(true);                         //设置窗体可最小化
		setClosable(true);                            //设置窗体可关闭
		setTitle("图书信息添加");                        //设置窗体标题
		setBounds(100,100,369,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);
		
		bookTypeBox = new JComboBox();
		bookTypeModel = (DefaultComboBoxModel)bookTypeBox.getModel();
		
		//从数据库中取出图书类别
		List list = Dao.selectBookCategory(); //Dao获得图书类别list集合
		for(int i = 0; i < list.size(); 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(bookTypeBox);
		
		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("出版社:");
		panel.add(label_2_1);
		
		publisher = new JComboBox();
		String[] array = new String[]{"***出版社","**信息出版社","**大型出版社","***小型出版社"};
		publisher.setModel(new DefaultComboBoxModel(array));// 设置 publisher 用于获取项列表的数据模型
		panel.add(publisher);
		
		final JLabel label_4 = new JLabel("译者: ");
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(label_4);
		
		translator = new JTextField();
		translator.setDocument(new MyDocument(10));
		panel.add(translator);
		
		final JLabel label_1_1 = new JLabel("出版日期:");
		panel.add(label_1_1);
		
		SimpleDateFormat myfmt = new SimpleDateFormat("yyyy-MM-dd");
		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);
		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());
		panel_1.add(buttonadd);
		
		buttonclose = new JButton("关闭");
		buttonclose.addActionListener(new CloseActionListener());
		panel_1.add(buttonclose);
		
		final JLabel label_5 = new JLabel("添加新书(LOGO图片)");
		ImageIcon bookAddIcon = CreatedIcon.add("bookAdd.jpg");
		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);
		
		setVisible(true);                                    //显示窗体可关闭--必须在添加所有的控件之后执行该语句
		pack();
		this.setResizable(false);
	} 
	
	class ISBNFocusListener extends FocusAdapter{            //添加ISBN JTestField的光标监听器
		public void focusLost(FocusEvent e){
			if(!Dao.selectBookInfo(ISBN.getText().trim()).isEmpty()){
				JOptionPane.showMessageDialog(null,"添加书号重复!");
				return;
			}
		}
	}
	class ISBNKeyListener extends KeyAdapter{              //添加ISBN JTestField的键盘监听器
		public void keyPressed(final KeyEvent e){
			if(e.getKeyCode() == 13){
				buttonadd.doClick();
			}
		}
	}
	class addBookActionListener implements ActionListener{      //添加按钮的单击事件监听器
		public void actionPerformed(final ActionEvent e) {
            //添加新书
			if(ISBN.getText().length() == 0){
				JOptionPane.showMessageDialog(null,"书号文本框不能为空");
				return;
			}
			if(ISBN.getText().length() != 13){
				JOptionPane.showMessageDialog(null, "书号输入位数为13位");
				return;
			}
			if(bookName.getText().length() == 0){
				JOptionPane.showMessageDialog(null, "图书名称文本框不可以为空");
				return;
			}
			if(writer.getText().length() == 0){
				JOptionPane.showMessageDialog(null, "作者文本框不可以为空");
				return;
			}
			if(pubDate.getText().length() == 0){
				JOptionPane.showMessageDialog(null, "出版日期文本框不可以为空");
				return;
			}
			if(price.getText().length() == 0){
				JOptionPane.showMessageDialog(null, "单价文本框不能为空");
				return;
			}
			String ISBNs = ISBN.getText().trim();
			
			//分类
			Object selectedItem = bookTypeBox.getSelectedItem();
			if(selectedItem == null)
				return;
			Item item = (Item)selectedItem;
            String bookTypes = item.getId();
            String translators = translator.getText().trim();
            String bookNames = bookName.getText().trim();
            String writers = writer.getText().trim();
            String publishers = (String)publisher.getSelectedItem();
            String pubDates = pubDate.getText().trim();
            String prices = price.getText().trim();
            System.out.println("bookTypes=" + bookTypes);
            
            int i = Dao.insertBook(ISBNs,  bookTypes,  bookNames, writers,  translators,
            		publishers, java.sql.Date.valueOf(pubDates), Double.parseDouble(prices));

            if(i == 1){
            	JOptionPane.showMessageDialog(null, "添加新书成功");
            	doDefaultCloseAction();
            }
		}
	}
	class NumberListener extends KeyAdapter{
		public void keyTyped(KeyEvent e){
			String numStr = "0123456789." + (char)8;
			if(numStr.indexOf(e.getKeyChar()) < 0){
				e.consume();
			}
		}
	}
	class CloseActionListener implements ActionListener{        //添加关闭按钮的事件监听器
		public void actionPerformed(ActionEvent e) {
            doDefaultCloseAction();			
		}
		
	}
//	public static void main(String[] args){
//		new BookAddIFrame();
//	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -