import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.print.PrinterException;
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.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
public class LibraryMainF extends InitFrame
{
private static final long serialVersionUID = 1L;
public LibraryMainF()
{
super("图书管理系统—主界面","Library.png",850,600);
//设置系统托盘
TrayIcon trayIcon =null;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
this.setResizable(false);//窗体不可调节大小
//设置系统图标显示的右键菜单
PopupMenu popup = new PopupMenu();
MenuItem exit_pop = new MenuItem("退出");
MenuItem showMainF_pop = new MenuItem("显示主界面");
exit_pop.addActionListener(listener);
showMainF_pop.addActionListener(listener);
popup.add(showMainF_pop);
popup.addSeparator();
popup.add(exit_pop);
trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage("library_tray.png"), "图书管理系统", popup);
trayIcon.addActionListener(listener);
//为托盘添加鼠标事件 当单击或者双击系统托盘图标时 显示 主界面
trayIcon.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e)
{
// 当单击左键时将主界面设为可见
if(e.getButton()==MouseEvent.BUTTON1)
LibraryMainF.this.setVisible(true);
}
public void mouseEntered(MouseEvent e1) {
}
public void mouseExited(MouseEvent e2) {
}
public void mousePressed(MouseEvent e3) {
}
public void mouseReleased(MouseEvent e4) {
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
if (trayIcon != null) {
trayIcon.setImage(Toolkit.getDefaultToolkit().getImage("library_tray.png"));
}
}
//添加设置菜单
JMenuBar bar = new JMenuBar();
JMenu menu_book = new JMenu("图书");
JMenu menu_edit = new JMenu("编辑");
JMenu menu_help = new JMenu("帮助");
this.setJMenuBar(bar);
bar.add(menu_book);
bar.add(menu_edit);
bar.add(menu_help);
menuItem_add = new JMenuItem("添书(A)",new ImageIcon("add.png"));
//设置助记符
menuItem_add.setMnemonic('A');
//设置加速键
menuItem_add.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_A,
InputEvent.CTRL_MASK));
//添加监听器
menuItem_add.addActionListener(listener);
menuItem_add_class = new JMenuItem("添类(C)",new ImageIcon("add_class.png"));
//设置助记符
menuItem_add_class.setMnemonic('C');
//设置加速键
menuItem_add_class.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_C,
InputEvent.CTRL_MASK));
//添加监听器
menuItem_add_class.addActionListener(listener);
menuItem_search = new JMenuItem("查询(S)",new ImageIcon("search.png"));
menuItem_search.setMnemonic('S');
menuItem_search.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuItem_search.addActionListener(listener);
menuItem_del = new JMenuItem("借出(B)",new ImageIcon("del.png"));
menuItem_del.setMnemonic('B');
menuItem_del.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_B,
InputEvent.CTRL_MASK));
menuItem_del.addActionListener(listener);
menuItem_print = new JMenuItem("打印(P)",new ImageIcon("print.png"));
menuItem_print.setMnemonic('P');
menuItem_print.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_P,
InputEvent.CTRL_MASK));
menuItem_print.addActionListener(listener);
menuItem_exit = new JMenuItem("退出(E)",new ImageIcon("exit.png"));
menuItem_exit.setMnemonic('E');
menuItem_exit.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_E,
InputEvent.CTRL_MASK));
menuItem_exit.addActionListener(listener);
menu_book.add(menuItem_add);
menu_book.add(menuItem_add_class);
menu_book.add(menuItem_search);
menu_book.add(menuItem_del);
menu_book.add(menuItem_print);
menu_book.addSeparator();
menu_book.add(menuItem_exit);
JMenuItem menuItem_edit_passW = new JMenuItem("改密(W)",new ImageIcon("edit_paasW.png"));
menuItem_edit_passW.setMnemonic('W');
menuItem_edit_passW.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_W,
InputEvent.CTRL_MASK));
menuItem_edit_passW.addActionListener(listener);
JMenuItem menuItem_new_user = new JMenuItem("新用户(N)",new ImageIcon("new_user.png"));
menuItem_new_user.setMnemonic('N');
menuItem_new_user.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_N,
InputEvent.CTRL_MASK));
menuItem_new_user.addActionListener(listener);
menu_edit.add(menuItem_edit_passW);
menu_edit.addSeparator();
menu_edit.add(menuItem_new_user);
JMenuItem menuItem_help = new JMenuItem("帮助文档(H)",new ImageIcon("help.png"));
menuItem_help.setMnemonic('S');
menuItem_help.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuItem_help.addActionListener(listener);
JMenuItem menuItem_about = new JMenuItem("关于(T)",new ImageIcon("about.png"));
menuItem_about.setMnemonic('T');
menuItem_about.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_T,
InputEvent.CTRL_MASK));
menuItem_about.addActionListener(listener);
menu_help.add(menuItem_help);
menu_help.addSeparator();
menu_help.add(menuItem_about);
this.setLayout(new BorderLayout());
//添加面板
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
this.add(northPanel,BorderLayout.NORTH);
this.add(southPanel,BorderLayout.CENTER);
northPanel.setLayout(new BorderLayout());
southPanel.setLayout(new BorderLayout());
//设置工具箱
JToolBar toolBar = new JToolBar();
toolBar.setEnabled(false);
JButton button_add =new JButton(new ImageIcon("add.png"));
button_add.setToolTipText("添书");
button_add.setActionCommand("添书(A)");
toolBar.add(button_add);
button_add.addActionListener(listener);
JButton button_add_class = new JButton(new ImageIcon("add_class.png"));
button_add_class.setToolTipText("添类");
button_add_class.setActionCommand("添类(C)");
toolBar.add(button_add_class);
button_add_class.addActionListener(listener);
JButton button_search =new JButton(new ImageIcon("search.png"));
button_search.setToolTipText("查询");
button_search.setActionCommand("查询(S)");
toolBar.add(button_search);
button_search.addActionListener(listener);
JButton button_del =new JButton(new ImageIcon("del.png"));
button_del.setToolTipText("借出");
button_del.setActionCommand("借出(B)");
toolBar.add(button_del);
button_del.addActionListener(listener);
JButton button_print = new JButton(new ImageIcon("print.png"));
button_print.setToolTipText("打印");
button_print.setActionCommand("打印(P)");
toolBar.add(button_print);
button_print.addActionListener(listener);
JButton button_refresh =new JButton(new ImageIcon("refresh.png"));
button_refresh.setToolTipText("刷新");
button_refresh.setActionCommand("刷新(R)");
toolBar.add(button_refresh);
button_refresh.addActionListener(listener);
northPanel.add(toolBar,BorderLayout.WEST);
//添加右键菜单
pop = new JPopupMenu();
southPanel.setComponentPopupMenu(pop);
pop_add = new JMenuItem("添书(A)",new ImageIcon("add.png"));
pop_add.addActionListener(listener);
pop_add_class = new JMenuItem("添类(C)");
pop_add_class.addActionListener(listener);
pop_search = new JMenuItem("查询(S)",new ImageIcon("search.png"));
pop_search.addActionListener(listener);
pop_del = new JMenuItem("借出(B)",new ImageIcon("del.png"));
pop_del.addActionListener(listener);
pop_exit = new JMenuItem("退出(E)",new ImageIcon("exit.png"));
pop_exit.addActionListener(listener);
pop.add(pop_add);
pop.add(pop_search);
pop.add(pop_del);
pop.add(pop_exit);
southPanel.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
if(pop.isPopupTrigger(event))
pop.show(LibraryMainF.this,event.getX(),event.getY());
}
});
//创建 图书分类的下拉列表
File file_Library= new File("E:\\图书管理系统\\分类");
file_class = file_Library.listFiles();
comBo= new JComboBox(file_class);
comBo.setSelectedItem("E:\\图书管理系统\\分类\\文学");
northPanel.add(comBo,BorderLayout.EAST);
comBo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
// 根据数据模型创建二维表格
table_MainF = new JTable(initTabelModel());
// 为二维表格添加滚动窗格
js = new JScrollPane(table_MainF);
// 设置主界面中的二维表格中被选中的行的前景色
table_MainF.setSelectionForeground(Color.ORANGE) ;
// 设置列为不可拖动
table_MainF.getTableHeader().setReorderingAllowed(false);
setAlternateColor(table_MainF);
// 将滚动窗格添加入面板
southPanel.add(js,BorderLayout.CENTER);
}
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("退出")||e.getActionCommand().equals("退出(E)"))
{
System.exit(0);
}else if(e.getActionCommand().equals("显示主界面"))
{
LibraryMainF.this.setVisible(true);
}else if(e.getActionCommand().equals("添书(A)"))
{
AddBookFrame abf= new AddBookFrame();
abf.setVisible(true);
}else if (e.getActionCommand().equals("添类(C)"))
{
JLabel label = new JLabel("请输入需要添加的图书分类:");
String selection = JOptionPane.showInputDialog(pop_add_class,label, "图书管理系统-添加图书分类", JOptionPane.OK_CANCEL_OPTION);
if(selection.equals(str_null))
{
JLabel label_result = new JLabel("你输入的书籍分类为空,请重新输入!!");
JOptionPane.showConfirmDialog(pop_add_class, label_result,"图书管理系统-加添分类—输入为空", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("null.png"));
}else
{
File f_class = new File("E:\\图书管理系统\\分类\"+selection);
if(f_class .exists())//如果分类已经存在给予提示
{
JLabel label_result = new JLabel("你输入的书籍分类已存在!!");
JOptionPane.showConfirmDialog(pop_add_class, label_result,"图书管理系统-分类已存在", JOptionPane.WARNING_MESSAGE,JOptionPane.OK_OPTION , new ImageIcon("result.png"));
}
if(!f_class .exists()){//添加的分类不存在的时候 进行添加
f_class .mkdir();
File f_book = new File("E:\\图书管理系统\\分类\"+selection+"\"+"bookinfo.dat");
File f_book_num = new File("E:\\图书管理系统\\分类\"+selection+"\"+"booknum.dat");
try {
FileWriter fo = new FileWriter(f_book);
String str = "";
fo.write(str);
fo.close();
} catch (IOException e1) {
e1.printStackTrace();
}
// 初始化booknum.dat
int num = 0;
try {
FileOutputStream fo = new FileOutputStream(f_book_num);
ObjectOutputStream oos = new ObjectOutputStream(fo);
oos.writeInt(num);
oos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}catch (IOException e5) {
System.out.println("初始化写入异常");
e5.printStackTrace();
}
JLabel label_result = new JLabel(selection+"分类创建成功!!");
JOptionPane.showConfirmDialog(pop_add_class, label_result,"图书管理系统-添加成功", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon("succes.png"));
}}}
else if(e.getActionCommand().equals("查询(S)"))
{
SearchFrame sf = new SearchFrame();
sf.setVisible(true);
}else if(e.getActionCommand().equals("借出(B)"))
{
}else if(e.getActionCommand().equals("打印(P)"))
{
try {
table_MainF.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}else if(e.getActionCommand().equals("改密(W)"))
{
EditpassW edit_pass_word = new EditpassW();
edit_pass_word.setVisible(true);
}else if (e.getActionCommand().equals("新用户(N)"))
{
AddUserFrame addFrame =new AddUserFrame();
addFrame.setVisible(true);
}
else if(e.getActionCommand().equals("刷新(R)"))
{
}else if(e.getActionCommand().equals("关于(T)"))
{
AboutFrame af = new AboutFrame();
System.out.println("he");
af.setVisible(true);
}} };
//获得下拉列表的选项
public File getClassName()
{
return (File)comBo.getSelectedItem();
}
//实现隔一行换一个背景色 的功能
public void setAlternateColor(JTable table)
{
//构造渲染器
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer()
{
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if(row%2==0)
{
this.setBackground(Color.PINK);
}else
this.setBackground(Color.WHITE);
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
};
// 对每一行运用渲染器
for (int i = 0 ;i<table_MainF.getColumnCount();i++)
{
TableColumn column = table.getColumn(table.getColumnName(i));
column.setCellRenderer(cellRenderer);
}
}
public TableModel initTabelModel()
{
Book book_read;
// 首先读取该分类下的书籍对象个数的文件 读取对象个数
File file_book_num= new File("E:\\图书管理系统\\分类\\书法\\booknum.dat");
try {
FileInputStream fi = new FileInputStream(file_book_num);
ObjectInputStream oi = new ObjectInputStream(fi);
System.out.println(num);
num =oi.readInt();
System.out.println(num);
fi.close();
oi.close();
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}catch(InvalidClassException e4)
{
e4.printStackTrace();
}catch(StreamCorruptedException e5)
{
e5.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
if(num!=0){
data = new Object[num][5];//存放分类下的书籍信息
Object [] clumnNames = {"编号","书名 ","作者","出版社","数量"};
// 读取书籍信息的文件
File file_book_info = new File("E:\\图书管理系统\\分类\\书法\\bookinfo.dat");
try {
FileInputStream fis = new FileInputStream(file_book_info);
ObjectInputStream ois = new ObjectInputStream(fis);
int j=0;
while(j<num)
{
book_read = (Book)ois.readObject();
// 将书籍数据遍历 存放到temp数组中 然后传入data
Object [] temp2 = new Object[]{book_read.getNumber(),book_read.getName(),book_read.getAuthor(),book_read.getPress(),book_read.getCount()};
data[j]= temp2;
j++;
}
model_default = new DefaultTableModel(data,clumnNames);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
if(num==0)
{
model_default = new DefaultTableModel();
data = new Object[1][];//存放分类下的书籍信息
Object [] clumnNames = {"编号","书名 ","作者","出版社","数量"};
Object[] temp = new Object[]{"","","","","",""};
data[0]= temp;
model_default = new DefaultTableModel(data,clumnNames);
}
return model_default ;
}
private File[] file_class ;
private JTable table_MainF;
private JMenuItem menuItem_add;
private JMenuItem menuItem_search ;
private JMenuItem menuItem_del;
private JMenuItem menuItem_print;
private JMenuItem menuItem_exit ;
private JMenuItem menuItem_add_class;
private JMenuItem pop_add;//右键菜单
private JMenuItem pop_add_class;
private JMenuItem pop_search;
private JMenuItem pop_del;
private JMenuItem pop_exit;
private JPopupMenu pop ;
private JScrollPane js;
private JComboBox comBo;
;
private Book book_main;
private TableModel model;
TableModel model_default;
private Object[][] book;
private Object [][] data ;
private int num =0;
private String str_null ="";
}
————————————————
版权声明:本文为CSDN博主「明明如月学长」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w605283073/article/details/46572399