图书管理系统代码 4SearchFrame (…
来源:互联网 发布:软件防火墙有哪些 编辑:程序博客网 时间:2024/06/05 23:04
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());
// 创建面板
JPanel northPanel = new JPanel();
JPanel northPanel1 = new JPanel();//放置下拉列表
JPanel northPanel2 = new JPanel();//放置检索分类
JPanel centerPanel = new JPanel();//放置文本框和二维表
JPanel southPanel
// 添加面板到窗体
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();// 获取
// 定义按钮组
//
Radio_name
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);
// 添加接受检索输入的文本框
centerPanel.add(textField,BorderLayout.NORTH);
// 添加操作按钮
JButton button_ok = new JButton("确定",newImageIcon("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 = newFile(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][];
Object [] book_info = new Object[]{"编号","书名","作者","出版社","数量"};
// 创建表格模型
TableModel model = new DefaultTableModel(book,book_info);
// 根据表格模型类创建二维表格
table_search = new JTable(model);
//当选中的是作者或者是书名的单选按钮时执行以下操作
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][];
Object [] book_info = new Object[]{"编号","书名","作者","出版社","数量"};
// 创建表格模型
TableModel model = new DefaultTableModel(book,book_info);
// 根据表格模型类创建二维表格
table_search = new JTable(model);
n ++;
}
}
}while(book_search!=null);}
//当没有检索到书的时候显示结果
if(!exist){
JLabel label_result = new JLabel("没有检索到该书!!");
JOptionPane.showConfirmDialog(SearchFrame.this,label_result,"图书管理系统-检索结果",JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_OPTION , newImageIcon("result.png"));
}
// 关闭流
fr.close();
obo.close();
}catch(InvalidClassException e3)
{
e3.printStackTrace();
}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
}catch(StreamCorruptedException
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 , newImageIcon("result.png"));
}
}
});
JButton button_cancel = new JButton("取消",newImageIcon("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
}
// 获取下拉列表选择对分类
public File getClassName()
{
return (File)comBoBox.getSelectedItem();
}
// 返回检索出来的书籍的JTabel对象
public JTable getJTabel()
{
return table_search;
}
private JButton button_ok;
private JButton button_cancel;
private
private JTextField textField ;
private JTable
private Book
private
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
// 设置窗体的布局管理器
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("确定",newImageIcon("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();
}
});
}
}
}
- 图书管理系统代码 4SearchFrame (…
- 图书管理系统代码 3 LibraryMainF…
- 图书管理系统代码 6 AboutFrame (…
- 图书管理系统代码 8AddBookFrame …
- 图书管理系统代码 9 EditpassW (…
- 图书管理系统代码 2 LibraryMain …
- 图书管理系统代码 1 Libraryload …
- 图书管理系统代码 5 Book(图书类…
- 图书管理系统代码 7GBC 网格组布…
- 图书管理系统 工程文件
- 图书管理系统(mysql部分) 初步设…
- 图书管理系统 运行效果
- 图书管理系统 出现的问题
- 图书管理系统添加的新类 10 InitFr…
- 图书管理系统添加的新类 11AddUser…
- 图书管理系统代码
- 图书管理系统 java代码
- 3.3 管理的基本原则--- …
- Java File类思考
- 字体问题
- 图书管理系统 运行效果
- 图书管理系统代码 2 LibraryMain …
- 图书管理系统代码 3 LibraryMainF…
- 图书管理系统代码 4SearchFrame (…
- 图书管理系统代码 5 Book(图书类…
- 图书管理系统代码 6 AboutFrame (…
- 图书管理系统代码 7GBC 网格组布…
- 图书管理系统代码 8AddBookFrame …
- 图书管理系统 出现的问题
- 图书管理系统功能
- 图书管理系统 工程文件
- 图书管理系统代码 9 EditpassW (…