?? bookaddiframe.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()); //注冊鍵盤監(jiān)聽對象
ISBN.addFocusListener(new ISBNFocusListener()); //注冊光標監(jiān)聽對象
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); //顯示窗體可關閉--必須在添加所有的控件之后執(zhí)行該語句
pack();
this.setResizable(false);
}
class ISBNFocusListener extends FocusAdapter{ //添加ISBN JTestField的光標監(jiān)聽器
public void focusLost(FocusEvent e){
if(!Dao.selectBookInfo(ISBN.getText().trim()).isEmpty()){
JOptionPane.showMessageDialog(null,"添加書號重復!");
return;
}
}
}
class ISBNKeyListener extends KeyAdapter{ //添加ISBN JTestField的鍵盤監(jiān)聽器
public void keyPressed(final KeyEvent e){
if(e.getKeyCode() == 13){
buttonadd.doClick();
}
}
}
class addBookActionListener implements ActionListener{ //添加按鈕的單擊事件監(jiān)聽器
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{ //添加關閉按鈕的事件監(jiān)聽器
public void actionPerformed(ActionEvent e) {
doDefaultCloseAction();
}
}
// public static void main(String[] args){
// new BookAddIFrame();
// }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -