📄 basebookadd.java
字号:
package librarymanagement.view.dialog.base;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import librarymanagement.action.bookMangementAction.PanelFocusAction;
import librarymanagement.view.common.CheckInputText;
import librarymanagement.view.common.GBC;
import librarymanagement.vo.BookVo;
/*
* 添加图书和修改图书类的父类
*/
public class Basebookadd extends JDialog {
//面板分别是:分割面板的上半部分面板,图书信息面板的左、右面板,操作信息面板
private JPanel topPanel, jpanel,leftPanel,rightPanel, btnPanel;
public JTextField IDtext;// 图书编号
public JTextField nametext;// 图书名
public JTextField sorttext;// 类别
public JTextField timetext; // 版次
public JTextField writertext;// 作者
public JTextField locationtext;// 存放位置
public JTextField pricetext;// 价格
public JTextField dateinBase;// 在库数量
public JTextField datepublish;// 出版社
public JTextField numbertext;// 库存数量
public JComboBox publishBox, box;
//JLable:分别是图书编号、图书名、类别、版次、作者、存放位置、价格、在库数量、出版社、库存数量
public JLabel ID, name, sort, booktime, writer, location, price, indate,
publish, number;
public JTable table;
private JButton Btn;
//分割面板
private JSplitPane splitPane = new JSplitPane();
/**
* author:廖运球 构造函数
*/
public Basebookadd() {
this.setSize(650, 700);
this.setLayout(new BorderLayout());
this.setLocation(100,100);
initialPanel();
}
/**
* 分割面板
*/
public void initialPanel() {
this.setLayout(new BorderLayout());
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(buildTopJPanel());
splitPane.setBottomComponent(getJTableScrollPane());
splitPane.setOneTouchExpandable(true);
this.add(splitPane);
}
/**
* 带有滚动条的表
* @return
*/
public JScrollPane getJTableScrollPane() {
JScrollPane js = new JScrollPane(buildJTable());
return js;
}
/**
* 分割面板的上半部分面板:由圖書信息面板、操作信息面板组成
*
* @return topPanel
*/
public JPanel buildTopJPanel() {
if (topPanel == null) {
topPanel = new JPanel(new BorderLayout());
topPanel.add(buildJPanel(), BorderLayout.NORTH);
topPanel.add(buildBottommainJPanel());
topPanel.setBorder(BorderFactory.createTitledBorder("图书信息"));
// topPanel.add(buildJTable(), BorderLayout.SOUTH);
}
return topPanel;
}
/**
* 填寫圖書信息的面板
*
* @return
*/
public JPanel buildJPanel() {
if (jpanel == null) {
jpanel = new JPanel();
jpanel.setLayout(new GridBagLayout());
buildJLabel();
buildJTxt();
jpanel.add(ID, new GBC(0, 0).setInset(10).setFill(GBC.NORTHWEST));
jpanel.add(IDtext, new GBC(1, 0).setInset(10).setWeight(3, 0)
);
jpanel.add(name, new GBC(2, 0).setFill(GBC.WEST).setInset(10));
jpanel.add(nametext, new GBC(3, 0).setWeight(3, 0)
.setInset(10));
jpanel.add(sort, new GBC(0, 1).setInset(10).setFill(GBC.WEST));
jpanel.add(sorttext, new GBC(1, 1).setWeight(3, 0).setInset(10)
);
jpanel.add(writer, new GBC(2, 1).setFill(GBC.WEST).setInset(10));
jpanel.add(writertext, new GBC(3, 1).setWeight(3, 0)
.setInset(10));
jpanel.add(publish, new GBC(0, 2).setFill(GBC.WEST));
jpanel.add(datepublish, new GBC(1, 2).setWeight(3, 0)
.setInset(10));
jpanel.add(location, new GBC(2, 2).setFill(GBC.WEST).setInset(
10));
jpanel.add(locationtext, new GBC(3, 2).setWeight(3, 0)
.setInset(10));
jpanel.add(indate, new GBC(0, 3).setFill(GBC.WEST));
jpanel.add(dateinBase, new GBC(1, 3).setWeight(3, 0)
.setInset(10));
jpanel.add(price, new GBC(2, 3).setFill(GBC.WEST).setInset(10));
jpanel.add(pricetext, new GBC(3, 3).setWeight(3, 0)
.setInset(10));
jpanel.add(booktime, new GBC(0, 4).setFill(GBC.WEST));
jpanel.add(timetext, new GBC(1, 4).setWeight(3, 0)
.setInset(10));
jpanel.add(number, new GBC(2, 4).setFill(GBC.WEST).setInset(10));
jpanel.add(numbertext, new GBC(3, 4).setWeight(3, 0)
.setInset(10));
IDtext.addKeyListener(new PanelFocusAction(jpanel,1));
nametext.addKeyListener(new PanelFocusAction(jpanel,3));
sorttext.addKeyListener(new PanelFocusAction(jpanel,5));
writertext.addKeyListener(new PanelFocusAction(jpanel,7));
datepublish.addKeyListener(new PanelFocusAction(jpanel,9));
locationtext.addKeyListener(new PanelFocusAction(jpanel,11));
dateinBase.addKeyListener(new PanelFocusAction(jpanel,13));
pricetext.addKeyListener(new PanelFocusAction(jpanel,15));
timetext.addKeyListener(new PanelFocusAction(jpanel,17));
numbertext.addKeyListener(new PanelFocusAction(jpanel,19));
}
return jpanel;
}
/**
* 由"图书编号", "书名", "类别", "作者", "版次", "出版社",
"存放位置","在库数量", "价格", "库存数量" 组成的表
* @return
*/
public JTable buildJTable() {
if (table == null) {
String[] names = { "图书编号", "书名", "类别", "作者", "版次", "出版社", "存放位置",
"在库数量", "价格", "库存数量" };
Object[][] data = {};
DefaultTableModel model = new DefaultTableModel(data, names) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
table = new JTable(model);
table.getTableHeader().setReorderingAllowed(false);
}
return table;
}
public void buildJLabel() {
ID = new JLabel("图书编号");
sort = new JLabel("类别");
publish = new JLabel("出版社");
booktime = new JLabel("版次");
indate = new JLabel("在库数量");
name = new JLabel("书名");
writer = new JLabel("作者");
location = new JLabel("存放位置");
price = new JLabel("价格");
number = new JLabel("库存数量");
}
public void buildJTxt() {
IDtext = new JTextField(10);// 图书编号
sorttext = new JTextField(10);// 类别
datepublish = new JTextField(10);// 出版社
dateinBase = new JTextField(10);// 在库数量
timetext = new JTextField(10);// 版次
nametext = new JTextField(10);// 图书名
writertext = new JTextField(10);// 作者
locationtext = new JTextField(10);// 存放位置
pricetext = new JTextField(10);// 价格
numbertext = new JTextField(10);// 库存数量
}
/**
* 主界面的下半部分面板BottommainJPanel
*
* @return
*/
public JPanel buildBottommainJPanel() {
return null;
}
/**
* 清空文本框类容的方法
*/
public void getDeleteInfo() {
IDtext.setText(" ");
nametext.setText(" ");
timetext.setText(" ");
sorttext.setText(" ");
writertext.setText(" ");
locationtext.setText(" ");
pricetext.setText(" ");
dateinBase.setText(" ");
datepublish.setText(" ");
numbertext.setText(" ");
}
/**
* 修改图书信息面板中由表中的记录被双击时被显示在对应的文本框中
* @param names
*/
protected void initialUpdateValues(String[] names) {
IDtext.setText(names[0]);
nametext.setText(names[1]);
timetext.setText(names[4]);
sorttext.setText(names[2]);
writertext.setText(names[3]);
locationtext.setText(names[6]);
pricetext.setText(names[8]);
dateinBase.setText(names[7]);
datepublish.setText(names[5]);
numbertext.setText(names[9]);
}
/**
* 获得一个BookVo对象的一个方法
*
* @return
*/
public BookVo getInputTxtValue() {
int book_id = Integer.parseInt(IDtext.getText().trim());
String book_name = nametext.getText().trim();
String kind = sorttext.getText().trim();
int in_amount = Integer.parseInt(numbertext.getText().toString());
String author = writertext.getText();
String book_location = locationtext.getText().trim();
double price = Double
.parseDouble(pricetext.getText().trim().toString());
String publis_com = datepublish.getText().trim();
int book_amount = Integer.parseInt(dateinBase.getText().trim()
.toString());
String edition = timetext.getText().trim();
return new BookVo(book_id, book_name, kind, author, publis_com,
book_location, book_amount, price, edition, in_amount);
}
/**
* 检查所有的记录项是否按要求填写
*
* @return
*/
public boolean checkInputTxt() {
// buildJtext();
// System.out.println("取JTextField中的内容"+IDtext.getText().trim());
if (CheckInputText.checkInputIsNull(IDtext.getText())) {
JOptionPane.showMessageDialog(null, "图书编号不能为空", "提示信息",
JOptionPane.YES_OPTION);
IDtext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(nametext.getText())) {
JOptionPane.showMessageDialog(null, "图书名不能为空", "提示信息",
JOptionPane.YES_OPTION);
nametext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(sorttext.getText())) {
JOptionPane.showMessageDialog(null, "类别不能为空", "提示信息",
JOptionPane.YES_OPTION);
sorttext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(timetext.getText())) {
JOptionPane.showMessageDialog(null, "版次不能为空", "提示信息",
JOptionPane.YES_OPTION);
timetext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(locationtext.getText())) {
JOptionPane.showMessageDialog(null, "存放位置不能为空", "提示信息",
JOptionPane.YES_OPTION);
locationtext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(writertext.getText())) {
JOptionPane.showMessageDialog(null, "作者不能为空", "提示信息",
JOptionPane.YES_OPTION);
writertext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(pricetext.getText())) {
JOptionPane.showMessageDialog(null, "价格不能为空", "提示信息",
JOptionPane.YES_OPTION);
pricetext.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(dateinBase.getText())) {
JOptionPane.showMessageDialog(null, "在库数量不能为空", "提示信息",
JOptionPane.YES_OPTION);
dateinBase.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(datepublish.getText())) {
JOptionPane.showMessageDialog(null, "出版社不能为空", "提示信息",
JOptionPane.YES_OPTION);
datepublish.requestFocus();
return false;
}
if (CheckInputText.checkInputIsNull(numbertext.getText())) {
JOptionPane.showMessageDialog(null, "库存数量不能为空", "提示信息",
JOptionPane.YES_OPTION);
numbertext.requestFocus();
return false;
}
return true;
}
/*public void checkrangerInputNumber(JTextField argstext,int i,String input){
argstext.getText().trim().matches("\\d{0,i}")<input.length();
return ;
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -