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 AddBook extends InitFrame
{
public AddBook(){
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)
{
//将屏幕的输入转换成Book 类型的对象并写入对应分类下的文件
Book book_add = new Book(AddBook.this.getNumber(),AddBook.this.getName(),AddBook.this.getAuthor(),AddBook.this.getPress(),AddBook.this.getCount());
addBook( book_add);
}
});
//取消键
button_cancel = new JButton("取消",new ImageIcon("cancel.png"));
button_cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
AddBook.this.setVisible(false);//隐藏窗体
AddBook.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 void addBook(Book book_add)
{
//f1表示的是分类文件夹下的各个分类文件夹("文学","计算机"等等)
File f1 =AddBook.this.getClassName();
File file_write = new File(f1.getPath()+"\"+"bookinfo.dat");//获取选中的分类的文件夹下 存放该类下书籍的文件
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(AddBook.this, label_result,"图书管理系统-图书添加", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));
}
// 获取下拉列表选择的分类
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()
{
String name =field_name.getText();
//判断书名是否为空,不为空返回为空弹出对话框进行提示
if(!name.isEmpty())
{
return name;
}else
{
JLabel label_result = new JLabel("书名不可为空,请重新输入!!");
JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加图书", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
}
// 获取输入的作者
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 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;
}