📄 frmmain.java
字号:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.sql.*;
import javax.swing.*;
public class FrmMain extends JFrame implements ActionListener {
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem3 = new JMenuItem();
JMenu jMenu3 = new JMenu();
JMenuItem jMenuItem4 = new JMenuItem();
AbstractTableModel1 dataModel1 = new AbstractTableModel1();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(dataModel1);
JButton jButton2 = new JButton();
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
JComboBox jComboBox2 = new JComboBox();
JComboBox jComboBox3 = new JComboBox();
JButton jButton3 = new JButton();
GridLayout gridLayout1 = new GridLayout();
JLabel jLabel3 = new JLabel();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem("还书");
JMenuItem jMenuItem8 = new JMenuItem("图书报销");
JMenuItem jMenuItem9 = new JMenuItem("相关统计");
public FrmMain() {
Gload.checkLogin(this);
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
this.setTitle("图书管理");;
try {
jbInit();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frmUnLoad();
}
});
this.initSelect();
this.initBook();
jComboBox1.addItem("书名");
jComboBox1.addItem("作者");
jComboBox1.addItem("简介");
jComboBox1.addItem("出版社");
jComboBox2.addItem("不限借出状态");
jComboBox2.addItem("借出部分");
jComboBox2.addItem("全部借出");
jComboBox2.addItem("全部未借出");
this.setBounds((Gload.de.width - 800)/2, (Gload.de.height-600)/2, 800, 600);
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.getContentPane().setBackground(Color.lightGray);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setJMenuBar(jMenuBar1);
jMenu1.setText("系统");
jMenuItem1.setText("修改密码");
jMenuItem1.addActionListener(this);
jMenuItem2.setText("退出");
jMenuItem2.addActionListener(this);
jMenu2.setText("工具");
jMenuItem3.setText("类别管理");
jMenuItem3.addActionListener(this);
jMenu3.setText("帮助");
jMenuItem4.setText("关于");
jMenuItem4.addActionListener(this);
jButton1.setText("查看详细");
jButton1.addActionListener(this);
jButton2.setToolTipText("");
jButton2.setText("批量报销所选图书");
jButton2.addActionListener(this);
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setText(" 查询关键字:");
jPanel1.setLayout(gridLayout1);
jTextField1.setText("输入关键字");
jLabel2.setText("选择查询方式: 按照");
jButton3.setText("查 询");
jButton3.addActionListener(this);
jLabel3.setText("共n页 当前第n页 每页n条记录");
jButton4.setText("上一页");
jButton4.addActionListener(this);
jButton5.setText("下一页");
jButton5.addActionListener(this);
jMenuItem5.setText("新书入库");
jMenuItem5.addActionListener(this);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu2.add(jMenuItem3);
jMenu2.add(jMenuItem5);
jMenu2.add(jMenuItem6);
jMenu2.add(jMenuItem7);
jMenu2.add(jMenuItem8);
jMenu2.add(jMenuItem9);
jMenuItem6.addActionListener(this);
jMenuItem7.addActionListener(this);
jMenuItem8.addActionListener(this);
jMenuItem9.addActionListener(this);
jMenuItem6.setText("借书");
jMenu3.add(jMenuItem4);
jScrollPane1.getViewport().add(jTable1);
jPanel2.add(jButton1);
jPanel2.add(jButton2);
jPanel2.add(jLabel3);
jPanel2.add(jButton4);
jPanel2.add(jButton5);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField1, null);
jPanel1.add(jLabel2, null);
jPanel1.add(jComboBox1, null);
jPanel1.add(jComboBox2, null);
jPanel1.add(jComboBox3, null);
jPanel1.add(jButton3, null);
}
public void initSelect() {
jComboBox3.removeAllItems();
jComboBox3.addItem("所有类别");
ResultSet rs = Conn.query("select distinct 类名 from 类别");
try {
while (rs.next()) {
jComboBox3.addItem(rs.getString("类名"));
}
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void frmUnLoad() {
if (Gload.Confirm(this, "确认退出吗?") == Gload.YES) {
Gload.exit();
}
}
public void jButton1_actionPerformed(ActionEvent e) {
int sel = this.jTable1.getSelectedRow();
if (-1 == sel) {
Gload.MSG(this, "请先选择一条记录!");
return;
}
Gload.slcID = -1;
String tmp = this.jTable1.getValueAt(sel, 0).toString().trim();
if (tmp.equals("")) return;
try { Gload.slcID = Integer.parseInt(tmp);
} catch(Exception ex) { return; }
new FrmInfo(this, "详细信息", true).show();
}
public void jButton2_actionPerformed(ActionEvent e) {
if (Gload.NO == Gload.Confirm(this, "此操作极其危险!\n你真的要执行吗?")) return;
int[] sle = jTable1.getSelectedRows();
if (sle == null || sle.length == 0) return;
String sql;
for (int i=0; i<sle.length; i++) {
sql = jTable1.getValueAt(sle[i], 0).toString();
if (sql.equals("")) continue;
Conn.exec("delete from 图书信息 where id=" + sql);
Conn.exec("delete from 图书状态 where 书号=" + sql);
Conn.exec("delete from 借书 where 书号=" + sql);
Conn.exec("insert into 运行记录(操作,书号,备注) values('报销', " + sql + ", '全部')");
}
Gload.MSG(this, "删除成功");
this.initBook();
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o.equals(jMenuItem6))
new FrmBorrow(this, "借书", true).show();
else if (o.equals(jMenuItem7))
new FrmReturn(this, "还书", true).show();
else if (o.equals(jMenuItem8))
new FrmDelete(this, "图书报销", true).show();
else if (o.equals(jMenuItem9))
new FrmCount(this, "相关统计", true).show();
else if (o.equals(jMenuItem5))
new FrmAddBook(this, "添加新书", true).show();
else if (o.equals(jMenuItem3))
new ClassMNG(this, "类别管理", true).show();
else if (o.equals(jMenuItem4))
new FrmAbout(this, "关于图书管理系统", true).show();
else if (o.equals(jMenuItem1))
new ChgPwd(this, "修改密码", true).show();
else if (o.equals(jMenuItem2))
frmUnLoad();
else if (o.equals(jButton1))
jButton1_actionPerformed(e);
else if (o.equals(jButton2))
jButton2_actionPerformed(e);
else if (o.equals(jButton3))
this.search();
else if (o.equals(jButton4))
jButton4_actionPerformed(e);
else if (o.equals(jButton5))
jButton5_actionPerformed(e);
}
private int page = 1;
private int totalpage = 1;
public void jButton4_actionPerformed(ActionEvent e) {
if (page == 1) {
Gload.MSG(this, "已经到达第一页");
return;
}
page--;
initBook();
}
public void jButton5_actionPerformed(ActionEvent e) {
if (page == totalpage) {
Gload.MSG(this, "已经到达最后一页");
return;
}
page++;
initBook();
}
String sql = "select 图书信息.id, 书名, 作者,出版社ID,类别ID from 图书信息 where 1=1";
public void initBook() {
String msql = "select AA.id,AA.书名,AA.作者,AA.出版社,BB.类名 from (select a.*, b.出版社 from ("
+ sql + ") as a left join (select * from 出版社) as b on (a.出版社ID = b.id)) as AA left join (select * from 类别) as BB on (AA.类别ID = BB.id)";
int i = 0;
ResultSet rs = Conn.query("select count(id) as CO from (" + msql + ") AS TAB");
int sum = 0;
try {
rs.next();
sum = rs.getInt("CO");
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
return;
}
jLabel3.setText("共" + totalpage + "页 当前第" + page + "页 每页" + Gload.RowNum + "条记录");
if (sum == 0)
totalpage = 1;
else
totalpage = (sum+Gload.RowNum-1)/Gload.RowNum;
System.out.println();
int j = dataModel1.getColumnCount();
try {
rs = Conn.query(msql);
if ((page - 1) * Gload.RowNum > 0) {
// 移动结果集数据到当前页
rs.absolute((page - 1) * Gload.RowNum);
}
dataModel1.data = new String[j][Gload.RowNum];
while (rs.next() && i < Gload.RowNum) {
for (int k = 0; k<j; k++) {
dataModel1.data[k][i] = rs.getString(k+1);
}
i++;
}
rs.close();
} catch(Exception e) {
//e.printStackTrace();
return;
}
jTable1.addNotify();
jTable1.repaint();
}
private void search() {
String slcM = "";
String key = Gload.getText(jTextField1);
if (key == null) key = "";
sql = "select 图书信息.id,书名,作者,出版社ID,类别ID from 图书信息 where 1=1";
if (!key.equals("")) {
slcM = jComboBox1.getSelectedItem().toString();
sql += " and " + slcM + " like '%" + key + "%'";
}
slcM = this.jComboBox2.getSelectedItem().toString();
if (slcM.equals("借出部分")) {
sql += " and (select count(图书状态.id) from 图书状态 where 借出>0 and 库存>0 and 书号=图书信息.id)>0";
}
if (slcM.equals("全部借出")) {
sql += " and (select count(图书状态.id) from 图书状态 where 库存<=0 and 书号=图书信息.id)>0";
}
if (slcM.equals("全部未借出")) {
sql += " and (select count(图书状态.id) from 图书状态 where 借出<=0 and 书号=图书信息.id)>0";
}
slcM = this.jComboBox3.getSelectedItem().toString();
if (slcM != null && !slcM.equals("") && !slcM.equals("所有类别")) {
sql += " and (select count(类别.id) from 类别 where 类名='" + slcM + "' and 类别.id=图书信息.类别ID)>0";
}
page = 1;
this.initBook();
}
}
class AbstractTableModel1 extends AbstractTableModel {
private String[] title = {"自动编号", "书名", "作者", "出版社", "类别"};
public String[][] data;
public AbstractTableModel1() {
data = new String[title.length][Gload.RowNum];
}
public int getColumnCount() { return title.length; }
public int getRowCount() { return Gload.RowNum;}
public Object getValueAt(int row, int col) {
if (data[col][row] == null)
data[col][row] = "";
return data[col][row];
}
public String getColumnName(int column){ return title[column];}//设置表格列名
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -