当前位置:   article > 正文

数据库期末设计——图书管理系统_windowbuilder设计图书馆管理系统

windowbuilder设计图书馆管理系统

 

目录

1.前置软件以及开发环境:  

2.开发过程讲解

代码环节:

数据库代码

 1.BookDao.java

2.BookTypeDao.java

3.UserDao.java

4.Book.java

5.BookType.java

6.User.java

7.DbUtil.java

8.Stringutil.java

9.BookAddInterFrm.java

10.BookManageInterFrm.java

11.BookTypeAddInterFrm.java

12.BookTypeManagerInterFrm.java

13.Java666interframe.java

14.Login.java

15.Mainframe.java

3.结果展示:

4.结尾体会心得:

5.网盘地址分享:


本次设计基于eclipse+Javaswing+windowBuilder+JDBC+mysql实现,我会从头到尾对整个实现过程进行详细的讲解,最后我也会献上我的全部文件希望对大家有所帮助!!

1.前置软件以及开发环境:  

安装流程我就不在这里多说了,网上都有,我这里将一些要点。

eclipse:博主选用的是2022-12的版本比较稳定,其他版本也可以,IDEA也是可以的,不是很建议用vscode,虽然集成但是终归没有专门的软件好使。

Java swing以及window Builder:这些都是插件,具体步骤可以去网上搜,Javaswing是一个库,安装完毕不需要管,这边着重讲一下window Builder。

首先就是安装:很多人安装的时候看软件右下角的进度条读完或者等不及了就直接退出eclipse了,这样子是不对的,一定要等安装成功后会有一个小弹窗出来让你重启eclipse,这样子才会成功,如果你要是安装失败了,要么重装eclipse,要么就打开eclipse等待一会儿,看看能否继续安装,博主就是第二种情况,打开后等待几分钟就安装好了。

然后是使用:

找到你们的项目位置,右键src选择新建,然后最底下有个选项“其他”,找到其中的windowBuilder就可以创建窗口了,具体可以自己操作下。

Mysql:这个应该不用我多说,大家都有安装的肯定,这边讲一下好用的操作端,博主用的是datagrip,但是这个要付费大家可以去搜搜破解版(博主自己就是破解的)。还有就是Navicat也是不错的,实际上都是可以的只要能够建立数据库即可。

注意:有很多小伙伴开始做的时候啥也不会,认为eclipse一定要连接数据库什么的,实际上完全不需要,这些我们会在代码里面进行操作。

2.开发过程讲解

首先我们需要建立至少四个包,如下图:

第一次写这个可能都不是很了解我就简单讲一下我的理解:

1.dao层,就是用于导入导出数据的,简单点来说我们要在里面写一些数据库的sql语句

2.model层,就是模型层,就是在里面写具体的实体类,再明白点就是你可以把你数据库里面的一个表看成一种类,几个表就建几个类。

3.util层,这个是工具层,你可以在里面写一些方法以便里面的使用,我后续也会进行讲解。

4.view层,这个就是视图层,你的窗口都写在这个里面,一般来说你的程序也会从这个里面的主界面开始运行。

5.至于image:这个就是用来存放你的一些图片方便调用

最后一点:一定要下载最后的那个引用的库,这边可以去网上找然后直接拖拽进去就好了,当然最后我也会将文件分享给大家,这个是数据库连接驱动,没下可连接不了哦。

代码环节:

数据库代码

首先先给大家一个数据库生成代码防止大家搞不出数据库:

下面没写创建数据库的语句,额就自己创建一下,数据库名称叫db_book,写错了代码可就跑不动了。

  1. create table t_booktype
  2. (
  3. id int auto_increment
  4. primary key,
  5. bookTypeName varchar(20) null,
  6. bookTpeDesc varchar(1000) null
  7. );
  8. create table t_book
  9. (
  10. id int auto_increment
  11. primary key,
  12. bookName varchar(20) not null,
  13. author varchar(20) null,
  14. sex varchar(10) null,
  15. price float null,
  16. bookTypeId int null,
  17. bookDesc varchar(1000) null,
  18. constraint t_book_t_booktype_id_fk
  19. foreign key (bookTypeId) references t_booktype (id)
  20. );
  21. create table t_user
  22. (
  23. id int auto_increment
  24. primary key,
  25. username varchar(20) not null,
  26. password varchar(20) not null
  27. );

 直接粘贴在你的数据库查询台里就行了,可以自己看看效果,数据不重要可以自己添加。

这个其实也不是很好讲,我就一个一个来了,先给大家看下整个代码的样子(如果大家最后发现字符集什么的不行导致乱码就建议直接粘贴代码跑一下比较好):

如果有需要特别注意的地方我会指出,其他的无脑复制即可。

 1.BookDao.java

  1. package com.java1234.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction;
  6. import com.java1234.model.Book;
  7. import com.java1234.util.Stringutil;
  8. /**
  9. * book添加和删除
  10. * @author 46476
  11. *
  12. */
  13. public class BookDao {
  14. /**
  15. * 图书添加
  16. * @param con
  17. * @param book
  18. * @return
  19. * @throws Exception
  20. */
  21. public int add(Connection con,Book book)throws Exception{
  22. String sql="insert into t_book values(null,?,?,?,?,?,?)";
  23. PreparedStatement pstmt=con.prepareStatement(sql);
  24. pstmt.setString(1, book.getBookName());
  25. pstmt.setString(2, book.getAuthor());
  26. pstmt.setString(3, book.getSex());
  27. pstmt.setFloat(4, book.getPrice());
  28. pstmt.setInt(5, book.getBookTypeId());
  29. pstmt.setString(6, book.getBookTypeDesc());
  30. return pstmt.executeUpdate();
  31. }
  32. /**
  33. * 图书信息查询
  34. * @param con
  35. * @param book
  36. * @return
  37. * @throws Exception
  38. */
  39. public ResultSet list(Connection con,Book book)throws Exception{
  40. StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
  41. if(Stringutil.isNotEmpty(book.getBookName())) {
  42. sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
  43. }
  44. if(Stringutil.isNotEmpty(book.getAuthor())) {
  45. sb.append(" and b.author like '%"+book.getAuthor()+"%'");
  46. }
  47. if(book.getBookTypeId()!=null&&book.getBookTypeId()!=-1) {
  48. sb.append(" and b.bookTypeId="+book.getBookTypeId());
  49. }
  50. PreparedStatement pstmt=con.prepareStatement(sb.toString());
  51. return pstmt.executeQuery();
  52. }
  53. /**
  54. * 删除记录条数
  55. * @param con
  56. * @param id
  57. * @return
  58. * @throws Exception
  59. */
  60. public int delete(Connection con,String id)throws Exception{
  61. String sql="delete from t_book where id=?";
  62. PreparedStatement pstmt=con.prepareStatement(sql);
  63. pstmt.setString(1, id);
  64. return pstmt.executeUpdate();
  65. }
  66. public int updata(Connection con,Book book)throws Exception {
  67. String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookTypeId=?,bookDesc=? where id=?";
  68. PreparedStatement pstmt=con.prepareStatement(sql);
  69. pstmt.setString(1, book.getBookName());
  70. pstmt.setString(2, book.getAuthor());
  71. pstmt.setString(3, book.getSex());
  72. pstmt.setFloat(4, book.getPrice());
  73. pstmt.setInt(5, book.getBookTypeId());
  74. pstmt.setString(6, book.getBookTypeDesc());//这边之前写错了应该是bookDesc
  75. pstmt.setInt(7, book.getId());
  76. return pstmt.executeUpdate();
  77. }
  78. /**
  79. * 指定图书类别下是否存在图书
  80. * @param con
  81. * @param bookTypeId
  82. * @return
  83. * @throws Exception
  84. */
  85. public boolean existBook(Connection con,String bookTypeId)throws Exception {
  86. String sql="select * from t_book where bookTypeId=?";
  87. PreparedStatement pstmt=con.prepareStatement(sql);
  88. pstmt.setString(1, bookTypeId);
  89. ResultSet rs=pstmt.executeQuery();
  90. return rs.next();
  91. }
  92. }

2.BookTypeDao.java

  1. package com.java1234.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import com.java1234.model.BookType;
  6. import com.java1234.util.Stringutil;
  7. /**
  8. * 图书类别dao类
  9. * @author 46476
  10. *
  11. */
  12. public class BookTypeDao {
  13. /**
  14. * 图书类别添加
  15. * @param con
  16. * @param bookType
  17. * @return
  18. * @throws Exception
  19. */
  20. public int add(Connection con,BookType bookType)throws Exception{
  21. String sql="insert into t_bookType value(null,?,?)";
  22. PreparedStatement pstmt=con.prepareStatement(sql);
  23. pstmt.setString(1, bookType.getBookTypeName());
  24. pstmt.setString(2,bookType.getBookTypeDesc() );
  25. return pstmt.executeUpdate();
  26. }
  27. /**
  28. * 查询图书类别
  29. * @param con
  30. * @param bookType
  31. * @return
  32. * @throws Exception
  33. */
  34. public ResultSet list(Connection con,BookType bookType)throws Exception{
  35. StringBuffer sb=new StringBuffer("select * from t_bookType");
  36. if(Stringutil.isNotEmpty(bookType.getBookTypeName())) {
  37. sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
  38. }
  39. PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
  40. return pstmt.executeQuery();
  41. }
  42. /**
  43. * 删除图书类别
  44. * @param con
  45. * @param id
  46. * @return
  47. * @throws Exception
  48. */
  49. public int delete(Connection con,String id)throws Exception{
  50. String sql="delete from t_bookType where id=?";
  51. PreparedStatement pstmt=con.prepareStatement(sql);
  52. pstmt.setString(1, id);
  53. return pstmt.executeUpdate();
  54. }
  55. /**
  56. * 跟新图书类别
  57. * @param con
  58. * @param bookType
  59. * @return
  60. * @throws Exception
  61. */
  62. public int updata(Connection con,BookType bookType)throws Exception{
  63. String sql="update t_bookType set bookTypeName=?,bookTpeDesc=? where id=?";
  64. PreparedStatement pstmt=con.prepareStatement(sql);
  65. pstmt.setString(1, bookType.getBookTypeName());
  66. pstmt.setString(2, bookType.getBookTypeDesc());
  67. pstmt.setInt(3, bookType.getId());
  68. return pstmt.executeUpdate();
  69. }
  70. }

3.UserDao.java

  1. package com.java1234.dao;
  2. import java.nio.channels.SelectableChannel;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import com.java1234.model.User;
  7. /**
  8. * 用户dao类
  9. * @author 46476
  10. *
  11. */
  12. public class UserDao {
  13. public User login(Connection con,User user)throws Exception {
  14. User resultUser=null;
  15. String sql="select * from t_user where userName=? and password=?";
  16. PreparedStatement pstmt=con.prepareStatement(sql);
  17. pstmt.setString(1, user.getUserNameString());
  18. pstmt.setString(2, user.getPasswordString());
  19. ResultSet rs=pstmt.executeQuery();
  20. if(rs.next()) {
  21. resultUser=new User();
  22. resultUser.setId(rs.getInt("id"));
  23. resultUser.setUserNameString(rs.getString("userName"));
  24. resultUser.setPasswordString(rs.getString("password"));
  25. }
  26. return resultUser;
  27. }
  28. }

4.Book.java

  1. package com.java1234.model;
  2. import java.sql.Connection;
  3. import com.mysql.cj.protocol.a.NativeConstants.StringLengthDataType;
  4. public class Book {
  5. private int id;
  6. private String bookName;
  7. private String author;
  8. private String sex;
  9. private Float price;
  10. private Integer bookTypeId;
  11. private String bookTypeName;
  12. private String bookTypeDesc;
  13. public Book() {
  14. super();
  15. // TODO 自动生成的构造函数存根
  16. }
  17. public Book(int id, String bookName, String author, String sex, Float price, Integer bookTypeId,
  18. String bookTypeDesc) {
  19. super();
  20. this.id = id;
  21. this.bookName = bookName;
  22. this.author = author;
  23. this.sex = sex;
  24. this.price = price;
  25. this.bookTypeId = bookTypeId;
  26. this.bookTypeDesc = bookTypeDesc;
  27. }
  28. public Book(String bookName, String author, Integer bookTypeId) {
  29. super();
  30. this.bookName = bookName;
  31. this.author = author;
  32. this.bookTypeId = bookTypeId;
  33. }
  34. public Book(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookTypeDesc) {
  35. super();
  36. this.bookName = bookName;
  37. this.author = author;
  38. this.sex = sex;
  39. this.price = price;
  40. this.bookTypeId = bookTypeId;
  41. this.bookTypeDesc = bookTypeDesc;
  42. }
  43. public int getId() {
  44. return id;
  45. }
  46. public void setId(int id) {
  47. this.id = id;
  48. }
  49. public String getBookName() {
  50. return bookName;
  51. }
  52. public void setBookName(String bookName) {
  53. this.bookName = bookName;
  54. }
  55. public String getAuthor() {
  56. return author;
  57. }
  58. public void setAuthor(String author) {
  59. this.author = author;
  60. }
  61. public String getSex() {
  62. return sex;
  63. }
  64. public void setSex(String sex) {
  65. this.sex = sex;
  66. }
  67. public Float getPrice() {
  68. return price;
  69. }
  70. public void setPrice(Float price) {
  71. this.price = price;
  72. }
  73. public Integer getBookTypeId() {
  74. return bookTypeId;
  75. }
  76. public void setBookTypeId(Integer bookTypeId) {
  77. this.bookTypeId = bookTypeId;
  78. }
  79. public String getBookTypeName() {
  80. return bookTypeName;
  81. }
  82. public void setBookTypeName(String bookTypeName) {
  83. this.bookTypeName = bookTypeName;
  84. }
  85. public String getBookTypeDesc() {
  86. return bookTypeDesc;
  87. }
  88. public void setBookTypeDesc(String bookTypeDesc) {
  89. this.bookTypeDesc = bookTypeDesc;
  90. }
  91. }

5.BookType.java

  1. package com.java1234.model;
  2. /**
  3. * 图书类别实体
  4. * @author 46476
  5. *
  6. */
  7. public class BookType {
  8. private int id;//编号
  9. private String bookTypeName;//图书类别名称
  10. private String bookTypeDesc;//图书备注
  11. public BookType() {
  12. super();
  13. }
  14. public BookType(int id, String bookTypeName, String bookTypeDesc) {
  15. super();
  16. this.id = id;
  17. this.bookTypeName = bookTypeName;
  18. this.bookTypeDesc = bookTypeDesc;
  19. }
  20. public BookType(String bookTypeName,String bookTypeDesc) {
  21. // TODO 自动生成的构造函数存根
  22. this.bookTypeName=bookTypeName;
  23. this.bookTypeDesc=bookTypeDesc;
  24. }
  25. public int getId() {
  26. return id;
  27. }
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31. public String getBookTypeName() {
  32. return bookTypeName;
  33. }
  34. public void setBookTypeName(String bookTypeName) {
  35. this.bookTypeName = bookTypeName;
  36. }
  37. public String getBookTypeDesc() {
  38. return bookTypeDesc;
  39. }
  40. public void setBookTypeDesc(String bookTypeDesc) {
  41. this.bookTypeDesc = bookTypeDesc;
  42. }
  43. public String toString() {
  44. return this.bookTypeName;
  45. }
  46. }

6.User.java

  1. package com.java1234.model;
  2. /**
  3. * 用户实体
  4. * @author 46476
  5. *
  6. */
  7. public class User {
  8. private int id;//编号
  9. private String userNameString;//用户名
  10. private String passwordString;//密码
  11. public User() {
  12. super();
  13. }
  14. public User(String userNameString, String passwordString) {
  15. super();
  16. this.userNameString = userNameString;
  17. this.passwordString = passwordString;
  18. }
  19. public int getId() {
  20. return id;
  21. }
  22. public void setId(int id) {
  23. this.id = id;
  24. }
  25. public String getUserNameString() {
  26. return userNameString;
  27. }
  28. public void setUserNameString(String userNameString) {
  29. this.userNameString = userNameString;
  30. }
  31. public String getPasswordString() {
  32. return passwordString;
  33. }
  34. public void setPasswordString(String passwordString) {
  35. this.passwordString = passwordString;
  36. }
  37. }

7.DbUtil.java

这里注意啦!!!!!!!!!!!!!!!

这里面的用户名以及密码都是要填写自己的数据库用户名以及密码,也就是我打xxxx的地方。

  1. package com.java1234.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. /**
  5. * 数据库工具类
  6. * @author 46476
  7. *
  8. */
  9. public class DbUtil {
  10. private String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//数据库地址
  11. private String dbUserName="xxxxxx";//用户名
  12. private String dbPassword="xxxxxxxx";//密码
  13. private String jdbcNameString="com.mysql.cj.jdbc.Driver";//驱动名称
  14. public Connection getCon()throws Exception{
  15. Class.forName(jdbcNameString);
  16. Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
  17. return con;
  18. }
  19. public void closeCon(Connection con)throws Exception{
  20. if(con!=null)
  21. con.close();
  22. }
  23. public static void main(String[] args) {
  24. DbUtil dbUtil=new DbUtil();
  25. try {
  26. dbUtil.getCon();
  27. System.out.println("数据库连接成功");
  28. } catch (Exception e) {
  29. // TODO 自动生成的 catch 块
  30. e.printStackTrace();
  31. System.out.println("连接失败");
  32. }
  33. }
  34. }

8.Stringutil.java

  1. package com.java1234.util;
  2. /**
  3. * 字符串工具类
  4. * @author 46476
  5. *
  6. */
  7. public class Stringutil {
  8. //判断字符串是否为空
  9. public static boolean isEmpty(String str) {
  10. if(str==null||"".equals(str.trim())) {
  11. return true;
  12. }
  13. return false;
  14. }
  15. public static boolean isNotEmpty(String str) {
  16. if(str!=null&&"".equals(str.trim())==false)
  17. return true;
  18. return false;
  19. }
  20. }

 

从这里开始我需要提一嘴:中间的一大段代码都是不用看的,这些都是windowBuilder帮你自动生成的,你只需要知道怎么微调就行了,不会的无脑cv,会的可以自己调成喜欢的样子。 

9.BookAddInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import java.awt.Font;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import javax.swing.JTextField;
  12. import javax.swing.LayoutStyle.ComponentPlacement;
  13. import javax.swing.border.LineBorder;
  14. import com.java1234.dao.BookDao;
  15. import com.java1234.dao.BookTypeDao;
  16. import com.java1234.model.Book;
  17. import com.java1234.model.BookType;
  18. import com.java1234.util.DbUtil;
  19. import com.java1234.util.Stringutil;
  20. import javax.swing.JRadioButton;
  21. import javax.swing.ButtonGroup;
  22. import javax.swing.JTextArea;
  23. import javax.swing.JComboBox;
  24. import javax.swing.JButton;
  25. import javax.swing.ImageIcon;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28. public class BookAddInterFrm extends JInternalFrame {
  29. private static final long serialVersionUID = 1L;
  30. private JTextField bookNametxt;
  31. private JTextField authortxt;
  32. private final ButtonGroup buttonGroup = new ButtonGroup();
  33. private JTextField pricetxt;
  34. private JTextArea bookDesctxt;
  35. private JComboBox bookTypeJcb;
  36. private DbUtil dbUtil=new DbUtil();
  37. private BookTypeDao bookTypeDao=new BookTypeDao();
  38. private BookDao bookDao=new BookDao();
  39. private JRadioButton manjrb;
  40. JRadioButton womenjrb;
  41. /**
  42. * Launch the application.
  43. */
  44. public static void main(String[] args) {
  45. EventQueue.invokeLater(new Runnable() {
  46. public void run() {
  47. try {
  48. BookAddInterFrm frame = new BookAddInterFrm();
  49. frame.setVisible(true);
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. });
  55. }
  56. /**
  57. * Create the frame.
  58. */
  59. public BookAddInterFrm() {
  60. setClosable(true);
  61. setIconifiable(true);
  62. setTitle("图书添加");
  63. setBounds(100, 100, 602, 705);
  64. JLabel lblNewLabel = new JLabel("图书名称:");
  65. lblNewLabel.setBounds(53, 82, 65, 18);
  66. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  67. bookNametxt = new JTextField();
  68. bookNametxt.setBounds(136, 79, 126, 24);
  69. bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  70. bookNametxt.setColumns(10);
  71. JLabel lblNewLabel_1 = new JLabel("图书作者:");
  72. lblNewLabel_1.setBounds(336, 82, 55, 18);
  73. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  74. authortxt = new JTextField();
  75. authortxt.setBounds(409, 79, 126, 24);
  76. authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  77. authortxt.setColumns(10);
  78. JLabel lblNewLabel_2 = new JLabel("作者性别:");
  79. lblNewLabel_2.setBounds(53, 168, 65, 18);
  80. lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  81. manjrb = new JRadioButton("男");
  82. manjrb.setBounds(136, 164, 39, 27);
  83. buttonGroup.add(manjrb);
  84. manjrb.setSelected(true);
  85. manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  86. womenjrb = new JRadioButton("女");
  87. womenjrb.setBounds(193, 164, 39, 27);
  88. buttonGroup.add(womenjrb);
  89. womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  90. JLabel lblNewLabel_3 = new JLabel("图书价格:");
  91. lblNewLabel_3.setBounds(336, 168, 65, 18);
  92. lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  93. pricetxt = new JTextField();
  94. pricetxt.setBounds(411, 165, 126, 24);
  95. pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  96. pricetxt.setColumns(10);
  97. JLabel lblNewLabel_4 = new JLabel("图书描述:");
  98. lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  99. lblNewLabel_4.setBounds(53, 353, 65, 15);
  100. bookDesctxt = new JTextArea();
  101. bookDesctxt.setBounds(136, 348, 399, 242);
  102. getContentPane().setLayout(null);
  103. getContentPane().add(lblNewLabel);
  104. getContentPane().add(bookNametxt);
  105. getContentPane().add(lblNewLabel_4);
  106. getContentPane().add(lblNewLabel_2);
  107. getContentPane().add(manjrb);
  108. getContentPane().add(womenjrb);
  109. getContentPane().add(bookDesctxt);
  110. getContentPane().add(lblNewLabel_1);
  111. getContentPane().add(authortxt);
  112. getContentPane().add(lblNewLabel_3);
  113. getContentPane().add(pricetxt);
  114. JLabel lblNewLabel_5 = new JLabel("图书类别:");
  115. lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  116. lblNewLabel_5.setBounds(53, 273, 65, 15);
  117. getContentPane().add(lblNewLabel_5);
  118. bookTypeJcb = new JComboBox();
  119. bookTypeJcb.setBounds(136, 270, 126, 23);
  120. getContentPane().add(bookTypeJcb);
  121. JButton btnNewButton = new JButton("添加");
  122. btnNewButton.addActionListener(new ActionListener() {
  123. public void actionPerformed(ActionEvent e) {
  124. bookAddActionPerformed(e);
  125. }
  126. });
  127. btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/添加.png")));
  128. btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  129. btnNewButton.setBounds(220, 620, 93, 23);
  130. getContentPane().add(btnNewButton);
  131. JButton btnNewButton_1 = new JButton("重置");
  132. btnNewButton_1.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent e) {
  134. resetValueActionPerformed(e);
  135. }
  136. });
  137. btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/重置.png")));
  138. btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  139. btnNewButton_1.setBounds(370, 620, 93, 23);
  140. getContentPane().add(btnNewButton_1);
  141. bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  142. bookDesctxt.setLineWrap(true); //激活自动换行功能
  143. bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
  144. fillBookType();
  145. }
  146. protected void resetValueActionPerformed(ActionEvent e) {
  147. // TODO 自动生成的方法存根
  148. this.resetValue();
  149. }
  150. /**
  151. * 图书添加处理
  152. * @param e
  153. */
  154. protected void bookAddActionPerformed(ActionEvent e) {
  155. // TODO 自动生成的方法存根
  156. String bookName=this.bookNametxt.getText();
  157. String author=this.authortxt.getText();
  158. String price=this.pricetxt.getText();
  159. String bookDesc=this.bookDesctxt.getText();
  160. if(Stringutil.isEmpty(bookName)) {
  161. JOptionPane.showConfirmDialog(null, "图书名称不能为空");
  162. return;
  163. }
  164. if(Stringutil.isEmpty(author)) {
  165. JOptionPane.showConfirmDialog(null, "作者不能为空");
  166. return;
  167. }
  168. if(Stringutil.isEmpty(price)) {
  169. JOptionPane.showConfirmDialog(null, "图书价格不能为空");
  170. return;
  171. }
  172. String sex="";
  173. if(manjrb.isSelected()) {
  174. sex="男";
  175. }
  176. else if(womenjrb.isSelected()) {
  177. sex="女";
  178. }
  179. BookType bookType=(BookType)bookTypeJcb.getSelectedItem();
  180. int bookTypeId = bookType.getId();
  181. Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
  182. Connection con=null;
  183. try {
  184. con=dbUtil.getCon();
  185. int addNum=bookDao.add(con,book);
  186. if(addNum==1) {
  187. JOptionPane.showConfirmDialog(null, "图书添加成功");
  188. this.resetValue();
  189. return ;
  190. }
  191. else {
  192. JOptionPane.showConfirmDialog(null, "图书添加失败");
  193. }
  194. } catch (Exception e2) {
  195. // TODO: handle exception
  196. JOptionPane.showConfirmDialog(null, "图书添加失败");
  197. e2.printStackTrace();
  198. }finally {
  199. try {
  200. dbUtil.closeCon(con);
  201. } catch (Exception e1) {
  202. // TODO 自动生成的 catch 块
  203. e1.printStackTrace();
  204. }
  205. }
  206. }
  207. /**
  208. * 重置表单
  209. */
  210. private void resetValue() {
  211. this.bookNametxt.setText("");
  212. this.pricetxt.setText("");
  213. this.authortxt.setText("");
  214. this.manjrb.setSelected(true);
  215. this.bookDesctxt.setText("");
  216. //意思就是说如果有下拉框选项,那么就默认选回第一个
  217. if(this.bookTypeJcb.getItemCount()>0) {
  218. this.bookTypeJcb.setSelectedIndex(0);
  219. }
  220. }
  221. /**
  222. * 初始化图书类别下拉框
  223. */
  224. private void fillBookType() {
  225. Connection con=null;
  226. BookType bookType=null;
  227. try {
  228. con=dbUtil.getCon();
  229. ResultSet rs =bookTypeDao.list(con, new BookType());
  230. while(rs.next()) {
  231. bookType=new BookType();
  232. bookType.setId(rs.getInt("id"));
  233. bookType.setBookTypeName(rs.getString("bookTypeName"));
  234. this.bookTypeJcb.addItem(bookType);
  235. }
  236. } catch (Exception e) {
  237. // TODO: handle exception
  238. e.printStackTrace();
  239. }finally {
  240. }
  241. }
  242. }

10.BookManageInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.table.DefaultTableModel;
  9. import com.java1234.dao.BookDao;
  10. import com.java1234.dao.BookTypeDao;
  11. import com.java1234.model.Book;
  12. import com.java1234.model.BookType;
  13. import com.java1234.util.DbUtil;
  14. import com.java1234.util.Stringutil;
  15. import com.mysql.cj.util.EscapeTokenizer;
  16. import javax.swing.LayoutStyle.ComponentPlacement;
  17. import javax.swing.JPanel;
  18. import javax.swing.border.TitledBorder;
  19. import javax.swing.border.EtchedBorder;
  20. import javax.swing.border.LineBorder;
  21. import java.awt.Color;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import java.awt.Font;
  25. import java.sql.Connection;
  26. import java.sql.ResultSet;
  27. import java.util.Vector;
  28. import javax.swing.JTextField;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JButton;
  31. import javax.swing.ImageIcon;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.ActionEvent;
  34. import javax.swing.JRadioButton;
  35. import javax.swing.ButtonGroup;
  36. import javax.swing.JTextArea;
  37. import java.awt.event.MouseAdapter;
  38. import java.awt.event.MouseEvent;
  39. public class BookManageInterFrm extends JInternalFrame {
  40. private static final long serialVersionUID = 1L;
  41. private JTable booktable;
  42. private JTextField s_bookNametxt;
  43. private JTextField s_authortxt;
  44. private JComboBox s_bookTypejcb;
  45. private DbUtil dbUtil=new DbUtil();
  46. private BookTypeDao bookTypeDao=new BookTypeDao();
  47. private BookDao bookDao=new BookDao();
  48. private JTextField idtxt;
  49. private JTextField bookNametxt;
  50. private final ButtonGroup buttonGroup = new ButtonGroup();
  51. private JTextField pricetxt;
  52. private JTextField authortxt;
  53. private JRadioButton manjrb;
  54. private JRadioButton womenjrb;
  55. private JTextArea bookDesctxt;
  56. private JComboBox bookTypejcb;
  57. /**
  58. * Launch the application.
  59. */
  60. public static void main(String[] args) {
  61. EventQueue.invokeLater(new Runnable() {
  62. public void run() {
  63. try {
  64. BookManageInterFrm frame = new BookManageInterFrm();
  65. frame.setVisible(true);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. });
  71. }
  72. /**
  73. * Create the frame.
  74. */
  75. public BookManageInterFrm() {
  76. setClosable(true);
  77. setIconifiable(true);
  78. setTitle("图书管理");
  79. setBounds(100, 100, 743, 728);
  80. JScrollPane scrollPane = new JScrollPane();
  81. JPanel panel = new JPanel();
  82. panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
  83. JPanel panel_1 = new JPanel();
  84. panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  85. GroupLayout groupLayout = new GroupLayout(getContentPane());
  86. groupLayout.setHorizontalGroup(
  87. groupLayout.createParallelGroup(Alignment.TRAILING)
  88. .addGroup(groupLayout.createSequentialGroup()
  89. .addGap(21)
  90. .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
  91. .addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 673, Short.MAX_VALUE)
  92. .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
  93. .addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  94. .addGap(33))
  95. );
  96. groupLayout.setVerticalGroup(
  97. groupLayout.createParallelGroup(Alignment.LEADING)
  98. .addGroup(groupLayout.createSequentialGroup()
  99. .addContainerGap()
  100. .addComponent(panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
  101. .addGap(34)
  102. .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
  103. .addGap(18)
  104. .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
  105. .addContainerGap())
  106. );
  107. JLabel lblNewLabel_3 = new JLabel("编号:");
  108. lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  109. idtxt = new JTextField();
  110. idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  111. idtxt.setEnabled(false);
  112. idtxt.setColumns(10);
  113. JLabel lblNewLabel_4 = new JLabel("图书名称:");
  114. lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  115. bookNametxt = new JTextField();
  116. bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  117. bookNametxt.setColumns(10);
  118. JLabel lblNewLabel_5 = new JLabel("作者性别:");
  119. lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  120. manjrb = new JRadioButton("男");
  121. buttonGroup.add(manjrb);
  122. manjrb.setSelected(true);
  123. manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  124. womenjrb = new JRadioButton("女");
  125. buttonGroup.add(womenjrb);
  126. womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  127. JLabel lblNewLabel_6 = new JLabel("价格:");
  128. lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  129. pricetxt = new JTextField();
  130. pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  131. pricetxt.setColumns(10);
  132. JLabel lblNewLabel_7 = new JLabel("图书作者:");
  133. lblNewLabel_7.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  134. authortxt = new JTextField();
  135. authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  136. authortxt.setColumns(10);
  137. JLabel lblNewLabel_8 = new JLabel("图书类别:");
  138. lblNewLabel_8.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  139. bookTypejcb = new JComboBox();
  140. bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  141. JLabel lblNewLabel_9 = new JLabel("图书描述:");
  142. lblNewLabel_9.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  143. bookDesctxt = new JTextArea();
  144. JButton btnNewButton_1 = new JButton("修改");
  145. btnNewButton_1.addActionListener(new ActionListener() {
  146. public void actionPerformed(ActionEvent e) {
  147. bookUpdataActionEvent(e);
  148. }
  149. });
  150. btnNewButton_1.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/修改.png")));
  151. btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  152. JButton btnNewButton_2 = new JButton("删除");
  153. btnNewButton_2.addActionListener(new ActionListener() {
  154. public void actionPerformed(ActionEvent e) {
  155. bookDeleteActionEvent(e);
  156. }
  157. });
  158. btnNewButton_2.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/删除.png")));
  159. btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  160. GroupLayout gl_panel_1 = new GroupLayout(panel_1);
  161. gl_panel_1.setHorizontalGroup(
  162. gl_panel_1.createParallelGroup(Alignment.LEADING)
  163. .addGroup(gl_panel_1.createSequentialGroup()
  164. .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  165. .addGroup(gl_panel_1.createSequentialGroup()
  166. .addGap(28)
  167. .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  168. .addGroup(gl_panel_1.createSequentialGroup()
  169. .addComponent(lblNewLabel_9)
  170. .addPreferredGap(ComponentPlacement.RELATED)
  171. .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 523, GroupLayout.PREFERRED_SIZE))
  172. .addGroup(gl_panel_1.createSequentialGroup()
  173. .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  174. .addGroup(gl_panel_1.createSequentialGroup()
  175. .addComponent(lblNewLabel_3)
  176. .addPreferredGap(ComponentPlacement.RELATED)
  177. .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  178. .addGap(18)
  179. .addComponent(lblNewLabel_4)
  180. .addPreferredGap(ComponentPlacement.RELATED)
  181. .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  182. .addGroup(gl_panel_1.createSequentialGroup()
  183. .addComponent(lblNewLabel_6)
  184. .addPreferredGap(ComponentPlacement.RELATED)
  185. .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  186. .addGap(18)
  187. .addComponent(lblNewLabel_7)
  188. .addPreferredGap(ComponentPlacement.RELATED)
  189. .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
  190. .addGap(40)
  191. .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  192. .addGroup(gl_panel_1.createSequentialGroup()
  193. .addComponent(lblNewLabel_5)
  194. .addPreferredGap(ComponentPlacement.RELATED)
  195. .addComponent(manjrb)
  196. .addGap(18)
  197. .addComponent(womenjrb))
  198. .addGroup(gl_panel_1.createSequentialGroup()
  199. .addComponent(lblNewLabel_8)
  200. .addPreferredGap(ComponentPlacement.RELATED)
  201. .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))))))
  202. .addGroup(gl_panel_1.createSequentialGroup()
  203. .addGap(167)
  204. .addComponent(btnNewButton_1)
  205. .addGap(129)
  206. .addComponent(btnNewButton_2)))
  207. .addContainerGap(8, Short.MAX_VALUE))
  208. );
  209. gl_panel_1.setVerticalGroup(
  210. gl_panel_1.createParallelGroup(Alignment.LEADING)
  211. .addGroup(gl_panel_1.createSequentialGroup()
  212. .addContainerGap()
  213. .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  214. .addComponent(lblNewLabel_5)
  215. .addComponent(lblNewLabel_4)
  216. .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  217. .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  218. .addComponent(lblNewLabel_3)
  219. .addComponent(manjrb)
  220. .addComponent(womenjrb))
  221. .addGap(57)
  222. .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  223. .addComponent(lblNewLabel_6)
  224. .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  225. .addComponent(lblNewLabel_7)
  226. .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  227. .addComponent(lblNewLabel_8)
  228. .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  229. .addGap(46)
  230. .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  231. .addComponent(lblNewLabel_9)
  232. .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
  233. .addGap(18)
  234. .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  235. .addComponent(btnNewButton_1)
  236. .addComponent(btnNewButton_2))
  237. .addContainerGap(23, Short.MAX_VALUE))
  238. );
  239. panel_1.setLayout(gl_panel_1);
  240. JLabel lblNewLabel = new JLabel("图书名称:");
  241. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  242. s_bookNametxt = new JTextField();
  243. s_bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  244. s_bookNametxt.setColumns(10);
  245. JLabel lblNewLabel_1 = new JLabel("作者:");
  246. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  247. s_authortxt = new JTextField();
  248. s_authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  249. s_authortxt.setColumns(10);
  250. JLabel lblNewLabel_2 = new JLabel("图书类别:");
  251. lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  252. s_bookTypejcb = new JComboBox();
  253. s_bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  254. JButton btnNewButton = new JButton("查询");
  255. btnNewButton.addActionListener(new ActionListener() {
  256. public void actionPerformed(ActionEvent e) {
  257. bookSearchActionPerformed(e);
  258. }
  259. });
  260. btnNewButton.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/查询.png")));
  261. btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  262. GroupLayout gl_panel = new GroupLayout(panel);
  263. gl_panel.setHorizontalGroup(
  264. gl_panel.createParallelGroup(Alignment.LEADING)
  265. .addGroup(gl_panel.createSequentialGroup()
  266. .addContainerGap()
  267. .addComponent(lblNewLabel)
  268. .addPreferredGap(ComponentPlacement.RELATED)
  269. .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  270. .addGap(24)
  271. .addComponent(lblNewLabel_1)
  272. .addPreferredGap(ComponentPlacement.RELATED)
  273. .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  274. .addGap(32)
  275. .addComponent(lblNewLabel_2)
  276. .addPreferredGap(ComponentPlacement.RELATED)
  277. .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  278. .addComponent(btnNewButton)
  279. .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))
  280. .addContainerGap(31, Short.MAX_VALUE))
  281. );
  282. gl_panel.setVerticalGroup(
  283. gl_panel.createParallelGroup(Alignment.LEADING)
  284. .addGroup(gl_panel.createSequentialGroup()
  285. .addContainerGap()
  286. .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  287. .addComponent(lblNewLabel)
  288. .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  289. .addComponent(lblNewLabel_1)
  290. .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  291. .addComponent(lblNewLabel_2)
  292. .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  293. .addPreferredGap(ComponentPlacement.UNRELATED)
  294. .addComponent(btnNewButton)
  295. .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  296. );
  297. panel.setLayout(gl_panel);
  298. booktable = new JTable();
  299. booktable.addMouseListener(new MouseAdapter() {
  300. @Override
  301. public void mousePressed(MouseEvent e) {
  302. bookTableMousePressed(e);
  303. }
  304. });
  305. booktable.setShowGrid(false);
  306. scrollPane.setViewportView(booktable);
  307. booktable.setModel(new DefaultTableModel(
  308. new Object[][] {
  309. },
  310. new String[] {
  311. "\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u4F5C\u8005\u6027\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B"
  312. }
  313. ) {
  314. boolean[] columnEditables = new boolean[] {
  315. true, false, false, false, false, false, false
  316. };
  317. public boolean isCellEditable(int row, int column) {
  318. return columnEditables[column];
  319. }
  320. });
  321. booktable.getColumnModel().getColumn(5).setPreferredWidth(173);
  322. getContentPane().setLayout(groupLayout);
  323. this.fillBookType("search");
  324. this.fillBookType("modify");
  325. this.fillTable(new Book());
  326. bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  327. bookDesctxt.setLineWrap(true); //激活自动换行功能
  328. bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
  329. }
  330. /**
  331. * 图书删除事件处理
  332. * @param e
  333. */
  334. protected void bookDeleteActionEvent(ActionEvent e) {
  335. // TODO 自动生成的方法存根
  336. String idString=idtxt.getText();
  337. if(Stringutil.isEmpty(idString)) {
  338. JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
  339. return;
  340. }
  341. int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
  342. if(n==0) {
  343. Connection con=null;
  344. try {
  345. con=dbUtil.getCon();
  346. int deteleNum=bookDao.delete(con, idString);
  347. if(deteleNum==1) {
  348. JOptionPane.showConfirmDialog(null, "删除成功");
  349. this.fillTable(new Book());
  350. this.resetValue();
  351. }
  352. else {
  353. JOptionPane.showConfirmDialog(null, "删除失败");
  354. }
  355. } catch (Exception e2) {
  356. // TODO: handle exception
  357. e2.printStackTrace();
  358. }finally {
  359. try {
  360. dbUtil.closeCon(con);
  361. } catch (Exception e1) {
  362. // TODO 自动生成的 catch 块
  363. e1.printStackTrace();
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * 图书修改事件处理
  370. * @param e
  371. */
  372. protected void bookUpdataActionEvent(ActionEvent e) {
  373. // TODO 自动生成的方法存根
  374. String id=this.idtxt.getText();
  375. if(Stringutil.isEmpty(id)) {
  376. JOptionPane.showConfirmDialog(null, "请选择一条记录");
  377. return;
  378. }
  379. String bookName=this.bookNametxt.getText();
  380. String author=this.authortxt.getText();
  381. String price=this.pricetxt.getText();
  382. String bookDesc=this.bookDesctxt.getText();
  383. if(Stringutil.isEmpty(bookName)) {
  384. JOptionPane.showConfirmDialog(null, "图书名称不能为空");
  385. return;
  386. }
  387. if(Stringutil.isEmpty(author)) {
  388. JOptionPane.showConfirmDialog(null, "作者不能为空");
  389. return;
  390. }
  391. if(Stringutil.isEmpty(price)) {
  392. JOptionPane.showConfirmDialog(null, "图书价格不能为空");
  393. return;
  394. }
  395. String sex=null;
  396. if(manjrb.isSelected()) {
  397. sex="男";
  398. }
  399. else {
  400. sex="女";
  401. }
  402. BookType bookType=(BookType) bookTypejcb.getSelectedItem();
  403. int bookTypeId=bookType.getId();
  404. Book book=new Book(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId,
  405. bookDesc);
  406. Connection con=null;
  407. try {
  408. con=dbUtil.getCon();
  409. int addNum=bookDao.updata(con,book);
  410. if(addNum==1) {
  411. JOptionPane.showConfirmDialog(null, "图书修改成功");
  412. this.resetValue();
  413. this.fillTable(new Book());
  414. return ;
  415. }
  416. else {
  417. JOptionPane.showConfirmDialog(null, "图书修改失败");
  418. }
  419. } catch (Exception e2) {
  420. // TODO: handle exception
  421. JOptionPane.showConfirmDialog(null, "图书修改失败");
  422. e2.printStackTrace();
  423. }finally {
  424. try {
  425. dbUtil.closeCon(con);
  426. } catch (Exception e1) {
  427. // TODO 自动生成的 catch 块
  428. e1.printStackTrace();
  429. }
  430. }
  431. }
  432. private void resetValue() {
  433. // TODO 自动生成的方法存根
  434. this.idtxt.setText("");
  435. this.bookNametxt.setText("");
  436. this.pricetxt.setText("");
  437. this.authortxt.setText("");
  438. this.manjrb.setSelected(true);
  439. this.bookDesctxt.setText("");
  440. //意思就是说如果有下拉框选项,那么就默认选回第一个
  441. if(this.bookTypejcb.getItemCount()>0) {
  442. this.bookTypejcb.setSelectedIndex(0);
  443. }
  444. }
  445. /**
  446. * 表格点击事件处理
  447. * @param e
  448. */
  449. protected void bookTableMousePressed(MouseEvent e) {
  450. // TODO 自动生成的方法存根
  451. int row=this.booktable.getSelectedRow();
  452. this.idtxt.setText((String) booktable.getValueAt(row, 0));
  453. this.bookNametxt.setText((String) booktable.getValueAt(row, 1));
  454. this.authortxt.setText((String) booktable.getValueAt(row, 2));
  455. String sex=(String) booktable.getValueAt(row, 3);
  456. if(sex.equals("男")) {
  457. this.manjrb.setSelected(true);
  458. }
  459. else {
  460. this.womenjrb.setSelected(true);
  461. }
  462. this.pricetxt.setText((Float) booktable.getValueAt(row, 4)+"");
  463. this.bookDesctxt.setText((String) booktable.getValueAt(row, 5));
  464. String bookTypeName=(String) this.booktable.getValueAt(row, 6);
  465. int n=this.bookTypejcb.getItemCount();
  466. for(int i=0;i<n;i++) {
  467. BookType item=(BookType) this.bookTypejcb.getItemAt(i);
  468. if(item.getBookTypeName().equals(bookTypeName)) {
  469. this.bookTypejcb.setSelectedIndex(i);
  470. }
  471. }
  472. }
  473. /**
  474. * 图书查询事件
  475. * @param e
  476. */
  477. protected void bookSearchActionPerformed(ActionEvent e) {
  478. // TODO 自动生成的方法存根
  479. String bookName=this.s_bookNametxt.getText();
  480. String authorString=this.s_authortxt.getText();
  481. BookType bookType=(BookType)this.s_bookTypejcb.getSelectedItem();
  482. int bookTypeId=bookType.getId();
  483. Book book=new Book(bookName,authorString,bookTypeId);
  484. this.fillTable(book);
  485. }
  486. /**
  487. * 初始化下拉框
  488. * @param Type
  489. */
  490. private void fillBookType(String Type) {
  491. Connection con=null;
  492. BookType bookType=null;
  493. try {
  494. con=dbUtil.getCon();
  495. ResultSet rs=bookTypeDao.list(con, new BookType());
  496. //设置默认
  497. if("search".equals(Type)) {
  498. bookType=new BookType();
  499. bookType.setBookTypeName("请选择...");
  500. bookType.setId(-1);
  501. this.s_bookTypejcb.addItem(bookType);
  502. }
  503. while(rs.next()) {
  504. bookType=new BookType();
  505. bookType.setBookTypeName(rs.getString("bookTypeName"));
  506. bookType.setId(rs.getInt("id"));
  507. if("search".equals(Type)) {
  508. this.s_bookTypejcb.addItem(bookType);
  509. }
  510. else if("modify".equals(Type)) {
  511. this.bookTypejcb.addItem(bookType);
  512. }
  513. }
  514. } catch (Exception e) {
  515. // TODO: handle exception
  516. e.printStackTrace();
  517. }finally {
  518. try {
  519. dbUtil.closeCon(con);
  520. } catch (Exception e) {
  521. // TODO 自动生成的 catch 块
  522. e.printStackTrace();
  523. }
  524. }
  525. }
  526. /**
  527. * 初始化表格数据
  528. * @param book
  529. */
  530. private void fillTable(Book book) {
  531. DefaultTableModel dtm=(DefaultTableModel)booktable.getModel();
  532. dtm.setRowCount(0);//设置成0
  533. Connection con=null;
  534. try {
  535. con = dbUtil.getCon();
  536. } catch (Exception e3) {
  537. // TODO 自动生成的 catch 块
  538. e3.printStackTrace();
  539. }
  540. try {
  541. ResultSet rs=bookDao.list(con, book);
  542. while(rs.next()) {
  543. Vector v=new Vector();
  544. v.add(rs.getString("id"));
  545. v.add(rs.getString("bookName"));
  546. v.add(rs.getString("author"));
  547. v.add(rs.getString("sex"));
  548. v.add(rs.getFloat("price"));
  549. v.add(rs.getString("bookDesc"));
  550. v.add(rs.getString("bookTypeName"));
  551. dtm.addRow(v);
  552. }
  553. } catch (Exception e) {
  554. // TODO: handle exception
  555. e.printStackTrace();
  556. }finally {
  557. try {
  558. dbUtil.closeCon(con);
  559. } catch (Exception e) {
  560. // TODO 自动生成的 catch 块
  561. e.printStackTrace();
  562. }
  563. }
  564. }
  565. }

11.BookTypeAddInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JTextField;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JButton;
  11. import javax.swing.LayoutStyle.ComponentPlacement;
  12. import javax.swing.border.LineBorder;
  13. import com.java1234.dao.BookTypeDao;
  14. import com.java1234.model.BookType;
  15. import com.java1234.util.DbUtil;
  16. import com.java1234.util.Stringutil;
  17. import java.awt.Font;
  18. import javax.swing.ImageIcon;
  19. import java.awt.event.ActionListener;
  20. import java.sql.Connection;
  21. import java.awt.event.ActionEvent;
  22. public class BookTypeAddInterFrm extends JInternalFrame {
  23. private static final long serialVersionUID = 1L;
  24. private JTextField bookTypeNametxt;
  25. private JTextArea bookTypeDesctxt;
  26. private DbUtil dbUtil = new DbUtil();
  27. private BookTypeDao bookTypeDao=new BookTypeDao();
  28. /**
  29. * Launch the application.
  30. */
  31. public static void main(String[] args) {
  32. EventQueue.invokeLater(new Runnable() {
  33. public void run() {
  34. try {
  35. BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
  36. frame.setVisible(true);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. });
  42. }
  43. /**
  44. * Create the frame.
  45. */
  46. public BookTypeAddInterFrm() {
  47. setClosable(true);
  48. setTitle("图书类别添加");
  49. setBounds(100, 100, 537, 300);
  50. JLabel lblNewLabel = new JLabel("图书类别名称:");
  51. lblNewLabel.setBounds(78, 64, 111, 18);
  52. lblNewLabel.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/导入.png")));
  53. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  54. JLabel lblNewLabel_1 = new JLabel("图书类别描述:");
  55. lblNewLabel_1.setBounds(78, 137, 111, 18);
  56. lblNewLabel_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/project.png")));
  57. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  58. bookTypeNametxt = new JTextField();
  59. bookTypeNametxt.setBounds(199, 61, 216, 24);
  60. bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  61. bookTypeNametxt.setColumns(10);
  62. bookTypeDesctxt = new JTextArea();
  63. bookTypeDesctxt.setBounds(199, 135, 216, 79);
  64. bookTypeDesctxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  65. JButton btnNewButton = new JButton("添加");
  66. btnNewButton.addActionListener(new ActionListener() {
  67. public void actionPerformed(ActionEvent e) {
  68. bookTypeAddActionPerformed(e);
  69. }
  70. });
  71. btnNewButton.setBounds(124, 233, 79, 27);
  72. btnNewButton.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/添加.png")));
  73. btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  74. JButton btnNewButton_1 = new JButton("重置");
  75. btnNewButton_1.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent e) {
  77. resetValueActionPerformed(e);
  78. }
  79. });
  80. btnNewButton_1.setBounds(253, 233, 79, 27);
  81. btnNewButton_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/重置.png")));
  82. btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  83. getContentPane().setLayout(null);
  84. getContentPane().add(lblNewLabel);
  85. getContentPane().add(bookTypeNametxt);
  86. getContentPane().add(lblNewLabel_1);
  87. getContentPane().add(btnNewButton);
  88. getContentPane().add(btnNewButton_1);
  89. getContentPane().add(bookTypeDesctxt);
  90. //设置文本域边框
  91. bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  92. bookTypeDesctxt.setLineWrap(true); //激活自动换行功能
  93. bookTypeDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
  94. }
  95. /**
  96. * 图书类别添加事件
  97. * @param e
  98. */
  99. protected void bookTypeAddActionPerformed(ActionEvent e) {
  100. // TODO 自动生成的方法存根
  101. String bookTypeName=this.bookTypeNametxt.getText();
  102. String bookTypeDesc=this.bookTypeDesctxt.getText();
  103. if(Stringutil.isEmpty(bookTypeName)) {
  104. JOptionPane.showConfirmDialog(null, "图书类别名称不能为空");
  105. return;
  106. }
  107. BookType bookType=new BookType(bookTypeName,bookTypeDesc);
  108. Connection con = null;
  109. try {
  110. con = dbUtil.getCon();
  111. } catch (Exception e3) {
  112. // TODO 自动生成的 catch 块
  113. e3.printStackTrace();
  114. }
  115. try {
  116. int n=bookTypeDao.add(con, bookType);
  117. if(n==1) {
  118. JOptionPane.showConfirmDialog(null, "图书类别添加成功");
  119. resetValue();
  120. return;
  121. }
  122. else {
  123. JOptionPane.showConfirmDialog(null, "添加失败");
  124. }
  125. } catch (Exception e2) {
  126. // TODO: handle exception
  127. e2.printStackTrace();
  128. JOptionPane.showConfirmDialog(null, "添加失败");
  129. }finally {
  130. try {
  131. dbUtil.closeCon(con);
  132. } catch (Exception e1) {
  133. // TODO 自动生成的 catch 块
  134. e1.printStackTrace();
  135. }
  136. }
  137. }
  138. //重置事件处理
  139. protected void resetValueActionPerformed(ActionEvent e) {
  140. // TODO 自动生成的方法存根
  141. this.resetValue();
  142. }
  143. /**
  144. * 重置表单
  145. */
  146. private void resetValue() {
  147. this.bookTypeNametxt.setText("");
  148. this.bookTypeDesctxt.setText("");
  149. }
  150. }

12.BookTypeManagerInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.util.Vector;
  6. import javax.swing.JInternalFrame;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.GroupLayout;
  9. import javax.swing.GroupLayout.Alignment;
  10. import javax.swing.JTable;
  11. import javax.swing.table.DefaultTableModel;
  12. import com.java1234.dao.BookDao;
  13. import com.java1234.dao.BookTypeDao;
  14. import com.java1234.model.BookType;
  15. import com.java1234.util.DbUtil;
  16. import com.java1234.util.Stringutil;
  17. import javax.swing.border.LineBorder;
  18. import java.awt.Color;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import java.awt.Font;
  22. import javax.swing.JTextField;
  23. import javax.swing.LayoutStyle.ComponentPlacement;
  24. import javax.swing.JButton;
  25. import javax.swing.ImageIcon;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28. import javax.swing.JPanel;
  29. import javax.swing.border.TitledBorder;
  30. import javax.swing.JTextArea;
  31. import java.awt.event.MouseAdapter;
  32. import java.awt.event.MouseEvent;
  33. public class BookTypeManagerInterFrm extends JInternalFrame {
  34. private static final long serialVersionUID = 1L;
  35. private JTable bookTypeTable;
  36. private DbUtil dbUtil = new DbUtil();
  37. private BookTypeDao bookTypeDao=new BookTypeDao();
  38. private BookDao bookDao=new BookDao();
  39. private JTextField s_bookTypeNametxt;
  40. private JTextField idtxt;
  41. private JTextField bookTypeNametxt;
  42. private JTextArea bookTypeDesctxt;
  43. /**
  44. * Launch the application.
  45. */
  46. public static void main(String[] args) {
  47. EventQueue.invokeLater(new Runnable() {
  48. public void run() {
  49. try {
  50. BookTypeManagerInterFrm frame = new BookTypeManagerInterFrm();
  51. frame.setVisible(true);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. });
  57. }
  58. /**
  59. * Create the frame.
  60. */
  61. public BookTypeManagerInterFrm() {
  62. setClosable(true);
  63. setIconifiable(true);
  64. setTitle("图书类别管理");
  65. setBounds(100, 100, 678, 622);
  66. JScrollPane scrollPane = new JScrollPane();
  67. JLabel lblNewLabel = new JLabel("图书类别名称:");
  68. lblNewLabel.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/project.png")));
  69. lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  70. s_bookTypeNametxt = new JTextField();
  71. s_bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  72. s_bookTypeNametxt.setColumns(10);
  73. JButton btnNewButton = new JButton("查询");
  74. btnNewButton.addActionListener(new ActionListener() {
  75. public void actionPerformed(ActionEvent e) {
  76. bookTypeSearchActionPerformed(e);
  77. }
  78. });
  79. btnNewButton.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/查询.png")));
  80. btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  81. JPanel panel = new JPanel();
  82. panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  83. GroupLayout groupLayout = new GroupLayout(getContentPane());
  84. groupLayout.setHorizontalGroup(
  85. groupLayout.createParallelGroup(Alignment.LEADING)
  86. .addGroup(groupLayout.createSequentialGroup()
  87. .addGap(48)
  88. .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
  89. .addGroup(groupLayout.createSequentialGroup()
  90. .addComponent(lblNewLabel)
  91. .addPreferredGap(ComponentPlacement.UNRELATED)
  92. .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
  93. .addGap(45)
  94. .addComponent(btnNewButton))
  95. .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
  96. .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  97. .addContainerGap(57, Short.MAX_VALUE))
  98. );
  99. groupLayout.setVerticalGroup(
  100. groupLayout.createParallelGroup(Alignment.LEADING)
  101. .addGroup(groupLayout.createSequentialGroup()
  102. .addGap(42)
  103. .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
  104. .addComponent(lblNewLabel)
  105. .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  106. .addComponent(btnNewButton))
  107. .addGap(27)
  108. .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
  109. .addGap(38)
  110. .addComponent(panel, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
  111. .addContainerGap())
  112. );
  113. JLabel lblNewLabel_1 = new JLabel("编号:");
  114. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  115. idtxt = new JTextField();
  116. idtxt.setEditable(false);
  117. idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  118. idtxt.setColumns(10);
  119. JLabel lblNewLabel_2 = new JLabel("图书类别名称:");
  120. lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  121. bookTypeNametxt = new JTextField();
  122. bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  123. bookTypeNametxt.setColumns(10);
  124. JLabel lblNewLabel_3 = new JLabel("描述:");
  125. lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  126. bookTypeDesctxt = new JTextArea();
  127. JButton btnNewButton_1 = new JButton("修改");
  128. btnNewButton_1.addActionListener(new ActionListener() {
  129. public void actionPerformed(ActionEvent e) {
  130. bookTypeUpdataActionEvent(e);
  131. }
  132. });
  133. btnNewButton_1.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/修改.png")));
  134. btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  135. JButton btnNewButton_2 = new JButton("删除");
  136. btnNewButton_2.addActionListener(new ActionListener() {
  137. public void actionPerformed(ActionEvent e) {
  138. bookTypeDeleteActionEvent(e);
  139. }
  140. });
  141. btnNewButton_2.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/删除.png")));
  142. btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  143. GroupLayout gl_panel = new GroupLayout(panel);
  144. gl_panel.setHorizontalGroup(
  145. gl_panel.createParallelGroup(Alignment.LEADING)
  146. .addGroup(gl_panel.createSequentialGroup()
  147. .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  148. .addGroup(gl_panel.createSequentialGroup()
  149. .addComponent(lblNewLabel_1)
  150. .addGap(18)
  151. .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  152. .addGap(44)
  153. .addComponent(lblNewLabel_2)
  154. .addPreferredGap(ComponentPlacement.UNRELATED)
  155. .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  156. .addGroup(gl_panel.createSequentialGroup()
  157. .addComponent(lblNewLabel_3)
  158. .addGap(18)
  159. .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  160. .addGroup(gl_panel.createSequentialGroup()
  161. .addComponent(btnNewButton_1)
  162. .addGap(105)
  163. .addComponent(btnNewButton_2))
  164. .addComponent(bookTypeDesctxt, GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))))
  165. .addContainerGap(81, Short.MAX_VALUE))
  166. );
  167. gl_panel.setVerticalGroup(
  168. gl_panel.createParallelGroup(Alignment.LEADING)
  169. .addGroup(gl_panel.createSequentialGroup()
  170. .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  171. .addComponent(lblNewLabel_1)
  172. .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  173. .addComponent(lblNewLabel_2)
  174. .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  175. .addGap(50)
  176. .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  177. .addComponent(lblNewLabel_3)
  178. .addComponent(bookTypeDesctxt, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
  179. .addGap(29)
  180. .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  181. .addComponent(btnNewButton_1)
  182. .addComponent(btnNewButton_2))
  183. .addContainerGap(18, Short.MAX_VALUE))
  184. );
  185. panel.setLayout(gl_panel);
  186. bookTypeTable = new JTable();
  187. bookTypeTable.addMouseListener(new MouseAdapter() {
  188. @Override
  189. public void mousePressed(MouseEvent e) {
  190. bookTypeMousePressed(e);
  191. }
  192. });
  193. bookTypeTable.setShowGrid(false);
  194. bookTypeTable.setModel(new DefaultTableModel(
  195. new Object[][] {
  196. },
  197. new String[] {
  198. "\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u5185\u5BB9"
  199. }
  200. ) {
  201. boolean[] columnEditables = new boolean[] {
  202. false, false, false
  203. };
  204. public boolean isCellEditable(int row, int column) {
  205. return columnEditables[column];
  206. }
  207. });
  208. bookTypeTable.getColumnModel().getColumn(0).setPreferredWidth(49);
  209. bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(103);
  210. bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(142);
  211. scrollPane.setViewportView(bookTypeTable);
  212. getContentPane().setLayout(groupLayout);
  213. //初始化表格
  214. this.fillTable(new BookType());
  215. bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  216. bookTypeDesctxt.setLineWrap(true); //激活自动换行功能
  217. bookTypeDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
  218. }
  219. /**
  220. * 删除事件处理
  221. * @param e
  222. */
  223. protected void bookTypeDeleteActionEvent(ActionEvent e) {
  224. // TODO 自动生成的方法存根
  225. String idString=idtxt.getText();
  226. if(Stringutil.isEmpty(idString)) {
  227. JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
  228. return;
  229. }
  230. int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
  231. if(n==0) {
  232. Connection con=null;
  233. try {
  234. con=dbUtil.getCon();
  235. boolean fl=bookDao.existBook(con, idString);
  236. if(fl==true) {
  237. JOptionPane.showConfirmDialog(null, "当前图书类型下有图书无法删除");
  238. return ;
  239. }
  240. int deteleNum=bookTypeDao.delete(con, idString);
  241. if(deteleNum==1) {
  242. JOptionPane.showConfirmDialog(null, "删除成功");
  243. this.fillTable(new BookType());
  244. this.resetValue();
  245. }
  246. else {
  247. JOptionPane.showConfirmDialog(null, "删除失败");
  248. }
  249. } catch (Exception e2) {
  250. // TODO: handle exception
  251. JOptionPane.showConfirmDialog(null, "删除失败");
  252. e2.printStackTrace();
  253. }finally {
  254. try {
  255. dbUtil.closeCon(con);
  256. } catch (Exception e1) {
  257. // TODO 自动生成的 catch 块
  258. e1.printStackTrace();
  259. }
  260. }
  261. }
  262. }
  263. /**
  264. * 图书类别修改
  265. * @param e
  266. */
  267. protected void bookTypeUpdataActionEvent(ActionEvent e) {
  268. // TODO 自动生成的方法存根
  269. String id=idtxt.getText();
  270. String bookTypeName=bookTypeNametxt.getText();
  271. String bookTypeDesc=bookTypeDesctxt.getText();
  272. if(Stringutil.isEmpty(id)) {
  273. JOptionPane.showConfirmDialog(null, "请选择要修改的记录");
  274. return;
  275. }
  276. else if(Stringutil.isEmpty(bookTypeName)) {
  277. JOptionPane.showConfirmDialog(null, "类别不能为空");
  278. return;
  279. }
  280. else {
  281. BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
  282. Connection con=null;
  283. try {
  284. con=dbUtil.getCon();
  285. int modifNum=bookTypeDao.updata(con, bookType);
  286. if(modifNum==1) {
  287. JOptionPane.showConfirmDialog(null, "修改成功");
  288. this.resetValue();
  289. //这里是为了直接刷新结果
  290. this.fillTable(new BookType());
  291. }
  292. else {
  293. JOptionPane.showConfirmDialog(null, "修改失败");
  294. }
  295. } catch (Exception e2) {
  296. // TODO: handle exception
  297. e2.printStackTrace();
  298. }finally {
  299. try {
  300. dbUtil.closeCon(con);
  301. } catch (Exception e3) {
  302. // TODO: handle exception
  303. e3.printStackTrace();
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * 表格行点击事件处理
  310. * @param e
  311. */
  312. protected void bookTypeMousePressed(MouseEvent e) {
  313. // TODO 自动生成的方法存根
  314. int row=bookTypeTable.getSelectedRow();
  315. idtxt.setText((String)bookTypeTable.getValueAt(row, 0));
  316. bookTypeNametxt.setText((String)bookTypeTable.getValueAt(row, 1));
  317. bookTypeDesctxt.setText((String)bookTypeTable.getValueAt(row, 2));
  318. }
  319. //图书类别查询事件
  320. protected void bookTypeSearchActionPerformed(ActionEvent evt) {
  321. // TODO 自动生成的方法存根
  322. String s_bookTypeName=this.s_bookTypeNametxt.getText();
  323. BookType bookType=new BookType();
  324. bookType.setBookTypeName(s_bookTypeName);
  325. this.fillTable(bookType);
  326. }
  327. private void fillTable(BookType bookType) {
  328. DefaultTableModel dtm=(DefaultTableModel)bookTypeTable.getModel();
  329. dtm.setRowCount(0);//设置成0
  330. Connection con=null;
  331. try {
  332. con = dbUtil.getCon();
  333. } catch (Exception e3) {
  334. // TODO 自动生成的 catch 块
  335. e3.printStackTrace();
  336. }
  337. try {
  338. ResultSet rs=bookTypeDao.list(con, bookType);
  339. while(rs.next()) {
  340. Vector v=new Vector();
  341. v.add(rs.getString("id"));
  342. v.add(rs.getString("bookTypeName"));
  343. v.add(rs.getString("bookTpeDesc"));
  344. dtm.addRow(v);
  345. }
  346. } catch (Exception e) {
  347. // TODO: handle exception
  348. e.printStackTrace();
  349. }finally {
  350. try {
  351. dbUtil.closeCon(con);
  352. } catch (Exception e) {
  353. // TODO 自动生成的 catch 块
  354. e.printStackTrace();
  355. }
  356. }
  357. }
  358. /**
  359. * 重置表单
  360. */
  361. private void resetValue() {
  362. this.idtxt.setText("");
  363. this.bookTypeDesctxt.setText("");
  364. this.bookTypeNametxt.setText("");
  365. }
  366. }

13.Java666interframe.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import javax.swing.GroupLayout;
  7. import javax.swing.GroupLayout.Alignment;
  8. import javax.swing.JLabel;
  9. import javax.swing.ImageIcon;
  10. public class Java666interframe extends JInternalFrame {
  11. private static final long serialVersionUID = 1L;
  12. /**
  13. * Launch the application.
  14. */
  15. public static void main(String[] args) {
  16. EventQueue.invokeLater(new Runnable() {
  17. public void run() {
  18. try {
  19. Java666interframe frame = new Java666interframe();
  20. frame.setVisible(true);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. });
  26. }
  27. /**
  28. * Create the frame.
  29. */
  30. public Java666interframe() {
  31. getContentPane().setBackground(new Color(255, 255, 255));
  32. JLabel lblNewLabel = new JLabel("");
  33. lblNewLabel.setIcon(new ImageIcon(Java666interframe.class.getResource("/images/原神 启动!!!!.png")));
  34. GroupLayout groupLayout = new GroupLayout(getContentPane());
  35. groupLayout.setHorizontalGroup(
  36. groupLayout.createParallelGroup(Alignment.LEADING)
  37. .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
  38. .addContainerGap(95, Short.MAX_VALUE)
  39. .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE)
  40. .addGap(74))
  41. );
  42. groupLayout.setVerticalGroup(
  43. groupLayout.createParallelGroup(Alignment.LEADING)
  44. .addGroup(groupLayout.createSequentialGroup()
  45. .addGap(75)
  46. .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
  47. .addContainerGap(80, Short.MAX_VALUE))
  48. );
  49. getContentPane().setLayout(groupLayout);
  50. setIconifiable(true);
  51. setClosable(true);
  52. setTitle("关于我们");
  53. setBounds(100, 100, 450, 300);
  54. }
  55. }

14.Login.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import com.java1234.dao.UserDao;
  7. import com.java1234.model.User;
  8. import com.java1234.util.DbUtil;
  9. import com.java1234.util.Stringutil;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.GroupLayout;
  13. import javax.swing.GroupLayout.Alignment;
  14. import javax.swing.ImageIcon;
  15. import java.awt.Font;
  16. import javax.swing.LayoutStyle.ComponentPlacement;
  17. import javax.swing.UIManager;
  18. import javax.swing.UnsupportedLookAndFeelException;
  19. import javax.swing.JTextField;
  20. import javax.swing.JButton;
  21. import java.awt.event.ActionListener;
  22. import java.sql.Connection;
  23. import java.awt.event.ActionEvent;
  24. import javax.swing.JComboBox;
  25. public class Login extends JFrame {
  26. private static final long serialVersionUID = 1L;
  27. private JPanel contentPane;
  28. private JTextField userNameTxt;
  29. private JTextField passwordtxt;
  30. private DbUtil dbUtil=new DbUtil();
  31. private UserDao userDao=new UserDao();
  32. /**
  33. * Launch the application.
  34. */
  35. public static void main(String[] args) {
  36. String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  37. try {
  38. UIManager.setLookAndFeel(lookAndFeel);
  39. } catch (ClassNotFoundException e1) {
  40. // TODO 自动生成的 catch 块
  41. e1.printStackTrace();
  42. } catch (InstantiationException e1) {
  43. // TODO 自动生成的 catch 块
  44. e1.printStackTrace();
  45. } catch (IllegalAccessException e1) {
  46. // TODO 自动生成的 catch 块
  47. e1.printStackTrace();
  48. } catch (UnsupportedLookAndFeelException e1) {
  49. // TODO 自动生成的 catch 块
  50. e1.printStackTrace();
  51. }
  52. EventQueue.invokeLater(new Runnable() {
  53. public void run() {
  54. try {
  55. Login frame = new Login();
  56. frame.setVisible(true);
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. });
  62. }
  63. /**
  64. * Create the frame.
  65. */
  66. public Login() {
  67. setTitle("管理员登录");
  68. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69. setBounds(100, 100, 601, 355);
  70. contentPane = new JPanel();
  71. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  72. setContentPane(contentPane);
  73. JLabel lblNewLabel = new JLabel("图书管理系统");
  74. lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 19));
  75. lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/images/图书 (1).png")));
  76. JLabel lblNewLabel_1 = new JLabel("用户名:");
  77. lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  78. lblNewLabel_1.setIcon(new ImageIcon(Login.class.getResource("/images/用户名-登录页.png")));
  79. JLabel lblNewLabel_2 = new JLabel("密 码:");
  80. lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  81. lblNewLabel_2.setIcon(new ImageIcon(Login.class.getResource("/images/密码.png")));
  82. userNameTxt = new JTextField();
  83. userNameTxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  84. userNameTxt.setColumns(10);
  85. passwordtxt = new JTextField();
  86. passwordtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  87. passwordtxt.setColumns(10);
  88. JButton btnNewButton = new JButton("登录");
  89. btnNewButton.addActionListener(new ActionListener() {
  90. public void actionPerformed(ActionEvent e) {
  91. loginActionPerformed(e);
  92. }
  93. });
  94. btnNewButton.setFocusable(false);
  95. btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  96. btnNewButton.setIcon(new ImageIcon(Login.class.getResource("/images/登录.png")));
  97. JButton btnNewButton_1 = new JButton("重置");
  98. btnNewButton_1.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent e) {
  100. resetValueActionPerformed(e);
  101. }
  102. });
  103. btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  104. btnNewButton_1.setFocusable(false);
  105. btnNewButton_1.setIcon(new ImageIcon(Login.class.getResource("/images/重置.png")));
  106. GroupLayout gl_contentPane = new GroupLayout(contentPane);
  107. gl_contentPane.setHorizontalGroup(
  108. gl_contentPane.createParallelGroup(Alignment.LEADING)
  109. .addGroup(gl_contentPane.createSequentialGroup()
  110. .addGap(96)
  111. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  112. .addGroup(gl_contentPane.createSequentialGroup()
  113. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  114. .addComponent(lblNewLabel_2)
  115. .addComponent(lblNewLabel_1))
  116. .addGap(38)
  117. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
  118. .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  119. .addComponent(userNameTxt)
  120. .addComponent(passwordtxt)))
  121. .addGroup(gl_contentPane.createSequentialGroup()
  122. .addGap(47)
  123. .addComponent(btnNewButton)
  124. .addGap(135)
  125. .addComponent(btnNewButton_1)))
  126. .addContainerGap(164, Short.MAX_VALUE))
  127. );
  128. gl_contentPane.setVerticalGroup(
  129. gl_contentPane.createParallelGroup(Alignment.LEADING)
  130. .addGroup(gl_contentPane.createSequentialGroup()
  131. .addGap(25)
  132. .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
  133. .addPreferredGap(ComponentPlacement.RELATED)
  134. .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  135. .addComponent(lblNewLabel_1)
  136. .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  137. .addGap(49)
  138. .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  139. .addComponent(lblNewLabel_2)
  140. .addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  141. .addGap(40)
  142. .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  143. .addComponent(btnNewButton)
  144. .addComponent(btnNewButton_1))
  145. .addContainerGap(49, Short.MAX_VALUE))
  146. );
  147. contentPane.setLayout(gl_contentPane);
  148. //窗口居中
  149. this.setLocationRelativeTo(null);
  150. }
  151. /**
  152. * 登录事件处理
  153. * @param e
  154. */
  155. protected void loginActionPerformed(ActionEvent e) {
  156. // TODO 自动生成的方法存根
  157. String userName=this.userNameTxt.getText();
  158. String password=this.passwordtxt.getText();
  159. if(Stringutil.isEmpty(userName)) {
  160. JOptionPane.showConfirmDialog(null, "用户名不能为空");
  161. return ;
  162. }
  163. if(Stringutil.isEmpty(password)) {
  164. JOptionPane.showConfirmDialog(null, "密码不能为空");
  165. return ;
  166. }
  167. User user=new User(userName,password);
  168. Connection con=null;
  169. try {
  170. con=dbUtil.getCon();
  171. User currentUser=userDao.login(con, user);
  172. if(currentUser!=null) {
  173. dispose();
  174. new Mainframe().setVisible(true);
  175. }
  176. else {
  177. JOptionPane.showConfirmDialog(null, "用户名或者密码错误");
  178. }
  179. } catch (Exception e1) {
  180. // TODO 自动生成的 catch 块
  181. e1.printStackTrace();
  182. }finally{
  183. try {
  184. dbUtil.closeCon(con);
  185. } catch (Exception e1) {
  186. // TODO 自动生成的 catch 块
  187. e1.printStackTrace();
  188. }
  189. }
  190. }
  191. /**
  192. * 重置事件
  193. * @param e
  194. */
  195. protected void resetValueActionPerformed(ActionEvent e) {
  196. // TODO 自动生成的方法存根
  197. this.userNameTxt.setText("");
  198. this.passwordtxt.setText("");
  199. }
  200. }

15.Mainframe.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import com.mysql.cj.xdevapi.Table;
  7. import javax.swing.GroupLayout;
  8. import javax.swing.GroupLayout.Alignment;
  9. import javax.swing.JMenuBar;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuItem;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JDesktopPane;
  15. import java.awt.BorderLayout;
  16. import javax.swing.JLayeredPane;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.Color;
  20. public class Mainframe extends JFrame {
  21. private static final long serialVersionUID = 1L;
  22. private JPanel contentPane;
  23. private JDesktopPane table = null;
  24. /**
  25. * Launch the application.
  26. */
  27. public static void main(String[] args) {
  28. EventQueue.invokeLater(new Runnable() {
  29. public void run() {
  30. try {
  31. Mainframe frame = new Mainframe();
  32. frame.setVisible(true);
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. });
  38. }
  39. /**
  40. * Create the frame.
  41. */
  42. public Mainframe() {
  43. setTitle("图书管理系统");
  44. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. setBounds(100, 100, 450, 300);
  46. JMenuBar menuBar = new JMenuBar();
  47. setJMenuBar(menuBar);
  48. JMenu mnNewMenu = new JMenu("基本数据维护");
  49. mnNewMenu.setIcon(new ImageIcon(Mainframe.class.getResource("/images/数据维护-基础数据.png")));
  50. menuBar.add(mnNewMenu);
  51. JMenu mnNewMenu_2 = new JMenu("图书类别管理");
  52. mnNewMenu_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书类别管理.png")));
  53. mnNewMenu.add(mnNewMenu_2);
  54. JMenuItem mntmNewMenuItem_1 = new JMenuItem("图书类别添加");
  55. mntmNewMenuItem_1.addActionListener(new ActionListener() {
  56. public void actionPerformed(ActionEvent e) {
  57. BookTypeAddInterFrm bookTypeAddInterFrm=new BookTypeAddInterFrm();
  58. bookTypeAddInterFrm.setVisible(true);
  59. table.add(bookTypeAddInterFrm);
  60. }
  61. });
  62. mntmNewMenuItem_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
  63. mnNewMenu_2.add(mntmNewMenuItem_1);
  64. JMenuItem mntmNewMenuItem_5 = new JMenuItem("图书类别维护");
  65. mntmNewMenuItem_5.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. BookTypeManagerInterFrm bookTypeManageInterFrm=new BookTypeManagerInterFrm();
  68. bookTypeManageInterFrm.setVisible(true);
  69. table.add(bookTypeManageInterFrm);
  70. }
  71. });
  72. mntmNewMenuItem_5.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
  73. mnNewMenu_2.add(mntmNewMenuItem_5);
  74. JMenu mnNewMenu_3 = new JMenu("图书管理");
  75. mnNewMenu_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书管理.png")));
  76. mnNewMenu.add(mnNewMenu_3);
  77. JMenuItem mntmNewMenuItem_2 = new JMenuItem("添加图书");
  78. mntmNewMenuItem_2.addActionListener(new ActionListener() {
  79. public void actionPerformed(ActionEvent e) {
  80. BookAddInterFrm bookAddInterFrm=new BookAddInterFrm();
  81. bookAddInterFrm.setVisible(true);
  82. table.add(bookAddInterFrm);
  83. }
  84. });
  85. mntmNewMenuItem_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
  86. mnNewMenu_3.add(mntmNewMenuItem_2);
  87. JMenuItem mntmNewMenuItem_3 = new JMenuItem("图书维护");
  88. mntmNewMenuItem_3.addActionListener(new ActionListener() {
  89. public void actionPerformed(ActionEvent e) {
  90. BookManageInterFrm bookAddInterFrm=new BookManageInterFrm();
  91. bookAddInterFrm.setVisible(true);
  92. table.add(bookAddInterFrm);
  93. }
  94. });
  95. mntmNewMenuItem_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
  96. mnNewMenu_3.add(mntmNewMenuItem_3);
  97. JMenuItem mntmNewMenuItem_4 = new JMenuItem("退出");
  98. mntmNewMenuItem_4.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent e) {
  100. int result=JOptionPane.showConfirmDialog(null,"是否退出");
  101. if(result==0) {
  102. dispose();
  103. }
  104. }
  105. });
  106. mntmNewMenuItem_4.setIcon(new ImageIcon(Mainframe.class.getResource("/images/退出.png")));
  107. mnNewMenu.add(mntmNewMenuItem_4);
  108. JMenu mnNewMenu_1 = new JMenu("关于我们");
  109. mnNewMenu_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
  110. menuBar.add(mnNewMenu_1);
  111. JMenuItem mntmNewMenuItem = new JMenuItem("关于Java");
  112. mntmNewMenuItem.addActionListener(new ActionListener() {
  113. public void actionPerformed(ActionEvent e) {
  114. Java666interframe java666interframe=new Java666interframe();
  115. java666interframe.setVisible(true);
  116. table.add(java666interframe);
  117. }
  118. });
  119. mntmNewMenuItem.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
  120. mnNewMenu_1.add(mntmNewMenuItem);
  121. contentPane = new JPanel();
  122. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  123. setContentPane(contentPane);
  124. contentPane.setLayout(new BorderLayout(0, 0));
  125. table = new JDesktopPane();
  126. table.setBackground(new Color(255, 255, 255));
  127. contentPane.add(table, BorderLayout.CENTER);
  128. //设置最大化
  129. this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  130. }
  131. }

好的代码到这里就结束了,结尾我会给出整个包的连接,里面有图片等等。

这边启动是在Login界面开始启动的喔。。。。

3.结果展示:

 

 没有做过多的可视化啊,有兴趣的小伙伴可以自己修改(期末考试太多了要复习没空做了

4.结尾体会心得:

这边想看我啰嗦的小伙伴可以看看,不想看的就跳。

1.这个实战项目实现了一个小小的前后端分离的操作,虽然很基础也很简单,但是主要是让我学会了怎么进行这种类似项目的开发,以及为日后的毕设做准备,如果说大家想看详细的教程,我是仿照b站上一个视频写的,这边主要是放源码并进行了一些修改。

java +swing(gui) +mysql 实现的图书管理系统_哔哩哔哩_bilibili

up主讲的有些快,希望大家尽力跟上就好了。

2.这边实际上我是不建议大家直接cv的,当然要是时间紧迫嘛也没什么。大家做这个课程设计虽然很大的目的是为了合格,但是我还是希望大家能够手动写一遍,以提升技术为目的,每次做完一个小项目都会让你受益良多。

3.这个开发的过程中呢,就算你是跟着视频写的,也很可能会写漏或者是写错什么变量。我这个代码里面就写错了一个变量但是不影响使用,有哪位细心的小伙伴可以指出来并在评论区留言。主要是因为我的这个eclipse自动填充变量名导致的,因此大家在开发项目之前对自己的编译器进行一定程度上的调节也是十分重要的,一切以自己用的舒服为先。

有什么问题也可以私聊或者直接@我都可以我会尽力帮大家解决。

5.网盘地址分享:

代码文件包:

链接: https://pan.baidu.com/s/1lulyGlbA50O5e82Kfdhkzw?pwd=ersp 提取码: ersp 复制这段内容后打开百度网盘手机App,操作更方便哦

驱动文件包(代码里面应该有了,保险起见还是再发一份):

链接: https://pan.baidu.com/s/1KQMqJrSeLRDXryO72agOWw?pwd=dhte 提取码: dhte 复制这段内容后打开百度网盘手机App,操作更方便哦

最后祝大家都能够度过一个完美的暑假!!!!

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号