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_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 = new File(f1.getPath()+"\"+"bookinfo.dat");//擷取選中的分類的檔案夾下 存放該類下書籍的檔案
//将螢幕的輸入轉換成Book 類型的對象并寫入對應分類下的檔案
Book book_add = new Book(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");
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) {
}catch (IOException e1) {
JLabel label_result = new JLabel("圖書添加成功!!");
JOptionPane.showConfirmDialog(AddBookFrame.this, label_result,"圖書管理系統-圖書添加", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));
});
//取消鍵
button_cancel = new JButton("取消",new ImageIcon("cancel.png"));
button_cancel.addActionListener(new ActionListener(){
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;
————————————————
版權聲明:本文為CSDN部落客「明明如月學長」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:
https://blog.csdn.net/w605283073/article/details/46572409