AddBook 类的简单修改(增加注释文…

简介: AddBook 类的简单修改(增加注释文…

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;

}

相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18755 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10139 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1524 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2680 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2258 0