由于代码量庞大,如果借鉴的话(你懂的),就直接(ctrl+c/v)
“”
如果奔着思路来的,建议直接去看两个核心的Util包
这是配置文件的jdbc.Properties里面的内容,可以根据自己的情况修改
user=root
password=3333
url=jdbc:mysql://localhost:3306/my_jdbc
driverclass=com.mysql.jdbc.Driver
三个类
Book类用来承载查询的图书信息,然后通过get,set函数进行数据的操作
BookType类用来承载查询的图书类别信息,然后通过get,set方法操作
User类用来承载查询的用户信息,然后通过get,set方法操作
Util两个自定义工具包
JDBC_Util,封装了关于数据库操作的一系列操作,包括所有表通用的增删改
所有表通用的查,以及一些特殊信息的查询(如:用户密码账户信息等)
Juage_Util,封装可了关于改管理系统的所有判断操作
七个view(可视化界面)
Book_Manager 主要实现前两张图的功能
Book_Type_Manager 主要实现第3,4两张图的功能
Book_View 主要实现第5张图的功能
bookType_view 主要实现第6张图的功能
Java 实现关于我们的功能
Login_view 实现登录功能
MainInterface 实现主菜单功能(登入进去后看到的界面)
package User_Book;
public class Book {
private int id;
private String bookName;
private String author;
private String sex;
private float price;
private String bookTypeName;
private String bookDesc;
public Book() {
}
public Book(int id, String bookName, String author, String sex, float price, String bookTypeName, String bookDesc) {
super();
this.id = id;
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeName = bookTypeName;
this.bookDesc = bookDesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
public String getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
@Override
public String toString() {
return bookDesc;
}
}
package User_Book;
/**
* 用来储存在数据库中读取的书籍类别信息
* @author 12177
* */
public class BookType {
private int id;
private String bookTypeName;
private String bookTypeDesc;
public BookType(String bookTypeName,String bookTypeDesc) {
this.bookTypeName=bookTypeName;
this.bookTypeDesc=bookTypeDesc;
}
public BookType(int id,String bookTypeName,String bookTypeDesc) {
this.id=id;
this.bookTypeName=bookTypeName;
this.bookTypeDesc=bookTypeDesc;
}
public BookType() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
public String getBookTypeDesc() {
return bookTypeDesc;
}
public void setBookTypeDesc(String bookTypeDesc) {
this.bookTypeDesc = bookTypeDesc;
}
@Override
public String toString() {
return "BookType [id=" + id + ", bookTypeName=" + bookTypeName + ", bookTypeDesc=" + bookTypeDesc + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookType other = (BookType) obj;
if (bookTypeDesc == null) {
if (other.bookTypeDesc != null)
return false;
} else if (!bookTypeDesc.equals(other.bookTypeDesc))
return false;
if (bookTypeName == null) {
if (other.bookTypeName != null)
return false;
} else if (!bookTypeName.equals(other.bookTypeName))
return false;
if (id != other.id)
return false;
return true;
}
}
package User_Book;
/**
* 用户类,用来储存在数据库中读取的用户信息;
* @author 1217743065
* */
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(String username,String password) {
this.username=username;
this.password=password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
package Util;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import User_Book.Book;
import User_Book.User;
public class JDBC_Util {
static Connection conn = null;
static ArrayList arr = new ArrayList();
/**
* GetConnection();返回Connection类型的数据库连接对象;
*
* CloseConnection();关闭数据库连接
*
* Selection_username_password();在数据中查询用户名以及密码并返回ArrayList<User>结果集.
*
* ALL_Table_Selection();除用户信息之外所有表的通用查询操作并返回ArrayList<T>结果集.
*
* ALL_Table_CUD();所有表的增删改操作,包括用户信息.
*
* Like_Selection_Book();对书籍进行模糊查询;返回ArrayList<Book>集合
* */
public static Connection GetConnection(){
try {
Properties properties = new Properties();
InputStream is = JDBC_Util.class.getClassLoader().getResourceAsStream("JDBC.Properties");
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverclass = properties.getProperty("driverclass");
Class.forName(driverclass);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
System.out.println("数据库连接失败!-->来自自定义工具类JDBC_Util--30行");
}
return null;
}
public static void CloseConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常!-->来自自定义工具类JDBC_Util--39行");
}
}
public static ArrayList Selection_username_password(Connection conn) {
try {
String sql = "select username,password from user where id !=0 ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet resultset = ps.executeQuery();
ResultSetMetaData rsmd = ps.getMetaData();
int ColumnCount = rsmd.getColumnCount();
while (resultset.next()) {
User user2 = new User();
for (int i = 0; i < ColumnCount; ++i) {
String ColumnValue = resultset.getString(i + 1);
String ColumnLabel = rsmd.getColumnLabel(i + 1);
Field field = User.class.getDeclaredField(ColumnLabel);
field.setAccessible(true);
field.set(user2, ColumnValue);
}
arr.add(user2);
}
} catch (Exception e) {
System.out.println("数据库用户查询异常!-->来自自定义工具类JDBC_Util--70行");
}
return arr;
}
public static <T> ArrayList<T> ALL_Table_Selection(Class<T> clazz ,String sql,Object...args) {
PreparedStatement ps = null;
ResultSet resultset = null;
ArrayList arr = new ArrayList();
try {
conn = JDBC_Util.GetConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; ++i) {
ps.setObject(i + 1, args[i]);
}
resultset = ps.executeQuery();
ResultSetMetaData rsmd = resultset.getMetaData();
int ColumnCount = rsmd.getColumnCount();
while (resultset.next()) {
T t = clazz.newInstance();
for (int i = 0; i < ColumnCount; ++i) {
String ColumnName = rsmd.getColumnLabel(i + 1);
Object ColumnValue = resultset.getObject(i + 1);
Field field = clazz.getDeclaredField(ColumnName);
field.setAccessible(true);
field.set(t, ColumnValue);
}
arr.add(t);
}
return arr;
} catch (Exception e) {
System.out.println("数据库通用查询异常!-->来自自定义工具类JDBC_Util--109行");
}finally {
JDBC_Util.CloseConnection(conn);
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
System.out.println("数据库通用查询异常!-->来自自定义工具类JDBC_Util--116行");
}
}
if(resultset!=null) {
try {
resultset.close();
} catch (SQLException e) {
System.out.println("数据库通用查询异常!-->来自自定义工具类JDBC_Util--124行");
}
}
}
return null;
}
public static void ALL_Table_CUD(String sql,Object...args) {
PreparedStatement ps = null;
try {
conn = JDBC_Util.GetConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; ++i) {
ps.setObject(i + 1, args[i]);
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库通用CUD异常!-->来自自定义工具类JDBC_Util--145行");
}finally {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
System.out.println("数据库通用CUD异常!-->来自自定义工具类JDBC_Util--151行");
}
}
JDBC_Util.CloseConnection(conn);
}
}
public static ArrayList Like_Selection_Book(String bookName,String bookAuthor,String bookType) {
ArrayList arr = new ArrayList();
PreparedStatement ps = null;
ResultSet resultset = null;
StringBuffer sql = new StringBuffer("select * from book where id!=0 ");
if(Juage_Util.isEmpty(bookName)&&Juage_Util.isEmpty(bookAuthor)&&
Juage_Util.isEmpty(bookType)) {
return null;
}
if(!Juage_Util.isEmpty(bookName)) {
sql.append("and bookName like '%"+bookName+"%' ");
}
if(!Juage_Util.isEmpty(bookAuthor)) {
sql.append("and author like '%"+bookAuthor+"%' ");
}
if(!Juage_Util.isEmpty(bookType)) {
sql.append("and bookTypeName= '"+bookType+"' ");
}
Connection conn = JDBC_Util.GetConnection();
try {
ps = conn.prepareStatement(sql.toString());
resultset = ps.executeQuery();
ResultSetMetaData rsmd = resultset.getMetaData();
int ColumnCount = rsmd.getColumnCount();
while (resultset.next()) {
Book book = new Book();
for (int i = 0; i < ColumnCount; ++i) {
String ColumnName = rsmd.getColumnLabel(i + 1);
Object ColumnValue = resultset.getObject(i + 1);
Field field = Book.class.getDeclaredField(ColumnName);
field.setAccessible(true);
field.set(book, ColumnValue);
}
arr.add(book);
}
return arr;
} catch (Exception e) {
System.out.println("查询图书信息异常!-->来自自定义工具类JDBC_Util--198行");
}finally {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(resultset!=null) {
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JDBC_Util.CloseConnection(conn);
}
return null;
}
}
package Util;
import java.sql.Array;
import java.sql.Connection;
import java.util.ArrayList;
import User_Book.BookType;
import User_Book.User;
public class Juage_Util {
static Connection conn = null;
/**
* isEmpty();参数为传入的字符串判断是否为空.
*
* isNotEmpty();参数为传入的字符串判断是否不是空.
*
* Yes_No_Login();参数1:获取的连接后数据库对象;参数2:user对象,其中包含了用户输入的用户名username密码password
*
* Check_Duplicate_User();检查添加的用户名是否重复;参数1:传入将要添加的用户名;
*
* Check_Effective_Value();检查在文本框输入的值是否有效;参数1:传入的float类型的值;
* */
public static boolean isEmpty(String str) {
if(str==null||"".equals(str.trim())) {
return true;
}
else {
return false;
}
}
//trim()函数忽略字符串的头尾空白字符
public static boolean IsNotEmpty(String str) {
if(str==null||"".equals(str.trim())) {
return false;
}
else {
return true;
}
}
public static boolean Yes_No_Login(Connection conn,User user) {
ArrayList arr = JDBC_Util.Selection_username_password(conn);
for(Object obj: arr) {
User user3 = (User)obj;
if(user3.equals(user)) {
return true;
}
}
return false;
}
public static boolean Check_Duplicate_User(String username) {
conn = JDBC_Util.GetConnection();
ArrayList arr = JDBC_Util.Selection_username_password(conn);
for(Object obj : arr) {
User user =(User)obj;
if(username.equals(user.getUsername())) {
return false;
}
}
JDBC_Util.CloseConnection(conn);
return true;
}
public static boolean Check_Duplicate_BookType(String booktype) {
String sql = "select bookTypeName from booktype where id !=?";
ArrayList<BookType> arr = JDBC_Util.ALL_Table_Selection(BookType.class, sql, 0);
for(Object obj : arr) {
BookType booktype2 =(BookType)obj;
if(booktype.equals(booktype2.getBookTypeName())) {
return false;
}
}
return true;
}
public static boolean Check_Effective_Value(String str) {
if(str.length()>8) {
return false;
}
for(int i=0;i<str.length();++i) {
if(!("0".equals(str.charAt(i)+"") || "1".equals(str.charAt(i)+"") ||
"2".equals(str.charAt(i)+"") || "3".equals(str.charAt(i)+"") ||
"4".equals(str.charAt(i)+"") || "5".equals(str.charAt(i)+"") ||
"6".equals(str.charAt(i)+"") || "7".equals(str.charAt(i)+"") ||
"8".equals(str.charAt(i)+"") || "9".equals(str.charAt(i)+"") || ".".equals(str.charAt(i) +"" )))
return false;
}
return true;
}
}
package view;
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import User_Book.Book;
import User_Book.BookType;
import Util.JDBC_Util;
import Util.Juage_Util;
import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Book_Manager extends JInternalFrame {
private JTable table;
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private JComboBox bookTypeTxt;
private JScrollPane bookTable2;
private JTextField idTxt;
private JTextField bookNametxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField priceTxt;
private JTextField authorTxt;
private JRadioButton manJrb;
private JRadioButton womenJrb;
private JTextArea bookDescTxt;
private JComboBox bookTypeJcb;
private JLabel label_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_Manager frame = new Book_Manager();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_Manager() {
getContentPane().setFont(new Font("宋体", Font.BOLD, 20));
setTitle("图书的维护");
setIconifiable(true);
setClosable(true);
setBounds(100, 100, 973, 729);
bookTable2 = new JScrollPane();
JPanel panel = new JPanel();
panel.setBackground(SystemColor.info);
panel.setForeground(SystemColor.textHighlight);
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLUE));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(31)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addComponent(panel_1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(bookTable2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 885, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(26)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
.addGap(35)
.addComponent(bookTable2, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
.addGap(26)
.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
.addContainerGap())
);
JLabel label_2 = new JLabel("编号:");
label_2.setFont(new Font("宋体", Font.BOLD, 20));
idTxt = new JTextField();
idTxt.setFont(new Font("宋体", Font.PLAIN, 20));
idTxt.setForeground(SystemColor.activeCaptionText);
idTxt.setEditable(false);
idTxt.setColumns(10);
JLabel label_3 = new JLabel("图书名称:");
label_3.setFont(new Font("宋体", Font.BOLD, 19));
bookNametxt = new JTextField();
bookNametxt.setFont(new Font("宋体", Font.PLAIN, 20));
bookNametxt.setColumns(10);
JLabel label_4 = new JLabel("作者性别:");
label_4.setFont(new Font("宋体", Font.BOLD, 20));
manJrb = new JRadioButton("男");
manJrb.setFont(new Font("宋体", Font.BOLD, 20));
buttonGroup.add(manJrb);
manJrb.setSelected(true);
womenJrb = new JRadioButton("女");
womenJrb.setFont(new Font("宋体", Font.BOLD, 20));
buttonGroup.add(womenJrb);
JLabel label_5 = new JLabel("价格:");
label_5.setFont(new Font("宋体", Font.BOLD, 20));
priceTxt = new JTextField();
priceTxt.setFont(new Font("宋体", Font.PLAIN, 20));
priceTxt.setColumns(10);
JLabel label_6 = new JLabel("图书作者:");
label_6.setFont(new Font("宋体", Font.BOLD, 20));
authorTxt = new JTextField();
authorTxt.setFont(new Font("宋体", Font.PLAIN, 20));
authorTxt.setColumns(10);
JLabel label_7 = new JLabel("图书类别:");
label_7.setFont(new Font("宋体", Font.BOLD, 20));
bookTypeJcb = new JComboBox();
bookTypeJcb.setFont(new Font("宋体", Font.PLAIN, 20));
JLabel label_8 = new JLabel("图书描述:");
label_8.setFont(new Font("宋体", Font.BOLD, 20));
bookDescTxt = new JTextArea();
bookDescTxt.setFont(new Font("Monospaced", Font.PLAIN, 20));
bookDescTxt.setBackground(SystemColor.info);
bookDescTxt.setForeground(SystemColor.menuText);
JButton btnNewButton = new JButton("修改");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookUpdateActionPerformed(e);
}
});
btnNewButton.setIcon(new ImageIcon(Book_Manager.class.getResource("/图片/修改.png")));
btnNewButton.setFont(new Font("宋体", Font.BOLD, 20));
JButton btnNewButton_1 = new JButton("删除");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookDeleteActionPerformed(e);
}
});
btnNewButton_1.setIcon(new ImageIcon(Book_Manager.class.getResource("/图片/删除.png")));
btnNewButton_1.setFont(new Font("宋体", Font.BOLD, 20));
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(77)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_5)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(priceTxt)))
.addGap(29)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addComponent(label_3)
.addComponent(label_6))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
.addComponent(bookNametxt)
.addComponent(authorTxt, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addComponent(label_7)
.addComponent(label_4))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(manJrb, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18)
.addComponent(womenJrb))
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(label_8)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(btnNewButton)
.addGap(76)
.addComponent(btnNewButton_1))
.addComponent(bookDescTxt, GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE))))
.addGap(24))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(29)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(manJrb)
.addComponent(label_4)
.addComponent(label_3)
.addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(womenJrb))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_7)
.addComponent(label_6)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_8)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(btnNewButton_1))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
JLabel lblNewLabel = new JLabel("图书名称:");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 20));
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
JLabel label = new JLabel("图书作者:");
label.setFont(new Font("宋体", Font.BOLD, 20));
bookAuthorTxt = new JTextField();
bookAuthorTxt.setColumns(10);
label_1 = new JLabel("图书类别:");
label_1.setFont(new Font("宋体", Font.BOLD, 20));
bookTypeTxt = new JComboBox();
bookTypeTxt.setFont(new Font("宋体", Font.PLAIN, 20));
JButton search = new JButton("");
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookInformationAdd(e);
}
});
search.setIcon(new ImageIcon(Book_Manager.class.getResource("/图片/u=1312342897,1874976194&fm=26&gp=0.png")));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(46)
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookNameTxt, GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
.addGap(18)
.addComponent(label)
.addGap(18)
.addComponent(bookAuthorTxt, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
.addGap(42)
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookTypeTxt, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(search)
.addGap(53))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(search, 0, 0, Short.MAX_VALUE)
.addContainerGap())
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(bookTypeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1)
.addComponent(label)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel)
.addComponent(bookAuthorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(10))))
);
panel.setLayout(gl_panel);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
bookTableMousePressed(e);
}
});
table.setFont(new Font("宋体", Font.PLAIN, 16));
table.setForeground(Color.WHITE);
table.setBackground(Color.WHITE);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\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\u540D\u79F0"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.getColumnModel().getColumn(0).setPreferredWidth(52);
table.getColumnModel().getColumn(2).setPreferredWidth(78);
table.getColumnModel().getColumn(5).setPreferredWidth(159);
table.getColumnModel().getColumn(6).setPreferredWidth(102);
bookTable2.setViewportView(table);
getContentPane().setLayout(groupLayout);
//设置文本域边框
bookTypeTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
fillbookType2();
fillbookType();
fillBookInformation();
//bookInformationAdd(new );
}
/**
* 图书删除事件处理
* */
private void bookDeleteActionPerformed(ActionEvent e) {
String id = this.idTxt.getText();
if(Juage_Util.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择删除的图书!");
return;
}
int Id = Integer.parseInt(id);
int selection = JOptionPane.showConfirmDialog(null, "是否删除该图书信息?");
if(selection==0) {
String sql ="delete from book where id = ? ";
JDBC_Util.ALL_Table_CUD(sql, Id);
this.idTxt.setText("");
this.bookNametxt.setText("");
this.authorTxt.setText("");
this.priceTxt.setText("");
this.bookDescTxt.setText("");
fillBookInformation();//刷新表格内容
JOptionPane.showMessageDialog(null, "删除成功!");
}
}
/**
* 图书修改事件处理
* */
private void bookUpdateActionPerformed(ActionEvent e) {
String id = this.idTxt.getText();
if(Juage_Util.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择修改的图书!");
return;
}
String bookName = this.bookNametxt.getText();
String author = this.authorTxt.getText();
String price = this.priceTxt.getText();
String bookDesc = this.bookDescTxt.getText();
if(Juage_Util.isEmpty(bookName)) {
JOptionPane.showMessageDialog(null, "图书名称不能为空!");
return;
}
if(Juage_Util.isEmpty(author)) {
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if(Juage_Util.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex = "";
if(manJrb.isSelected()) {
sex="男";
}else if(womenJrb.isSelected()) {
sex="女";
}
String bookType2 = (String)bookTypeJcb.getSelectedItem();//获取下拉框
int Id = Integer.parseInt(id);//将获取的id转成int型
float price2 = Float.parseFloat(price);
String sql ="update book set bookName=?,author=?,sex=?,price=?,bookTypeName=?,bookDesc=? where id = ?";
JDBC_Util.ALL_Table_CUD(sql,bookName,author,sex,price2,bookType2,bookDesc,Id);
this.idTxt.setText("");
this.bookNametxt.setText("");
this.authorTxt.setText("");
this.priceTxt.setText("");
this.bookDescTxt.setText("");
fillBookInformation();//刷新表格内容
JOptionPane.showMessageDialog(null, "修改成功!");
}
/**
* 初始化下面的下拉框
* */
private void fillbookType2() {
String sql = "select * from booktype where id !=?";
ArrayList<BookType> arr = JDBC_Util.ALL_Table_Selection(BookType.class, sql, 0);
for(Object obj : arr) {
BookType bt = (BookType)obj;
this.bookTypeJcb.addItem(bt.getBookTypeName());
}
}
/**
* 表格点击事件处理
* */
private void bookTableMousePressed(MouseEvent e) {
int row = this.table.getSelectedRow();
String str = String.valueOf(table.getValueAt(row, 0));
this.idTxt.setText(str);
this.bookNametxt.setText((String)table.getValueAt(row, 1));
this.authorTxt.setText((String)table.getValueAt(row, 2));
String sex = (String)table.getValueAt(row, 3);
if("男".equals(sex)) {
this.manJrb.setSelected(true);
}else if("女".equals(sex)) {
this.womenJrb.setSelected(true);
}
String price = String.valueOf(table.getValueAt(row, 4));
this.priceTxt.setText(price);
this.bookDescTxt.setText((String)table.getValueAt(row, 5));
//下拉框的赋值有些特殊,所以没有setText方法,只能使用别的方式,如下:
String str2 = (String)table.getValueAt(row, 6);
int n = this.bookTypeJcb.getItemCount();
for(int i=0;i<n;++i) {
String booktypename = (String)this.bookTypeJcb.getItemAt(i);
if(booktypename.equals(str2)) {
this.bookTypeJcb.setSelectedIndex(i);
}
}
}
/**
* 填充查寻的信息
* */
private void bookInformationAdd(ActionEvent e) {
String bookName = this.bookNameTxt.getText();
String bookAuthor = this.bookAuthorTxt.getText();
String bookType = (String)this.bookTypeTxt.getSelectedItem();
ArrayList arr = JDBC_Util.Like_Selection_Book(bookName, bookAuthor, bookType);
if(arr == null) {
JOptionPane.showMessageDialog(null, "查询信息不可以都为空!");
return;
}
DefaultTableModel dtm2 = (DefaultTableModel)table.getModel();
dtm2.setRowCount(0);//设置成0行,相当于情况表格;
for(Object obj : arr) {
Vector v = new Vector();
Book book = (Book)obj;
v.add(book.getId());
v.add(book.getBookName());
v.add(book.getAuthor());
v.add(book.getSex());
v.add(book.getPrice());
v.add(book.getBookDesc());
v.add(book.getBookTypeName());
dtm2.addRow(v);
}
}
/**
* 初始化下拉框上面那个初始化下拉框;bookTypeTxt
* */
private void fillbookType() {
this.bookTypeTxt.addItem("请选择...");
String sql = "select * from booktype where id !=?";
ArrayList<BookType> arr = JDBC_Util.ALL_Table_Selection(BookType.class, sql, 0);
for(Object obj : arr) {
BookType bt = (BookType)obj;
this.bookTypeTxt.addItem(bt.getBookTypeName());
}
}
/**
* 初始化图书信息表格内容
* */
private void fillBookInformation() {
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setRowCount(0);//设置成0行,相当于清空表格;
String sql ="select * from book where id != ?";
ArrayList arr = JDBC_Util.ALL_Table_Selection(Book.class, sql, 0);
for(Object obj : arr) {
Vector v = new Vector();
Book b = (Book)obj;
v.add(b.getId());
v.add(b.getBookName());
v.add(b.getAuthor());
v.add(b.getSex());
v.add(b.getPrice());
v.add(b.getBookDesc());
v.add(b.getBookTypeName());
dtm.addRow(v);
}
}
}
package view;
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import User_Book.BookType;
import Util.JDBC_Util;
import Util.Juage_Util;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Book_Type_Manager extends JInternalFrame {
private JTable BookTable;
private JTextField textField;
private JTextField idTxt;
private JTextField bookTypeNameTxt;
private JTextArea bookTyepDescTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_Type_Manager frame = new Book_Type_Manager();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_Type_Manager() {
setBackground(SystemColor.info);
getContentPane().setFont(new Font("宋体", Font.BOLD, 20));
getContentPane().setForeground(SystemColor.activeCaptionText);
setIconifiable(true);
setClosable(true);
setTitle("图书类别管理");
setBounds(100, 100, 791, 638);
JScrollPane scrollPane = new JScrollPane();
JLabel lblNewLabel = new JLabel("图书类别名称:");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18));
textField = new JTextField();
textField.setColumns(10);
JButton button = new JButton("查询");
button.setIcon(new ImageIcon(Book_Type_Manager.class.getResource("/图片/u=1312342897,1874976194&fm=26&gp=0.png")));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookTypeSearchActionPerformed(e);
}
});
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
JButton button_1 = new JButton("修改");
button_1.setIcon(new ImageIcon(Book_Type_Manager.class.getResource("/图片/修改.png")));
button_1.setFont(new Font("宋体", Font.BOLD, 20));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookTypeUpdateActionEvent(e);
}
});
JButton btnNewButton = new JButton("删除");
btnNewButton.setIcon(new ImageIcon(Book_Type_Manager.class.getResource("/图片/删除.png")));
btnNewButton.setFont(new Font("宋体", Font.BOLD, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookTypeDeleteActionEvent(e);
}
});
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap(98, Short.MAX_VALUE)
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button)
.addGap(173))
.addGroup(groupLayout.createSequentialGroup()
.addGap(80)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(scrollPane, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 598, GroupLayout.PREFERRED_SIZE))
.addContainerGap(97, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(182)
.addComponent(button_1)
.addGap(132)
.addComponent(btnNewButton)
.addContainerGap(243, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(58)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel))
.addGap(18)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 231, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_1)
.addComponent(btnNewButton))
.addContainerGap(15, Short.MAX_VALUE))
);
JLabel lblNewLabel_1 = new JLabel("编号:");
lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 20));
idTxt = new JTextField();
idTxt.setFont(new Font("宋体", Font.BOLD, 20));
idTxt.setEditable(false);
idTxt.setColumns(10);
JLabel label = new JLabel("图书类别名称:");
label.setFont(new Font("宋体", Font.BOLD, 20));
bookTypeNameTxt = new JTextField();
bookTypeNameTxt.setFont(new Font("宋体", Font.BOLD, 20));
bookTypeNameTxt.setColumns(10);
JLabel label_1 = new JLabel("描述:");
label_1.setFont(new Font("宋体", Font.BOLD, 20));
bookTyepDescTxt = new JTextArea();
bookTyepDescTxt.setFont(new Font("Monospaced", Font.PLAIN, 20));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(lblNewLabel_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookTyepDescTxt, GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)))
.addGap(13))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label)
.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(bookTyepDescTxt, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE))
.addContainerGap(136, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
BookTable = new JTable();
BookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
//鼠标点击事件,mousePressed事件
bookTypeTableMousePressed(e);
}
});
BookTable.setBackground(SystemColor.info);
BookTable.setFont(new Font("宋体", Font.BOLD, 16));
BookTable.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
BookTable.getColumnModel().getColumn(1).setPreferredWidth(132);
BookTable.getColumnModel().getColumn(2).setPreferredWidth(199);
scrollPane.setViewportView(BookTable);
getContentPane().setLayout(groupLayout);
this.Fill_Book_Type_Table(new BookType());
//设置文本域边框
bookTyepDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
}
/**
*
*
* */
/**
* 图书删除
* */
private void bookTypeDeleteActionEvent(ActionEvent e) {
String Id = idTxt.getText();
if(Juage_Util.isEmpty(Id)) {
JOptionPane.showMessageDialog(null, "请选择要删除的编号!");
return;
}
int selection = JOptionPane.showConfirmDialog(null, "是否删除该图书类型?");
if(selection==0) {
int id = Integer.parseInt(Id);
String sql = "delete from booktype where id = ?";
JDBC_Util.ALL_Table_CUD(sql, id);
resetValue();
JOptionPane.showMessageDialog(null, "删除成功!");
}
}
/**
* 图书类别修改
*
* */
private void bookTypeUpdateActionEvent(ActionEvent e) {
String Id = idTxt.getText();
if(Juage_Util.isEmpty(Id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
return;
}
String bookTypeName = bookTypeNameTxt.getText();
if(Juage_Util.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别不能为空!");
return;
}
int id = Integer.parseInt(idTxt.getText());
String bookTypeDesc = bookTyepDescTxt.getText();
String sql = "update booktype set bookTypeName=?,bookTypeDesc=? where id =?";
JDBC_Util.ALL_Table_CUD(sql, bookTypeName,bookTypeDesc,id);
resetValue();
this.Fill_Book_Type_Table(new BookType());//从新new个实例刷新表格
JOptionPane.showMessageDialog(null, "修改成功!");
}
private void resetValue() {
this.idTxt.setText("");
this.bookTypeNameTxt.setText("");
this.bookTyepDescTxt.setText("");
}
/**
* 鼠标点击
* 处理鼠标点击到的查村框内容;
* */
private void bookTypeTableMousePressed(MouseEvent evt) {
int Row = BookTable.getSelectedRow();//获取选中的行,并返回行号;
String in = String.valueOf(BookTable.getValueAt(Row, 0));
idTxt.setText(in);
bookTypeNameTxt.setText((String)BookTable.getValueAt(Row, 1));
bookTyepDescTxt.setText((String)BookTable.getValueAt(Row, 2));
}
/**
* 查询事件
* */
private void BookTypeSearchActionPerformed(ActionEvent e) {
Vector v = null;
DefaultTableModel dtm = (DefaultTableModel)BookTable.getModel();
dtm.setRowCount(0);//设置成0行,相当于情况表格;
//获取用户输入的图书类别名称;
String GetBookType = this.textField.getText();//获取用户输入在框里面的内容
String sql = "select id,bookTypeName,bookTypeDesc from booktype where id!=?";
ArrayList arr = JDBC_Util.ALL_Table_Selection(BookType.class,sql, 0);
for(int i=0;i<arr.size();++i) {
BookType bt = (BookType)arr.get(i);
flag: for(int j=0;j<bt.getBookTypeName().length();++j) {
for(int t=0;t<GetBookType.length();++t) {
if(bt.getBookTypeName().charAt(j)==GetBookType.charAt(t)) {
v = new Vector();
v.add(bt.getId());
v.add(bt.getBookTypeName());
v.add(bt.getBookTypeDesc());
dtm.addRow(v);
break flag;
}
}
}
}
}
/**
* 初始化表格
* */
private void Fill_Book_Type_Table(BookType booktype) {
DefaultTableModel dtm = (DefaultTableModel)BookTable.getModel();
dtm.setRowCount(0);//设置成0行,相当于清空表格;
//查询数据库中的所有书籍类型的数据,然后通过调用dtm的addRow方法,来填充表格;
String sql = "select id,bookTypeName,bookTypeDesc from booktype where id !=?";
ArrayList<BookType> arr = JDBC_Util.ALL_Table_Selection(BookType.class, sql, 0);
for(int i=0;i<arr.size();++i) {
Vector v = new Vector();
v.add(arr.get(i).getId());
v.add(arr.get(i).getBookTypeName());
v.add(arr.get(i).getBookTypeDesc());
dtm.addRow(v);
}
}
}
package view;
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import User_Book.BookType;
import Util.JDBC_Util;
import Util.Juage_Util;
import java.awt.Font;
import java.awt.SystemColor;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Book_View extends JInternalFrame {
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField bookPriceTxt;
private JComboBox bookType;
private JTextArea bookDescTxt;
private JRadioButton man;
private JRadioButton women;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_View frame = new Book_View();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_View() {
getContentPane().setFont(new Font("宋体", Font.PLAIN, 20));
setIconifiable(true);
setClosable(true);
getContentPane().setBackground(SystemColor.info);
setBounds(100, 100, 610, 640);
JLabel label = new JLabel("图书名称:");
label.setFont(new Font("宋体", Font.BOLD, 20));
bookNameTxt = new JTextField();
bookNameTxt.setFont(new Font("宋体", Font.PLAIN, 16));
bookNameTxt.setColumns(10);
JLabel label_1 = new JLabel("图书作者:");
label_1.setFont(new Font("宋体", Font.BOLD, 20));
bookAuthorTxt = new JTextField();
bookAuthorTxt.setFont(new Font("宋体", Font.PLAIN, 16));
bookAuthorTxt.setColumns(10);
JLabel label_2 = new JLabel("作者性别:");
label_2.setFont(new Font("宋体", Font.BOLD, 20));
man = new JRadioButton("男");
buttonGroup.add(man);
man.setSelected(true);
women = new JRadioButton("女");
buttonGroup.add(women);
JLabel label_3 = new JLabel("图书价格:");
label_3.setFont(new Font("宋体", Font.BOLD, 20));
bookPriceTxt = new JTextField();
bookPriceTxt.setFont(new Font("宋体", Font.PLAIN, 16));
bookPriceTxt.setColumns(10);
JLabel label_4 = new JLabel("图书类别:");
label_4.setFont(new Font("宋体", Font.BOLD, 20));
bookType = new JComboBox();
bookType.setFont(new Font("宋体", Font.PLAIN, 20));
JLabel label_5 = new JLabel("图书简述:");
label_5.setFont(new Font("宋体", Font.BOLD, 20));
bookDescTxt = new JTextArea();
bookDescTxt.setFont(new Font("Monospaced", Font.PLAIN, 16));
JButton button = new JButton("添加");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addbookActionPerformed(e);
}
});
button.setIcon(new ImageIcon(Book_View.class.getResource("/图片/添加.png")));
button.setFont(new Font("宋体", Font.BOLD, 20));
JButton button_1 = new JButton("重置");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValueActionPerformed(e);
}
});
button_1.setIcon(new ImageIcon(Book_View.class.getResource("/图片/重置.png")));
button_1.setFont(new Font("宋体", Font.BOLD, 20));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookType, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(man)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(women)))
.addGap(80)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookAuthorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(bookPriceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_5)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(bookDescTxt, GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button)
.addPreferredGap(ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
.addComponent(button_1)
.addGap(70)))))
.addContainerGap(68, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(91)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(label_1)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(bookAuthorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(41)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(label_3)
.addComponent(bookPriceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(man)
.addComponent(women))
.addGap(42)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_4)
.addComponent(bookType, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
.addGap(50)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE))
.addGap(30)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(button_1))
.addContainerGap(34, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
//设置文本域边框
bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
fillBookType();
}
/**
*
* 添加图书事件处理
* */
private void addbookActionPerformed(ActionEvent e) {
String sql = "insert into book(bookName,author,sex,price,bookTypeName,bookDesc)values(?,?,?,?,?,?)";
String bookName = this.bookNameTxt.getText();
String author = this.bookAuthorTxt.getText();
String price = this.bookPriceTxt.getText();
String bookDesc = this.bookDescTxt.getText();
if(!Juage_Util.Check_Effective_Value(price)) {
JOptionPane.showMessageDialog(null, "图书价格不合法!");
return;
}
if(Juage_Util.isEmpty(bookName)) {
JOptionPane.showMessageDialog(null, "图书名字不能为空!");
return;
}
if(Juage_Util.isEmpty(author)) {
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if(Juage_Util.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String bookType2 = (String)bookType.getSelectedItem();
if(Juage_Util.isEmpty(bookType2)) {
JOptionPane.showMessageDialog(null, "图书类别不能为空!");
return;
}
String sex = "";
if(man.isSelected()) {
sex="男";
}else if(women.isSelected()) {
sex="女";
}
float price2 = Float.parseFloat(price);
JDBC_Util.ALL_Table_CUD(sql,bookName,author,sex,price2,bookType2,bookDesc);
resetValue();
JOptionPane.showMessageDialog(null, "添加成功!");
}
/**
* 重置事件处理
* */
private void resetValueActionPerformed(ActionEvent e) {
resetValue();
}
/**
*
* 填充图书下拉框;
* */
private void fillBookType() {
String sql = "select * from booktype where id !=?";
ArrayList arr = JDBC_Util.ALL_Table_Selection(BookType.class, sql, 0);
for(Object obj : arr) {
BookType bt = (BookType)obj;
this.bookType.addItem(bt.getBookTypeName());//将获取的图书类别名称添加到下拉框
}
}
//重置事件方法
private void resetValue() {
this.bookNameTxt.setText("");
this.bookAuthorTxt.setText("");
this.bookPriceTxt.setText("");
this.bookDescTxt.setText("");
}
}
package view;
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import User_Book.BookType;
import User_Book.User;
import Util.JDBC_Util;
import Util.Juage_Util;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Font;
public class bookType_view extends JInternalFrame {
private JTextField BookNameTxt;
private JTextArea BookDescTxt;
private Connection conn = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bookType_view frame = new bookType_view();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public bookType_view() {
setFrameIcon(new ImageIcon(bookType_view.class.getResource("/图片/java.png")));
getContentPane().setBackground(SystemColor.info);
setIconifiable(true);
setClosable(true);
setTitle("图书类别添加");
setBounds(100, 100, 785, 525);
JLabel label = new JLabel("图书类别名称:");
label.setFont(new Font("宋体", Font.BOLD, 20));
JLabel lblNLabel = new JLabel("图书类别描述:");
lblNLabel.setFont(new Font("宋体", Font.BOLD, 20));
BookNameTxt = new JTextField();
BookNameTxt.setFont(new Font("宋体", Font.BOLD, 20));
BookNameTxt.setBackground(Color.WHITE);
BookNameTxt.setColumns(10);
BookDescTxt = new JTextArea();
BookDescTxt.setFont(new Font("Monospaced", Font.PLAIN, 18));
BookDescTxt.setBackground(SystemColor.textHighlightText);
JButton button = new JButton("添加");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookTypeAddActionPerformed(e);
}
});
button.setIcon(new ImageIcon(bookType_view.class.getResource("/图片/添加.png")));
button.setForeground(SystemColor.windowText);
button.setBackground(SystemColor.window);
JButton button_1 = new JButton("重置");
button_1.setBackground(SystemColor.window);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValueActionPerformed(e);//创建一个事件
}
});
button_1.setIcon(new ImageIcon(bookType_view.class.getResource("/图片/重置.png")));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(156)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(label)
.addComponent(lblNLabel))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(button)
.addPreferredGap(ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
.addComponent(button_1))
.addComponent(BookDescTxt, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
.addComponent(BookNameTxt, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE))
.addContainerGap(204, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(110)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(BookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(90)
.addComponent(BookDescTxt, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(82)
.addComponent(lblNLabel)))
.addGap(32)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(button_1))
.addContainerGap(51, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
//设置文本域边框
BookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
}
/**
* 图书类别添加事件
* */
private void BookTypeAddActionPerformed(ActionEvent e) {
//获取用户添加的文字
String bookname = this.BookNameTxt.getText();
String bookdesc = this.BookDescTxt.getText();
if(Juage_Util.isEmpty(bookname)) {
JOptionPane.showMessageDialog(null, "图书类别名不能为空");
return;
}
else if(Juage_Util.isEmpty(bookdesc)) {
JOptionPane.showMessageDialog(null, "图书类别描述不能为空");
return;
}
else if(!Juage_Util.Check_Duplicate_BookType(bookname)) {
JOptionPane.showMessageDialog(null, "图书类别名已经存在啦!");
return;
}
else {
//都可以则添加到数据库
String sql ="insert into booktype (bookTypeName,bookTypeDesc)values(?,?)";
JDBC_Util.ALL_Table_CUD(sql, bookname,bookdesc);
JOptionPane.showMessageDialog(null, "图书类别添加成功!");
resetValue();
}
}
/**
* 重置事件
* */
protected void resetValueActionPerformed(ActionEvent e) {
this.resetValue();
}
private void resetValue() {
this.BookNameTxt.setText("");
this.BookDescTxt.setText("");
}
}
package view;
import java.awt.EventQueue;
import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
public class Java extends JInternalFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Java frame = new Java();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Java() {
setIconifiable(true);
setClosable(true);
setTitle("从入门到放弃...");
setBounds(100, 100, 521, 386);
}
}
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import User_Book.User;
import Util.JDBC_Util;
import Util.Juage_Util;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Login_view extends JFrame {
private JPanel contentPane;
private JTextField UserName;
private JPasswordField PassWord;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login_view frame = new Login_view();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login_view() {
setTitle("管理员登录");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 719, 528);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setForeground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("图书管理系统");
lblNewLabel.setFont(new Font("黑体", Font.BOLD, 39));
lblNewLabel.setIcon(new ImageIcon(Login_view.class.getResource("/图片/图片2.png")));
JLabel lblNewLabel_1 = new JLabel("用 户 名:");
lblNewLabel_1.setIcon(new ImageIcon(Login_view.class.getResource("/图片/用户图片.png")));
lblNewLabel_1.setFont(new Font("黑体", Font.BOLD, 20));
JLabel lblNewLabel_2 = new JLabel("密 码:");
lblNewLabel_2.setIcon(new ImageIcon(Login_view.class.getResource("/图片/钥匙.png")));
lblNewLabel_2.setFont(new Font("黑体", Font.BOLD, 20));
JButton btnNewButton = new JButton("登录");
btnNewButton.setFont(new Font("黑体", Font.BOLD, 20));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
My_Login(e);
}
});
JButton btnNewButton_1 = new JButton("注册");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_1.setFont(new Font("黑体", Font.BOLD, 20));
UserName = new JTextField();
UserName.setFont(new Font("黑体", Font.BOLD, 20));
UserName.setColumns(10);
PassWord = new JPasswordField();
PassWord.setEchoChar('*');
PassWord.setForeground(new Color(0, 0, 0));
PassWord.setFont(new Font("黑体", Font.BOLD, 20));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(184)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel_2)
.addComponent(lblNewLabel_1))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(PassWord, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
.addComponent(UserName, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(170)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(lblNewLabel)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(56)
.addComponent(btnNewButton)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNewButton_1)
.addGap(59)))))
.addGap(203))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(62)
.addComponent(lblNewLabel)
.addGap(65)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(UserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(50)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_2)
.addComponent(PassWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(41)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(btnNewButton_1))
.addContainerGap(102, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
//设置JFrame居中显示
this.setLocationRelativeTo(null);
}
/**
* 登录事件处理
* @param e (参数类型ActionEvent e)
* */
private void My_Login(ActionEvent e) {
//getText():返回String类型获取当前界面上里面的用户名
String username = this.UserName.getText();//当前对象的
//由于密码的防护措施,在密码中getText已经淘汰了,又提供了新的方法getPassword()返回的是char类型,我们利用String的构造器转成String类型的
String password = new String(this.PassWord.getPassword());
//接下来判断输入的密码以及用户名是否为空.
//JOptionPane的静态方法ShowMessageDialog();弹出一个窗口提示消息;
if(Juage_Util.isEmpty(username)) {
JOptionPane.showMessageDialog(null,"用户名不能为空!");
}
else if(Juage_Util.isEmpty(password)) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
}
//创建User对象并且连接到数据库校验
Connection conn = JDBC_Util.GetConnection();
User user = new User(username,password);
boolean Yes_No=Juage_Util.Yes_No_Login(conn, user);
JDBC_Util.CloseConnection(conn);
if(Yes_No==true) {
dispose();//销毁当前窗口
new MainInterface().setVisible(true);//弹出另一个窗口
}else {
JOptionPane.showMessageDialog(null,"账号或密码错误!");
}
}
}
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import User_Book.BookType;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MainInterface extends JFrame {
private JPanel contentPane;
private JDesktopPane table=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainInterface frame = new MainInterface();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainInterface() {
setTitle("图书管理系统主界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1044, 791);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("基本数据维护");
mnNewMenu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
mnNewMenu.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/设置.png")));
menuBar.add(mnNewMenu);
JMenu mnNewMenu_2 = new JMenu("图书类别管理");
mnNewMenu_2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_2.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/滑稽.png")));
mnNewMenu.add(mnNewMenu_2);
JMenuItem menuItem_1 = new JMenuItem("图书类别添加");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化booKType_view对象,并将窗口显示出来
bookType_view bv = new bookType_view();
bv.setVisible(true);
//添加内部窗体组件
table.add(bv);
}
});
menuItem_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_2.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("图书类别维护");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化Book_Type_Manager对象,并将窗口显示出来
Book_Type_Manager btm = new Book_Type_Manager();
btm.setVisible(true);
//添加内部窗体组件
table.add(btm);
}
});
menuItem_2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_2.add(menuItem_2);
JMenu mnNewMenu_4 = new JMenu("图书管理");
mnNewMenu_4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_4.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/滑稽2.png")));
mnNewMenu.add(mnNewMenu_4);
JMenuItem menuItem_3 = new JMenuItem("图书添加");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化Book_Type_Manager对象,并将窗口显示出来
Book_View bv = new Book_View();
bv.setVisible(true);
//添加内部窗体组件
table.add(bv);
}
});
menuItem_3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_4.add(menuItem_3);
JMenuItem menuItem_4 = new JMenuItem("图书维护");
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化Book_Type_Manager对象,并将窗口显示出来
Book_Manager bm = new Book_Manager();
bm.setVisible(true);
//添加内部窗体组件
table.add(bm);
}
});
menuItem_4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mnNewMenu_4.add(menuItem_4);
JMenuItem menuItem_5 = new JMenuItem("安全退出");
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "是否退出系统");
if(result==0) {
dispose();//销毁当前窗体;
}
}
});
menuItem_5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
menuItem_5.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/滑稽3.png")));
mnNewMenu.add(menuItem_5);
JMenu mnNewMenu_1 = new JMenu("关于我们");
mnNewMenu_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
mnNewMenu_1.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/关于我们.png")));
menuBar.add(mnNewMenu_1);
JMenuItem mntmjava = new JMenuItem("关于java");
mntmjava.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化java对象,并将窗口显示出来
Java java = new Java();
java.setVisible(true);
//添加内部窗体组件
table.add(java);
}
});
mntmjava.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
mntmjava.setIcon(new ImageIcon(MainInterface.class.getResource("/图片/滑稽4.png")));
mnNewMenu_1.add(mntmjava);
JMenuItem menuItem = new JMenuItem("");
menuBar.add(menuItem);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
table = new JDesktopPane();
table.setBackground(Color.PINK);
contentPane.add(table, BorderLayout.CENTER);
//设置JFrame居中显示
this.setLocationRelativeTo(null);
}
}