作者:AlexTan
E-mail: [email protected]
更新日志:
1. 修改 Operator.java 文件,Operator.java文件里将无任何输出代码,把从数据库里取出来的数据全放到ArrayList里,方便传给UI界面。此处修改使整个程序更符合MVC编程思想。
2. 给图书管理系统程序加上java的UI界面(ps:比较丑,但功能上基本实现了),java界面是通过用windowbuilder做的,怎么下的这里就不详细阐述了( 安装教程:http://blog.csdn.net/powmxypow/article/details/11553395 )。目前暂无什么BUG,如果有,欢迎留言。
Book.java与Database.java与前面没有任何变化,MainClass.java文件对图形界面来说没有用,所以这里就不贴这三个文件的代码了。
package control;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import jdbc.Database;
import model.Book;
public class Operator {
public static int clearKey = 12345678;
public ArrayList getBookList()
{
ArrayList booklist = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//3.通过数据库的连接操作数据库,实现增删改查
try {
conn = Database.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select id,bookname,author,price from book");//选择import java.sql.ResultSet;
while(rs.next()){//如果对象中有数据,就会循环打印出来
String bookname = rs.getString("bookname");
String author = rs.getString("author");
float price = rs.getFloat("price");
int ID = rs.getInt("id");
Book book = new Book(ID,bookname,author,price);
booklist.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return booklist;
}
public boolean addBook(String bookname,String author,float price)
{
Connection conn = null;
Statement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.createStatement();
String sql = "insert into book(bookname,author,price) values('"+bookname+"','"+author+"',"+price+")";
//System.out.println(sql);
stmt.execute(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
Database.close(conn, stmt);
}
}
public boolean deleteBook(int id,String bookname)
{
Connection conn = null;
Statement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.createStatement();
String sql;
if(id != -1)
{
sql = "delete from book where id ="+id;
}
else
{
sql = "delete from book where bookname ='"+bookname+"'";
}
stmt.execute(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
Database.close(conn, stmt);
}
}
public boolean changeBoo(int id,String bookname,String changename)
{
Connection conn = null;
Statement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.createStatement();
String sql;
if (id != -1)
{
sql = "update book set bookname='"+changename+"'"+" where id="+id;
//System.out.println(sql);
}
else
{
sql = "update book set bookname='"+changename+"'"+" where bookname='"+bookname+"'";
}
stmt.execute(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
Database.close(conn,stmt);
}
}
public ArrayList findBoo(int id,String bookname,String author,String dimname,float minprice,float maxprice)
{
ArrayList booklist = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = Database.getConnection();
stmt = conn.createStatement();
String sql;
if (id != -1)
{
sql = "select id,bookname,author,price from book"+" where id="+id;
//System.out.println(sql);
}
else if(bookname != null)
{
sql = "select id,bookname,author,price from book"+" where bookname='"+bookname+"'";
}
else if(author != null)
{
sql = "select id,bookname,author,price from book"+" where author='"+author+"'";
}
else if(dimname != null)
{
sql = "select id,bookname,author,price from book"+" where bookname like'%"+dimname+"%'";
}
else if(maxprice != 0)
{
sql = "select id,bookname,author,price from book where price>="+minprice+" and price<="+maxprice;
}
else
{
System.out.println("出现未知错误,请联系管理员!");
sql="";
}
rs = stmt.executeQuery(sql);
while(rs.next()){//如果对象中有数据,就会循环打印出来
String bookName = rs.getString("bookname");
String Author = rs.getString("author");
float Price = rs.getFloat("price");
int ID = rs.getInt("id");
Book book = new Book(ID,bookName,Author,Price);
booklist.add(book);
}
/*
if(rs.next())
{
System.out.println("查找成功!您查找的结果为:\n");
do{//如果对象中有数据,就会循环打印出来
System.out.println("编号:"+rs.getInt("id")+" 书名:"+rs.getString("bookname")+",作者:"+rs.getString("author")+",价格:"+rs.getFloat("price"));
}while(rs.next());
}
else
System.out.println("未查找到您想要的图书!");
*/
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Database.close(conn, stmt,rs);
}
return booklist;
}
public ArrayList printAllbook()
{
ArrayList booklist = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//3.通过数据库的连接操作数据库,实现增删改查
try {
conn = Database.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select id,bookname,author,price from book");//选择import java.sql.ResultSet;
while(rs.next()){//如果对象中有数据,就会循环打印出来
String bookName = rs.getString("bookname");
String Author = rs.getString("author");
float Price = rs.getFloat("price");
int ID = rs.getInt("id");
Book book = new Book(ID,bookName,Author,Price);
booklist.add(book);
}
/*
if(rs.next())
{
do{//如果对象中有数据,就会循环打印出来
System.out.println("编号:"+rs.getInt("id")+" 书名:"+rs.getString("bookname")+",作者:"+rs.getString("author")+",价格:"+rs.getFloat("price"));
}while(rs.next());
}
else
{
System.out.println("图书库为空,请添加图书!");
}
*/
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Database.close(conn, stmt, rs);
}
return booklist;
}
public boolean clearBook()
{
Connection conn = null;
Statement stmt = null;
try {
conn = Database.getConnection();
stmt = conn.createStatement();
String sql = "truncate table book";
stmt.execute(sql);
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
Database.close(conn, stmt);
}
}
}
package ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MenuUI extends JFrame {
private JPanel contentPane;
public static int choice;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MenuUI frame = new MenuUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MenuUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("欢迎...");
label.setBounds(145, 0, 81, 21);
contentPane.add(label);
JButton addButton = new JButton("增加图书");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddUI addui = new AddUI();
addui.setVisible(true);
MenuUI.this.dispose();
}
});
addButton.setBounds(110, 25, 123, 29);
contentPane.add(addButton);
JButton deleteButton = new JButton("删除图书");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteUI delete = new DeleteUI();
delete.setVisible(true);
MenuUI.this.dispose();
}
});
deleteButton.setBounds(110, 56, 123, 29);
contentPane.add(deleteButton);
JButton button_1 = new JButton("修改图书");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChangeUI change = new ChangeUI();
change.setVisible(true);
MenuUI.this.dispose();
}
});
button_1.setBounds(110, 90, 123, 29);
contentPane.add(button_1);
JButton button_2 = new JButton("查询图书");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FindmenuUI findmenu = new FindmenuUI();
findmenu.setVisible(true);
MenuUI.this.dispose();
}
});
button_2.setBounds(110, 124, 123, 29);
contentPane.add(button_2);
JButton button_3 = new JButton("清空图书");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClearUI clear = new ClearUI();
clear.setVisible(true);
MenuUI.this.dispose();
}
});
button_3.setBounds(110, 157, 123, 29);
contentPane.add(button_3);
JButton button_4 = new JButton("退出系统");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI.this.dispose();
}
});
button_4.setBounds(110, 189, 123, 29);
contentPane.add(button_4);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
public class AddUI extends JFrame {
private JPanel contentPane;
private JTextField booknameText;
private JTextField authorText;
private JTextField priceText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddUI frame = new AddUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("请输入您要增加的图书:");
label.setBounds(105, 15, 211, 21);
contentPane.add(label);
JLabel label_1 = new JLabel("书名:");
label_1.setBounds(15, 50, 81, 21);
contentPane.add(label_1);
JLabel label_2 = new JLabel("作者:");
label_2.setBounds(15, 95, 81, 21);
contentPane.add(label_2);
JLabel label_3 = new JLabel("价格:");
label_3.setBounds(15, 137, 81, 21);
contentPane.add(label_3);
booknameText = new JTextField();
booknameText.setBounds(115, 51, 162, 27);
contentPane.add(booknameText);
booknameText.setColumns(10);
authorText = new JTextField();
authorText.setColumns(10);
authorText.setBounds(115, 92, 162, 27);
contentPane.add(authorText);
priceText = new JTextField();
priceText.setColumns(10);
priceText.setBounds(115, 134, 162, 27);
contentPane.add(priceText);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String choice = "增加";
String name = booknameText.getText();
String author = authorText.getText();
float price = Float.parseFloat(priceText.getText());
Operator operator = new Operator();
boolean isSuccess = operator.addBook(name, author, price);
if(isSuccess)
{
Success success = new Success(choice);
success.setVisible(true);
AddUI.this.dispose();
}
else
{
Error error = new Error();
error.setVisible(true);
AddUI.this.dispose();
}
}catch (Exception e){
Error error = new Error();
error.setVisible(true);
AddUI.this.dispose();
}
}
});
button.setBounds(268, 182, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u8FD4\u56DE");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
AddUI.this.dispose();
}
});
button_1.setBounds(37, 182, 123, 29);
contentPane.add(button_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DeleteUI extends JFrame {
private JPanel contentPane;
private JTextField idText;
private JTextField nameText;
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DeleteUI frame = new DeleteUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DeleteUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("按编号删除:");
label.setBounds(24, 62, 124, 21);
contentPane.add(label);
idText = new JTextField();
idText.setBounds(163, 59, 96, 27);
contentPane.add(idText);
idText.setColumns(10);
JLabel label_1 = new JLabel("或按书名删除:");
label_1.setBounds(15, 104, 124, 21);
contentPane.add(label_1);
nameText = new JTextField();
nameText.setBounds(163, 101, 96, 27);
contentPane.add(nameText);
nameText.setColumns(10);
JButton submitButton = new JButton("\u63D0\u4EA4");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int id = -1;
String name = "";
String choice = "删除";
try{
id = Integer.parseInt(idText.getText());
System.out.println(id);
Operator operator = new Operator();
boolean isSuccess = operator.deleteBook(id,name);
if(isSuccess)
{
System.out.println("id删除成功!");
Success success = new Success(choice);
success.setVisible(true);
DeleteUI.this.dispose();
}
else
{
System.out.println("id删除失败!请检查您输入的是否正确!");
Error error = new Error();
error.setVisible(true);
DeleteUI.this.dispose();
}
}catch (Exception e){
name = nameText.getText();
if(name.trim().equals(""))
{
Error error = new Error();
error.setVisible(true);
DeleteUI.this.dispose();
}
else
{
System.out.println(name);
Operator operator = new Operator();
boolean isSuccess = operator.deleteBook(id,name);
if(isSuccess)
{
System.out.println("name删除成功!");
Success success = new Success(choice);
success.setVisible(true);
DeleteUI.this.dispose();
}
else
{
System.out.println("name删除失败!请检查您输入的是否正确!");
Error error = new Error();
error.setVisible(true);
DeleteUI.this.dispose();
}
}
}
}
});
submitButton.setBounds(257, 143, 123, 29);
contentPane.add(submitButton);
button = new JButton("\u8FD4\u56DE");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
DeleteUI.this.dispose();
}
});
button.setBounds(28, 143, 123, 29);
contentPane.add(button);
JLabel label_2 = new JLabel("\u8BF7\u8F93\u5165\u5176\u4E2D\u4E4B\u4E00");
label_2.setBounds(144, 15, 126, 21);
contentPane.add(label_2);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ChangeUI extends JFrame {
private JPanel contentPane;
private JTextField idText;
private JTextField changeText;
private JButton button_1;
private JLabel label_2;
private JTextField nameText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeUI frame = new ChangeUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangeUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("请输入您要修改的书的编号:");
label.setBounds(15, 15, 243, 21);
contentPane.add(label);
idText = new JTextField();
idText.setBounds(249, 12, 62, 27);
contentPane.add(idText);
idText.setColumns(10);
changeText = new JTextField();
changeText.setBounds(249, 115, 96, 27);
contentPane.add(changeText);
changeText.setColumns(10);
JButton button = new JButton("提交");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String choice = "修改";
int id = -1;
String name = "";
String change = changeText.getText();
Operator operator = new Operator();
if(change.equals(""))
{
Error error = new Error();
error.setVisible(true);
ChangeUI.this.dispose();
}
else
{
try{
id = Integer.parseInt(idText.getText());
System.out.println(id+" "+change);
boolean isSuccess = operator.changeBoo(id,name,change);
if(isSuccess)
{
System.out.println("修改成功!");
Success success = new Success(choice);
success.setVisible(true);
ChangeUI.this.dispose();
}
else
{
System.out.println("修改失败!");
Error error = new Error();
error.setVisible(true);
ChangeUI.this.dispose();
}
}catch (Exception e){
name = nameText.getText();
System.out.println(name+" "+change);
boolean isSuccess = operator.changeBoo(id,name,change);
if(isSuccess)
{
System.out.println("修改成功!");
Success success = new Success(choice);
success.setVisible(true);
ChangeUI.this.dispose();
}
else
{
System.out.println("修改失败!");
Error error = new Error();
error.setVisible(true);
ChangeUI.this.dispose();
}
}
}
}
});
button.setBounds(279, 200, 123, 29);
contentPane.add(button);
button_1 = new JButton("\u8FD4\u56DE");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
ChangeUI.this.dispose();
}
});
button_1.setBounds(15, 200, 123, 29);
contentPane.add(button_1);
label_2 = new JLabel("\u6216\u4E66\u540D");
label_2.setBounds(177, 52, 81, 21);
contentPane.add(label_2);
nameText = new JTextField();
nameText.setColumns(10);
nameText.setBounds(249, 49, 62, 27);
contentPane.add(nameText);
JLabel label_1 = new JLabel("\u8BF7\u8F93\u5165\u60A8\u8981\u4FEE\u6539\u4E3A\u4EC0\u4E48\u4E66\u540D\uFF1A");
label_1.setBounds(15, 118, 266, 21);
contentPane.add(label_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FindmenuUI extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FindmenuUI frame = new FindmenuUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FindmenuUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u9009\u62E9\u60A8\u8981\u5982\u4F55\u67E5\u627E\u56FE\u4E66\uFF1A");
label.setBounds(94, 33, 216, 21);
contentPane.add(label);
JButton idButton = new JButton("编号");
idButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
IDfindUI menu = new IDfindUI();
menu.setVisible(true);
FindmenuUI.this.dispose();
}
});
idButton.setBounds(134, 60, 107, 29);
contentPane.add(idButton);
JButton button = new JButton("书名");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
NameFindUI menu = new NameFindUI();
menu.setVisible(true);
FindmenuUI.this.dispose();
}
});
button.setBounds(134, 92, 107, 29);
contentPane.add(button);
JButton button_1 = new JButton("作者");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AuthorFindUI menu = new AuthorFindUI();
menu.setVisible(true);
FindmenuUI.this.dispose();
}
});
button_1.setBounds(134, 126, 107, 29);
contentPane.add(button_1);
JButton button_3 = new JButton("价格");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PriceFindUI menu = new PriceFindUI();
menu.setVisible(true);
FindmenuUI.this.dispose();
}
});
button_3.setBounds(134, 159, 107, 29);
contentPane.add(button_3);
JButton button_2 = new JButton("\u8FD4\u56DE");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
FindmenuUI.this.dispose();
}
});
button_2.setBounds(134, 189, 107, 29);
contentPane.add(button_2);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import model.Book;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class IDfindUI extends JFrame {
private JPanel contentPane;
private JTextField idText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IDfindUI frame = new IDfindUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public IDfindUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u60A8\u8981\u67E5\u627E\u7684\u7F16\u53F7:");
label.setBounds(15, 71, 210, 21);
contentPane.add(label);
idText = new JTextField();
idText.setBounds(224, 68, 96, 27);
contentPane.add(idText);
idText.setColumns(10);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = null;
int id = -1;
String author = null;
String dimname = null;
float minprice = 0;
float maxprice = -1;
Operator operator = new Operator();
try{
id = Integer.parseInt(idText.getText());
ArrayList booklist = operator.findBoo(id,name,author,dimname,minprice,maxprice);
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
IDfindUI.this.dispose();
}catch (Exception e){
Error error = new Error();
error.setVisible(true);
IDfindUI.this.dispose();
}
}
});
button.setBounds(267, 167, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("返回");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FindmenuUI menu = new FindmenuUI();
menu.setVisible(true);
IDfindUI.this.dispose();
}
});
button_1.setBounds(27, 167, 123, 29);
contentPane.add(button_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import model.Book;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class NameFindUI extends JFrame {
private JPanel contentPane;
private JTextField nameText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NameFindUI frame = new NameFindUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NameFindUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u60A8\u8981\u67E5\u627E\u7684\u4E66\u540D\uFF1A");
label.setBounds(78, 53, 227, 21);
contentPane.add(label);
nameText = new JTextField();
nameText.setBounds(88, 90, 170, 27);
contentPane.add(nameText);
nameText.setColumns(10);
JButton button = new JButton("\u6A21\u7CCA\u67E5\u627E");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = null;
int id = -1;
String author = null;
String dimname = null;
float minprice = 0;
float maxprice = -1;
Operator operator = new Operator();
dimname = nameText.getText();
if (dimname.equals(""))
{
Error error = new Error();
error.setVisible(true);
NameFindUI.this.dispose();
}
else
{
ArrayList booklist = operator.findBoo(id,name,author,dimname,minprice,maxprice);
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
NameFindUI.this.dispose();
}
}
});
button.setBounds(241, 176, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u7CBE\u786E\u67E5\u627E");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = null;
int id = -1;
String author = null;
String dimname = null;
float minprice = 0;
float maxprice = -1;
Operator operator = new Operator();
name = nameText.getText();
if (name.equals(""))
{
Error error = new Error();
error.setVisible(true);
NameFindUI.this.dispose();
}
else
{
ArrayList booklist = operator.findBoo(id,name,author,dimname,minprice,maxprice);
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
NameFindUI.this.dispose();
}
}
});
button_1.setBounds(241, 132, 123, 29);
contentPane.add(button_1);
JButton button_2 = new JButton("\u8FD4\u56DE");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FindmenuUI menu = new FindmenuUI();
menu.setVisible(true);
NameFindUI.this.dispose();
}
});
button_2.setBounds(26, 176, 123, 29);
contentPane.add(button_2);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import model.Book;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class AuthorFindUI extends JFrame {
private JPanel contentPane;
private JTextField authorText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AuthorFindUI frame = new AuthorFindUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AuthorFindUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u60A8\u8981\u67E5\u627E\u7684\u4F5C\u8005\uFF1A");
label.setBounds(15, 48, 212, 21);
contentPane.add(label);
authorText = new JTextField();
authorText.setBounds(226, 45, 124, 27);
contentPane.add(authorText);
authorText.setColumns(10);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = null;
int id = -1;
String author = null;
String dimname = null;
float minprice = 0;
float maxprice = -1;
Operator operator = new Operator();
author = authorText.getText();
if (author.equals(""))
{
Error error = new Error();
error.setVisible(true);
AuthorFindUI.this.dispose();
}
else
{
ArrayList booklist = operator.findBoo(id,name,author,dimname,minprice,maxprice);
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
AuthorFindUI.this.dispose();
}
}
});
button.setBounds(278, 158, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u8FD4\u56DE");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FindmenuUI menu = new FindmenuUI();
menu.setVisible(true);
AuthorFindUI.this.dispose();
}
});
button_1.setBounds(40, 158, 123, 29);
contentPane.add(button_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import model.Book;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class PriceFindUI extends JFrame {
private JPanel contentPane;
private JTextField minpriceText;
private JTextField maxpriceText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PriceFindUI frame = new PriceFindUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PriceFindUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u60A8\u8981\u67E5\u627E\u7684\u4EF7\u683C\u533A\u95F4\uFF1A");
label.setBounds(50, 28, 274, 21);
contentPane.add(label);
minpriceText = new JTextField();
minpriceText.setBounds(36, 64, 96, 27);
contentPane.add(minpriceText);
minpriceText.setColumns(10);
maxpriceText = new JTextField();
maxpriceText.setColumns(10);
maxpriceText.setBounds(217, 64, 96, 27);
contentPane.add(maxpriceText);
JLabel label_1 = new JLabel("\u81F3");
label_1.setBounds(160, 67, 81, 21);
contentPane.add(label_1);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = null;
int id = -1;
String author = null;
String dimname = null;
float minprice = 0;
float maxprice = -1;
Operator operator = new Operator();
try{
minprice = Float.parseFloat(minpriceText.getText());
maxprice = Float.parseFloat(maxpriceText.getText());
ArrayList booklist = operator.findBoo(id,name,author,dimname,minprice,maxprice);
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
PriceFindUI.this.dispose();
}catch (Exception e){
Error error = new Error();
error.setVisible(true);
PriceFindUI.this.dispose();
}
}
});
button.setBounds(245, 156, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u8FD4\u56DE");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FindmenuUI menu = new FindmenuUI();
menu.setVisible(true);
PriceFindUI.this.dispose();
}
});
button_1.setBounds(15, 156, 123, 29);
contentPane.add(button_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import model.Book;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class Success extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Success frame = new Success("haha");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Success(String choice) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel(choice+"成功!");
label.setBounds(152, 69, 130, 21);
contentPane.add(label);
JButton button = new JButton("\u8FD4\u56DE\u83DC\u5355");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
Success.this.dispose();
}
});
button.setBounds(31, 142, 130, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u67E5\u770B\u6240\u6709\u56FE\u4E66");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Operator operator = new Operator();
ArrayList booklist = operator.printAllbook();
PrintUI menu = new PrintUI(booklist);
menu.setVisible(true);
Success.this.dispose();
}
});
button_1.setBounds(219, 142, 141, 29);
contentPane.add(button_1);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Error extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Error frame = new Error();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Error() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u64CD\u4F5C\u5931\u8D25\uFF01");
label.setBounds(151, 67, 110, 21);
contentPane.add(label);
JButton button = new JButton("\u8FD4\u56DE\u4E3B\u83DC\u5355");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
Error.this.dispose();
}
});
button.setBounds(246, 168, 123, 29);
contentPane.add(button);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import model.Book;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
public class PrintUI extends JFrame {
private JPanel contentPane;
private JTable table;
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArrayList booklist = null;
PrintUI frame = new PrintUI(booklist);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PrintUI(ArrayList booklist) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
int high = booklist.size();
Object[][] obj = new Object[high][4];
for (int j = 0; j < booklist.size(); j++)
{
Book book = (Book)booklist.get(j);
System.out.println("编号"+book.getBookid()+" 书名:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
obj[j][0] = Integer.toString(book.getBookid());
obj[j][1] = book.getBookname();
obj[j][2] = book.getAuthor();
obj[j][3] = book.getPrice() + "";
}
table = new JTable();
table.setModel(new DefaultTableModel(
obj,
new String[] {
"\u7F16\u53F7", "\u4E66\u540D", "\u4F5C\u8005", "\u4EF7\u683C"
}
));
table.setBounds(54, 211, 306, -186);
JScrollPane scroll = new JScrollPane(table);
scroll.setLocation(0, 0);
scroll.setSize(428,208);
contentPane.add(scroll);
button = new JButton("\u8FD4\u56DE\u4E3B\u83DC\u5355");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
PrintUI.this.dispose();
}
});
button.setBounds(271, 215, 123, 29);
contentPane.add(button);
this.setVisible(true);
}
}
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import control.Operator;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ClearUI extends JFrame {
private JPanel contentPane;
private JPasswordField keyText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClearUI frame = new ClearUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ClearUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u6E05\u7A7A\u5BC6\u7801\uFF1A");
label.setBounds(41, 70, 155, 21);
contentPane.add(label);
keyText = new JPasswordField();
keyText.setBounds(180, 67, 137, 27);
contentPane.add(keyText);
JButton button = new JButton("\u63D0\u4EA4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
int key = Integer.parseInt(keyText.getText());
if(key == Operator.clearKey)
{
Operator operator = new Operator();
boolean isSuccess = operator.clearBook();
String choice = "清空";
if(isSuccess)
{
System.out.println("清空成功!");
Success success = new Success(choice);
success.setVisible(true);
ClearUI.this.dispose();
}
else
{
System.out.println("清空失败!");
Error error = new Error();
error.setVisible(true);
ClearUI.this.dispose();
}
}
else
{
System.out.println("您没有权限执行此操作!");
Error error = new Error();
error.setVisible(true);
ClearUI.this.dispose();
}
}catch (Exception f){
Error error = new Error();
error.setVisible(true);
ClearUI.this.dispose();
}
}
});
button.setBounds(277, 166, 123, 29);
contentPane.add(button);
JButton button_1 = new JButton("\u8FD4\u56DE");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MenuUI menu = new MenuUI();
menu.setVisible(true);
ClearUI.this.dispose();
}
});
button_1.setBounds(41, 166, 123, 29);
contentPane.add(button_1);
}
}
代码比较多,不过主要是界面的代码,后面会陆续更新发到github上...
请期待下一篇:Java入门教程之图书管理系统(由简入繁)(八)
转载请注明出处:http://blog.csdn.net/alextan_/article/details/70272804