图书管理系统代码  8AddBookFrame …

来源:互联网 发布:软件防火墙有哪些 编辑:程序博客网 时间:2024/06/01 08:54

图书管理系统代码 <wbr> <wbr>8AddBookFrame <wbr>(添加书目)

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridBagLayout;

import java.awt.Image;

import java.awt.Toolkit;

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.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InvalidClassException;

import java.io.NotSerializableException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;


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.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;



public class AddBookFrame extends InitFrame

{

public AddBookFrame(){

super("图书管理系统—添加书目","Library.png",460,340);

//设置布局

this.setLayout(new BorderLayout());

this.setResizable(false);

panel_north = new JPanel();

       panel_north.setLayout(new GridBagLayout());

this.add(panel_north,BorderLayout.CENTER);

panel_south = new JPanel();

panel_south.setLayout(new FlowLayout());

this.add(panel_south,BorderLayout.SOUTH);

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

//设置标签

label_book_class = new JLabel("分类:");

label_number = new JLabel("编号:");

label_name = new JLabel("书名:");

label_author = new JLabel("作者:");

label_press = new JLabel("出版社:");

label_count = new JLabel("数量:");

//设置 下拉列表框 (书籍分类)

File file_Library= new File("E:\\图书管理系统\\分类");

file_class = file_Library.listFiles();

comBoBox= new JComboBox(file_class);

// 设置接受输入的文本域

field_number = new JTextField(WIDTH );

field_name  = new JTextField(WIDTH );

field_author = new JTextField(WIDTH );

field_author = new JTextField(WIDTH );

field_press = new JTextField(WIDTH );

field_count = new JTextField(WIDTH );

button_ok = new JButton("确定",new ImageIcon("ok.png"));

button_ok.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

//f1表示的是分类文件夹下的各个分类文件夹("文学","计算机"等等)

File f1 =AddBookFrame.this.getClassName();

File file_write = newFile(f1.getPath()+"\"+"bookinfo.dat");//获取选中的分类的文件夹下 存放该类下书籍的文件

//将屏幕的输入转换成Book 类型的对象并写入对应分类下的文件

Book book_add = newBook(AddBookFrame.this.getNumber(),AddBookFrame.this.getName(),AddBookFrame.this.getAuthor(),AddBookFrame.this.getPress(),AddBookFrame.this.getCount());

try {

//建立对该文件的文件流

FileOutputStream fr = new FileOutputStream(file_write,true);

//用对象输出流包装该文件输出流

ObjectOutputStream obo = new ObjectOutputStream(fr);

// 将该输入的对象写入 对应分类的文本文件中

obo.writeObject(book_add);

// 关闭流

fr.close();

obo.close();

}catch(NotSerializableException e3)

{

e3.printStackTrace();

}catch(InvalidClassException e2)

{

e2.printStackTrace();

catch (IOException e1) {

System.out.println("写入异常");

e1.printStackTrace();

}


// 将该类下的书籍对象个数写入文件

File f3 = new File(f1.getPath()+"\"+"booknum.dat");

try {

FileInputStream fis = new FileInputStream(f3);

ObjectInputStream oji = new ObjectInputStream(fis);

int temp_int =oji.readInt();//该类下读出存放书籍对象个数的整数

fis.close();

oji.close();

temp_int++;//对其进行加加操作

System.out.println(temp_int);


 // 将变化后的添加书籍对象个数,重新写入文件中

FileOutputStream fos = new FileOutputStream(f3,false);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeInt(temp_int);

// 在这里需要特别特别注意!!

// oos必须先flush()才能关闭 否则老师报错!!

oos.flush();

fos.close();

oos.close();

 

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}catch (IOException e1) {

e1.printStackTrace();

}

JLabel label_result = new JLabel("图书添加成功!!");

JOptionPane.showConfirmDialog(AddBookFrame.this,label_result,"图书管理系统-图书添加",JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, newImageIcon("succes.png"));

}

});

//取消键

button_cancel = new JButton("取消",newImageIcon("cancel.png"));

button_cancel.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

AddBookFrame.this.setVisible(false);//隐藏窗体

AddBookFrame.this.dispose();//释放窗体资源

}

});

panel_north.add(label_book_class,new GBC(0,0).setInsets(2));

panel_north.add(comBoBox,new GBC(1,0).setInsets(2));

panel_north.add(label_number,new GBC(0,1).setInsets(2));

panel_north.add(field_number,new GBC(1,1).setInsets(2));

panel_north.add(label_name,new GBC(0,2).setInsets(2));

panel_north.add(field_name,new GBC(1,2).setInsets(2));

panel_north.add(label_author,new GBC(0,3).setInsets(2));

panel_north.add(field_author,new GBC(1,3).setInsets(2));

panel_north.add(label_press,new GBC(0,4).setInsets(2));

panel_north.add(field_press,new GBC(1,4).setInsets(2));

panel_north.add(label_count,new GBC(0,5).setInsets(2));

panel_north.add(field_count,new GBC(1,5).setInsets(2));

panel_south.add(button_ok);

panel_south.add(button_cancel);

}

public File getClassName()

{

return (File)comBoBox.getSelectedItem();

}

public int getNumber()

{

String num =field_number.getText();

int number = Integer.parseInt(num);

return number;

}

public String getName()

{

return field_name.getText();

}

public String getAuthor()

{

return field_author.getText();

}

public String getPress()

{

return field_press.getText();

}

   public int getCount()

   {

  String cou = field_count.getText();

  int count = Integer.parseInt(cou);

  return count;

   }

  



private static final int WIDTH = 20;//文本域的宽度

private JPanel panel_north;

private JPanel panel_south;

private JLabel label_number;

private JLabel label_book_class;

private JLabel label_name;

private JLabel label_author;

private JLabel label_press;

private JLabel label_count;

private JTextField field_number;

private JComboBox combo_class;

private JTextField field_name;

private JTextField field_author;

private JTextField field_press;

private JTextField field_count;

private File[] file_class;

private JComboBox comBoBox;

private JButton button_ok;

private JButton button_cancel;

}


0 0