📄 bookeditpanel.java
字号:
package com.ciash.bms.gui.impl;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import com.ciash.bms.entity.Book;
import com.ciash.bms.entity.BookGenus;
import com.ciash.bms.entity.User;
import com.ciash.common.gui.DateSelecter;
import com.ciash.bms.gui.selecter.BookSelecter;
import java.awt.*;
import java.awt.event.*;
/**
* <p>
* Title: BMS
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2009
* </p>
* <p>
* Company: Cigarette Ash Inc.
* </p>
*
* @author Cigarette Ash
* @version 1.0
*/
public class BookEditPanel extends JPanel implements BookSelecter {
private JPanel panel = new JPanel();
private JTabbedPane mainPanel = new JTabbedPane();
private JPanel additionalInfo = new JPanel();
private JPanel commonInfo = new JPanel();
private JLabel bookIdLabel = new JLabel();
private JLabel bookNameLabel = new JLabel();
private JTextField bookIdField = new JTextField("");
private JLabel bookGenusLabel = new JLabel();
private JComboBox bookGenus = new JComboBox();
private JLabel bookAuthor = new JLabel();
private JTextField bookNameField = new JTextField("");
private JTextField bookAuthorField = new JTextField("");
private JLabel bookPDLabel = new JLabel();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
private JLabel bookPriceLabel = new JLabel();
private JTextField bookPriceField = new JTextField("");
private JLabel bookCountLabel = new JLabel();
private JTextField userIdField = new JTextField("");
private JSpinner bookCountSpinner = new JSpinner();
private SpinnerNumberModel spinnerModel = new SpinnerNumberModel();
private JLabel userIdLabel = new JLabel();
private DateSelecter dateSelecter = new DateSelecter();
private JPanel IntroducePanel = new JPanel();
private JScrollPane jScrollPane1 = new JScrollPane();
private BorderLayout borderLayout3 = new BorderLayout();
private JTextArea introduceTextArea = new JTextArea("");
private JLabel jLabel1 = new JLabel();
private JButton browseUserButton = new JButton();
private ImageChooser imageChooser = new ImageChooser();
private HashMap genusMap = new HashMap();
private Book selectBook;
public BookEditPanel() {
jbInit();
}
private void jbInit() {
panel.setLayout(new BorderLayout());
bookIdLabel.setText("索书号");
bookIdLabel.setBounds(new Rectangle(17, 25, 90, 22));
commonInfo.setLayout(null);
bookNameLabel.setAlignmentY((float) 0.5);
bookNameLabel.setDebugGraphicsOptions(0);
bookNameLabel.setDoubleBuffered(false);
bookNameLabel.setOpaque(false);
bookNameLabel.setText("书名");
bookNameLabel.setBounds(new Rectangle(17, 62, 90, 22));
bookIdField.setText("");
bookIdField.setBounds(new Rectangle(117, 25, 250, 22));
bookGenusLabel.setText("图书分类");
bookGenusLabel.setBounds(new Rectangle(17, 101, 90, 22));
bookAuthor.setText("作者");
bookAuthor.setBounds(new Rectangle(17, 143, 90, 22));
bookNameField.setText("");
bookNameField.setBounds(new Rectangle(117, 62, 249, 22));
bookAuthorField.setSelectionStart(0);
bookAuthorField.setText("");
bookAuthorField.setBounds(new Rectangle(117, 143, 250, 22));
bookPDLabel.setText("购买日期");
bookPDLabel.setBounds(new Rectangle(17, 181, 90, 20));
setLayout(gridBagLayout1);
bookPriceLabel.setText("价格");
bookPriceLabel.setBounds(new Rectangle(17, 221, 90, 22));
bookPriceField.setText("");
bookPriceField.setBounds(new Rectangle(117, 221, 245, 22));
bookCountLabel.setText("库存量");
bookCountLabel.setBounds(new Rectangle(17, 260, 90, 22));
userIdField.setEditable(false);
userIdField.setEnabled(true);
userIdField.setText("");
userIdField.setBounds(new Rectangle(117, 299, 120, 22));
userIdLabel.setText("借书者用户名");
userIdLabel.setBounds(new Rectangle(17, 300, 100, 22));
commonInfo.setDebugGraphicsOptions(0);
commonInfo.setMaximumSize(new Dimension(2147483647, 2147483647));
bookCountSpinner.setModel(spinnerModel);
bookCountSpinner.setBounds(new Rectangle(117, 260, 46, 22));
spinnerModel.setMinimum(new Integer(0));
spinnerModel.setMaximum(new Integer(999999));
IntroducePanel.setLayout(borderLayout3);
introduceTextArea.setText("");
jScrollPane1.setDebugGraphicsOptions(0);
jLabel1.setVerifyInputWhenFocusTarget(true);
jLabel1.setText("预览");
jLabel1.setBounds(new Rectangle(6, 11, 49, 24));
browseUserButton.setBounds(new Rectangle(260, 299, 90, 23));
browseUserButton.setText("浏览");
browseUserButton
.addActionListener(new BookEditPanel_browseUserButton_actionAdapter(
this));
jLabel2.setVerifyInputWhenFocusTarget(true);
jLabel2.setText("图书介绍");
dateSelecter.setBounds(new Rectangle(117, 173, 264, 28));
bookGenus.setBounds(new Rectangle(117, 99, 248, 24));
add(panel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
0, 0, 0, 0), 453, 315));
panel.add(mainPanel, BorderLayout.CENTER);
commonInfo.add(bookIdField, null);
commonInfo.add(browseUserButton, null);
commonInfo.add(dateSelecter, null);
commonInfo.add(bookAuthorField, null);
commonInfo.add(bookGenus, null);
commonInfo.add(bookNameField, null);
commonInfo.add(userIdLabel, null);
commonInfo.add(bookAuthor, null);
commonInfo.add(bookGenusLabel, null);
commonInfo.add(bookNameLabel, null);
commonInfo.add(bookIdLabel, null);
commonInfo.add(bookPDLabel, null);
commonInfo.add(bookPriceLabel, null);
commonInfo.add(bookCountLabel, null);
commonInfo.add(bookPriceField, null);
commonInfo.add(bookCountSpinner, null);
commonInfo.add(userIdField, null);
IntroducePanel.add(jScrollPane1, BorderLayout.CENTER);
IntroducePanel.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel2, null);
jScrollPane1.getViewport().add(introduceTextArea, null);
additionalInfo.setLayout(new BorderLayout());
additionalInfo.add(imageChooser, BorderLayout.CENTER);
mainPanel.add(commonInfo, "普通信息");
mainPanel.add(IntroducePanel, "图书介绍");
mainPanel.add(additionalInfo, "图片信息");
}
private void initValue(Book book) {
selectBook = book;
bookIdField.setEditable(false);
if (book.getUserId() == null) {
browseUserButton.setEnabled(false);
} else {
browseUserButton.setEnabled(true);
}
bookIdField.setText(book.getBookId());
bookAuthorField.setText(book.getBookAuthor());
bookPriceField.setText(book.getBookPrice());
bookNameField.setText(book.getBookName());
resetCount(book);
userIdField.setText(book.getUserId());
dateSelecter.setCalendar(book.getPurchaseDate());
bookGenus.setSelectedItem(book.getBookGenus());
}
private void clearValues() {
selectBook = null;
bookIdField.setEditable(true);
bookIdField.setText("");
bookAuthorField.setText("");
bookPriceField.setText("");
bookNameField.setText("");
resetCount(null);
userIdField.setText("");
bookGenus.setSelectedIndex(0);
dateSelecter.setCalendar(Calendar.getInstance());
}
/**
* 使用当前面板值创建一个书籍对象
*
* @return 返回新创建的书籍对象
*/
private Book createBook() {
selectBook = new Book();
selectBook.setBookId(bookIdField.getText());
selectBook.setBookAuthor(bookAuthorField.getText());
selectBook.setBookPrice(bookPriceField.getText());
selectBook.setBookName(bookNameField.getText());
selectBook.setBookCount(bookCountSpinner.getValue().toString());
selectBook.setUserId(userIdField.getText());
selectBook.setBookInfo(introduceTextArea.getText());
selectBook.setPurchaseDate(dateSelecter.getCalendar());
selectBook.setBookGenus((String) bookGenus.getSelectedItem());
return selectBook;
}
/**
* 检查id值是否为空
*
* @return true id值为空
*/
private boolean idIsNull() {
if (bookIdField.getText() == null || bookIdField.getText().equals("")) {
JOptionPane.showMessageDialog(null, "索书号不能为空", "错误!",
JOptionPane.ERROR_MESSAGE);
return true;
}
return false;
}
public void setEditable(boolean isEditable) {
editable(isEditable);
imageChooser.setEditable(isEditable);
dateSelecter.setEditable(isEditable);
introduceTextArea.setEditable(isEditable);
}
private void editable(boolean isEditable) {
bookIdField.setEditable(isEditable);
bookAuthorField.setEditable(isEditable);
bookCountSpinner.setEnabled(isEditable);
bookGenus.setEditable(isEditable);
bookNameField.setEditable(isEditable);
bookPriceField.setEditable(isEditable);
browseUserButton.setEnabled(isEditable);
bookGenus.setEnabled(isEditable);
}
public boolean isEditable() {
return imageChooser.isEditable();
}
private void resetCount(Book book) {
if (book == null || book.getBookCount() == null) {
bookCountSpinner.getModel().setValue(new Integer(0));
} else {
bookCountSpinner.setValue(new Integer(book.getBookCount()));
}
}
JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel jLabel2 = new JLabel();
public void setGenus(Collection v) {
bookGenus.removeAllItems();
Iterator it = v.iterator();
int i = 0;
while (it.hasNext()) {
BookGenus g = (BookGenus) it.next();
bookGenus.addItem(g.getDescription());
i++;
}
if (selectBook != null) {
bookGenus.setSelectedItem(selectBook.getBookGenus());
}
}
public boolean checkValues() {
if (idIsNull()) {
return false;
}
return true;
}
public void setSelectBook(Book book) {
if (book != null) {
initValue(book);
} else {
clearValues();
}
}
public Book getSelectBook() {
return createBook();
}
void browseUserButton_actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "功能还未完善,等待后期添加,谢谢!", "错误!",
JOptionPane.ERROR_MESSAGE);
}
private class BookEditPanel_browseUserButton_actionAdapter implements
java.awt.event.ActionListener {
BookEditPanel adaptee;
BookEditPanel_browseUserButton_actionAdapter(BookEditPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.browseUserButton_actionPerformed(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -