📄 bookinfoframe.java
字号:
/*
*图书信息管理系统Frame构建
*/
package pkg;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.sql.*;
public class BookinfoFrame extends JFrame implements
ActionListener {
/*
*添加各种控件
*/
JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu("文件");
JMenu operation = new JMenu("操作");
JMenu option = new JMenu("选项");
JMenuItem MIinit = new JMenuItem("加载并连接数据库");
JMenuItem MIExit = new JMenuItem("关闭退出所有");
JMenuItem MIopid = new JMenu("更新");
JMenuItem MIinsert = new JMenuItem("添加");
JMenuItem MIdelete = new JMenuItem("删除");
JMenuItem MIlocate = new JMenuItem("查询");
JMenuItem MIhelp = new JMenu("帮助");
JMenuItem MIabout = new JMenuItem("关于");
JMenuItem MIexit = new JMenuItem("退出");
JMenuItem MIhfile = new JMenuItem("文件帮助");
JMenuItem MIhoperation = new JMenuItem("操作说明");
private JLabel lbbooknum = new JLabel();
private JLabel lbbookname = new JLabel();
private JLabel lbclassic = new JLabel();
private JLabel lblocate = new JLabel();
private JLabel lbchoose = new JLabel();
private JLabel lbchosenum = new JLabel();
private JLabel lbchosename = new JLabel();
private JLabel lbchoseati = new JLabel();
private JLabel lbbookauther = new JLabel();
private JLabel lbbookpub = new JLabel();
private JTextField tfbooknum = new JTextField();
private JTextField tfbookname = new JTextField();
private JTextField tfclassic = new JTextField();
private JTextField tfchosenum = new JTextField();
private JTextField tfchosename = new JTextField();
private JTextField tfchoseati = new JTextField();
private JTextField tfbookauther = new JTextField();
private JTextField tfbookpub = new JTextField();
// private JTextField tfisrent = new JTextField();
private JTextArea taCollect = new JTextArea();
private JButton btnInsert = new JButton();
private JButton btnDelete = new JButton();
private JButton btnLocate = new JButton();
private JButton btnRentOK = new JButton();
private JButton btnChoose = new JButton();
private JRadioButton rdbLocnum;
private JRadioButton rdbLocname;
private JRadioButton rdbLocate;
private JRadioButton rdbRent;
private JRadioButton rdbBack;
private JPanel radioPanel_locate,radioPanel_isrent;
private ButtonGroup btg_locate,btg_isrent;
/*
* 改用table??
*/
/* String [] colHeads= {"图书编号","图书名","分类","是否借出"};
Object[][] data = {{"1001","世界五千年","2","true"}};
private JTable jt = new JTable(data,colHeads);
*/
private Container contrainer = getContentPane();
Bookinfo chosebook = new Bookinfo();
boolean flag = false,flagrent = false,init = false;
int flaglocate = 0;
public BookinfoFrame() {
super("图书信息管理系统 张聪 040421116");
// Thread thread=new Thread(this);
// thread.start();
this.setSize(900,700);
btnInsert.addActionListener(this);
btnDelete.addActionListener(this);
btnLocate.addActionListener(this);
btnRentOK.addActionListener(this);
btnChoose.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/////////////////////////////////////////////////
public boolean isNumeric(String str)
{
/*
*用以判断所获取字符串是否是数字,从而进行相应错误处理,并防止抛出异常
*/
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
/////////////////////////////////////////////////
public void setLayout() {
contrainer.setLayout(null);
/*
*添加控件
*/
this.setJMenuBar(jmb);
jmb.add(file);
jmb.add(operation);
jmb.add(option);
operation.add(MIopid);
operation.addSeparator();
operation.add(MIlocate);
MIopid.add(MIinsert);
MIopid.add(MIdelete);
option.add(MIhelp);
option.addSeparator();
option.add(MIabout);
option.addSeparator();
option.add(MIexit);
MIhelp.add(MIhfile);
MIhelp.add(MIhoperation);
file.add(MIinit);
file.addSeparator();
file.add(MIExit);
regListener();
lbbooknum.setText("图书编号");
contrainer.add(lbbooknum);
lbbooknum.setBounds(16,26,120,16);
lbbookauther.setText("图书作者");
contrainer.add(lbbookauther);
lbbookauther.setBounds(450,26,120,16);
lbbookpub.setText("图书出版");
contrainer.add(lbbookpub);
lbbookpub.setBounds(450,56,120,16);
lbchoseati.setText("图书状态");
contrainer.add(lbchoseati);
lbchoseati.setBounds(580,390,125,16);
lbbookname.setText("图书名");
contrainer.add(lbbookname);
lbbookname.setBounds(16,56,115,16);
lbclassic.setText("图书分类");
contrainer.add(lbclassic);
lbclassic.setBounds(16,90,110,16);
lblocate.setText("图书查询");
contrainer.add(lblocate);
lblocate.setBounds(16,180,115,16);
lbchosenum.setText("已选图书编号");
contrainer.add(lbchosenum);
lbchosenum.setBounds(580,250,126,16);
lbchosename.setText("已选图书名");
contrainer.add(lbchosename);
lbchosename.setBounds(580,320,125,16);
lbchoose.setText("已选图书信息");
contrainer.add(lbchoose);
lbchoose.setBounds(580,180,115,16);
contrainer.add(tfchosenum);
tfchosenum.setBounds(700,250,150,22);
contrainer.add(tfchosename);
tfchosename.setBounds(700,320,150,22);
contrainer.add(tfchoseati);
tfchoseati.setBounds(700,390,150,22);
contrainer.add(tfbooknum);
tfbooknum.setBounds(140,25,300,22);
contrainer.add(tfbookname);
tfbookname.setBounds(140,57,300,22);
contrainer.add(tfclassic);
tfclassic.setBounds(140,90,200,22);
contrainer.add(tfbookauther);
tfbookauther.setBounds(520,25,150,22);
contrainer.add(tfbookpub);
tfbookpub.setBounds(520,57,150,22);
btnInsert.setText("添加");
contrainer.add(btnInsert);
btnInsert.setToolTipText("添加新图书");
btnInsert.setBounds(700,25,100,30);
btnDelete.setText("删除");
contrainer.add(btnDelete);
btnDelete.setToolTipText("删除图书数据");
btnDelete.setBounds(700,75,100,30);
btnLocate.setText("查询");
contrainer.add(btnLocate);
btnLocate.setToolTipText("开始数据查询");
btnLocate.setBounds(300,130,100,30);
btnChoose.setText("选中");
contrainer.add(btnChoose);
btnChoose.setToolTipText("选中图书信息");
btnChoose.setBounds(420,130,100,30);
btnRentOK.setText("确定");
contrainer.add(btnRentOK);
btnRentOK.setToolTipText("确定修改图书状态");
btnRentOK.setBounds(680,540,100,30);
taCollect.enable(false);
taCollect.append("图书信息\n");
contrainer.add(taCollect);
taCollect.setBounds(16,200,550,400);
// contrainer.add(jt);
// jt.setBounds(16,200,450,400);
radioPanel_locate = new JPanel();
radioPanel_isrent = new JPanel();
rdbLocname = new JRadioButton("按书名");
rdbLocname.addActionListener(this);
rdbLocnum = new JRadioButton("按编号");
rdbLocnum.addActionListener(this);
rdbLocate = new JRadioButton("全部查询");
rdbLocate.addActionListener(this);
// rdbLocnum.setSelected(true);
rdbRent = new JRadioButton("出借");
rdbRent.addActionListener(this);
rdbBack = new JRadioButton("还书");
rdbBack.addActionListener(this);
rdbRent.setSelected(true);
btg_locate = new ButtonGroup();
btg_isrent = new ButtonGroup();
//单选按钮属于不同的组
btg_locate.add(rdbLocnum);
btg_locate.add(rdbLocname);
btg_locate.add(rdbLocate);
btg_isrent.add(rdbRent);
btg_isrent.add(rdbBack);
contrainer.add(rdbLocnum);
rdbLocnum.setBounds(30,130,80,30);
contrainer.add(rdbLocname);
rdbLocname.setBounds(120,130,80,30);
contrainer.add(rdbLocate);
rdbLocate.setBounds(210,130,80,30);
contrainer.add(rdbRent);
rdbRent.setBounds(650,480,60,30);
contrainer.add(rdbBack);
rdbBack.setBounds(750,480,60,30);
}
private void regListener(){
MIinit.addActionListener(this);
MIExit.addActionListener(this);
MIinsert.addActionListener(this);
MIdelete.addActionListener(this);
MIlocate.addActionListener(this);
MIabout.addActionListener(this);
MIexit.addActionListener(this);
MIhfile.addActionListener(this);
MIhoperation.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
OPerations operation = new OPerations();
/*********************初始化 用以创建或读取原图书信息储存文件BookData.txt*************************/
if(e.getSource()==MIinit){
/*
*调用初始化方法
*/
init = operation.inits(init);
tfchosenum.setText("");
tfchosename.setText("");
tfchoseati.setText("");
tfbooknum.setText("");
tfbookname.setText("");
tfclassic.setText("");
}//初始化
/*********************完成BookData.txt的初始化工作,是执行系统的开始*************************/
/*********************添加新的图书信息 在"保存"之前 所添加都在BookList中并不在存储文件中生效*************************/
if(e.getActionCommand().equals("添加")||e.getSource()==MIinsert){
if(!init){
String output = "请首先装载!";
JOptionPane.showMessageDialog(null,output,"Message",
JOptionPane.INFORMATION_MESSAGE);
}
else if(tfbooknum.getText().equals("")){
String output = "图书编号不能为空!";
JOptionPane.showMessageDialog(null,output,"ErrorMessage",
JOptionPane.INFORMATION_MESSAGE);
}
else if(tfbookname.getText().equals("")){
String output = "图书名不能为空!";
JOptionPane.showMessageDialog(null,output,"ErrorMessage",
JOptionPane.INFORMATION_MESSAGE);
}
else if(tfclassic.getText().equals("")){
String output = "图书分类不能为空!";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -