import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class SearchFrame extends InitFrame
{
private static final long serialVersionUID = 343751176496776183L;
@SuppressWarnings("unchecked")
public SearchFrame()
{
super("图书管理系统—查询","Library.png",650,300);
//设置布局
this.setLayout(new BorderLayout());
this.setResizable(false);
// 创建面板
JPanel northPanel = new JPanel();
JPanel northPanel1 = new JPanel();//放置下拉列表
JPanel northPanel2 = new JPanel();//放置检索分类
JPanel centerPanel = new JPanel();//放置文本框和二维表
JPanel southPanel = new JPanel();//放置按钮
// 添加面板到窗体
this.add(northPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.add(southPanel,BorderLayout.SOUTH);
// 设置各面板的布局
northPanel.setLayout(new GridLayout(2,1));
northPanel.add(northPanel1);
northPanel.add(northPanel2);
northPanel1.setLayout(new BorderLayout());
northPanel2.setLayout(new GridLayout(3,1));
centerPanel.setLayout(new BorderLayout());
southPanel.setLayout(new FlowLayout());
//设置 下拉列表框 (书籍分类)
File file_Library= new File("E:\\图书管理系统\\分类");
File[] file_class = file_Library.listFiles();// 获取 “分类”文件夹下的各个具体分类的文件夹
comBoBox= new JComboBox(file_class);
northPanel1.add(comBoBox,BorderLayout.EAST);
JLabel label = new JLabel("请选择分类:");
northPanel1.add(label,BorderLayout.WEST);
// 定义按钮组
bg = new ButtonGroup();
// 定义单选按钮
Radio_number = new JRadioButton("按编号",true);
Radio_name = new JRadioButton("按书名");
Radio_author = new JRadioButton("按作者");
// 将按钮添加到按钮组中
bg.add(Radio_number);
bg.add(Radio_name);
bg.add(Radio_author);
// 将单选按钮添加到面板
northPanel2.add(Radio_number);
northPanel2.add(Radio_name);
northPanel2.add(Radio_author);
//设置按钮组的边框
Border line = BorderFactory.createLineBorder(Color.BLUE, 2, true);
Border title = BorderFactory.createTitledBorder(line,"检索方式",TitledBorder.LEADING,TitledBorder.TOP);
northPanel2.setBorder(title);
// 添加接受检索输入的文本框
textField = new JTextField(35);
centerPanel.add(textField,BorderLayout.NORTH);
// 添加操作按钮
JButton button_ok = new JButton("确定",new ImageIcon("ok.png"));
southPanel.add(button_ok);
button_ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// 获取下拉列表的路径(文件)
File f1 =SearchFrame.this.getClassName();
// 从获得的分类下 读取存放书籍信息的文件
File file_book_info = new File(f1.getPath()+"\"+"bookinfo.dat");
try {
FileInputStream fr = new FileInputStream(file_book_info);
ObjectInputStream obo = new ObjectInputStream(fr);
int n = 0;//加入变量n 的目的是考虑到搜索出来的同一个作者多本书的情况下 可能出现多行
boolean exist = false;//引入布尔型变量,用来判断有没有查到
if (Radio_number.isSelected())
{
int m = Integer.parseInt(textField.getText());
book_search =(Book)obo.readObject();//先读取第一个对象
// 如果文本框中输入的文字出现在某个对象中执行以下操作
if(book_search.getNumber()==m)
{
exist =true;
Object [][] book = new Object[1][];
int number=book_search.getNumber();
String name=book_search.getName();
String author=book_search.getAuthor();
String press=book_search.getPress();
int count=book_search.getCount();
Object [] book_searched = new Object[]{number,name,author,press,count};
book[0] = book_searched ;
Object [] book_info = new Object[]{"编号","书名 ","作者","出版社","数量"};
// 创建表格模型
TableModel model = new DefaultTableModel(book,book_info);
// 根据表格模型类创建二维表格
table_search = new JTable(model);
new SearchResult().setVisible(true);
}}
//当选中的是作者或者是书名的单选按钮时执行以下操作
if(Radio_name.isSelected()||Radio_author.isSelected()){
int m=0;
do{
if(m<=n){
book_search =(Book)obo.readObject();//先读取第一个对象
// 如果文本框中输入的文字出现在某个对象中执行以下操作
if(book_search.toString().contains(textField.getText()))
{
exist =true;
Object [][] book = new Object[8][];
int number=book_search.getNumber();
String name=book_search.getName();
String author=book_search.getAuthor();
String press=book_search.getPress();
int count=book_search.getCount();
Object [] book_searched = new Object[]{number,name,author,press,count};
book[n] = book_searched ;
Object [] book_info = new Object[]{"编号","书名 ","作者","出版社","数量"};
// 创建表格模型
TableModel model = new DefaultTableModel(book,book_info);
// 根据表格模型类创建二维表格
table_search = new JTable(model);
n ++;
new SearchResult().setVisible(true);
}
}
}while(book_search!=null);}
//当没有检索到书的时候显示结果
if(!exist){
JLabel label_result = new JLabel("没有检索到该书!!");
JOptionPane.showConfirmDialog(SearchFrame.this, label_result,"图书管理系统-检索结果", JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
// 关闭流
fr.close();
obo.close();
}catch(InvalidClassException e3)
{
e3.printStackTrace();
}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
}catch(StreamCorruptedException e4){
e4.printStackTrace();
}catch(OptionalDataException e5)
{
e5.printStackTrace();
}catch(FileNotFoundException e6)
{
}
catch (IOException e2) {
// 当所选择的分类没有一个书籍对象时 没有找到此书
JLabel label_result = new JLabel("没有检索到该书!!");
JOptionPane.showConfirmDialog(SearchFrame.this, label_result,"图书管理系统-检索结果", JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
}
});
JButton button_cancel = new JButton("取消",new ImageIcon("cancel.png"));
southPanel.add(button_cancel);
button_cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
SearchFrame.this.setVisible(false);
SearchFrame.this.dispose();
}
});
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
// 获取下拉列表选择对分类
public File getClassName()
{
return (File)comBoBox.getSelectedItem();
}
// 返回检索出来的书籍的JTabel对象
public JTable getJTabel()
{
return table_search;
}
private JButton button_ok;
private JButton button_cancel;
private JComboBox comBoBox;
private JTextField textField ;
private JTable table_search ;
private Book book_search ;
private ButtonGroup bg;
private JRadioButton Radio_number ;
private JRadioButton Radio_name ;
private JRadioButton Radio_author ;
class SearchResult extends InitFrame
{
public SearchResult(){
super("图书管理系统-查询结果","library.png",650,300);
// 关闭窗体时释放资源
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 设置窗体的布局管理器
this.setLayout(new BorderLayout());
// 创建面板 并进行布局
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
//将面板添加到窗体
this.add(centerPanel,BorderLayout.CENTER);
this.add(southPanel,BorderLayout.SOUTH);
//设置面板布局
centerPanel.setLayout(new BorderLayout());
southPanel.setLayout(new FlowLayout());
// 获取父类传入的二维表格并添加入窗体
JTable table_result = SearchFrame.this.getJTabel();
JScrollPane jsp = new JScrollPane(table_result);
centerPanel.add(jsp,BorderLayout.CENTER);
JButton button_ok = new JButton("确定",new ImageIcon("ok.png"));
southPanel.add(button_ok);
button_ok.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
SearchResult.this.setVisible(false);
SearchResult.this.disable();
}
});
}
}
}
————————————————
版权声明:本文为CSDN博主「明明如月学长」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w605283073/article/details/46572401