作者: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文件對圖形界面來說沒有用,所以這裏就不貼這三個文件的代碼了。
Operator.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<Book> getBookList()
{
ArrayList<Book> booklist = new ArrayList<Book>();
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<Book> findBoo(int id,String bookname,String author,String dimname,float minprice,float maxprice)
{
ArrayList<Book> booklist = new ArrayList<Book>();
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<Book> printAllbook()
{
ArrayList<Book> booklist = new ArrayList<Book>();
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);
}
}
}
MenuUI.java:
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);
}
}
AddUI.java:
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);
}
}
DeleteUI.java:
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);
}
}
ChangeUI.java:
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);
}
}
FindmenuUI.java:
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);
}
}
IDfindUI.java:
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<Book> 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);
}
}
NameFindUI.java:
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<Book> 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<Book> 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);
}
}
AuthorFindUI.java:
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<Book> 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);
}
}
PriceFindUI.java:
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<Book> 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);
}
}
Success.java:
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<Book> 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);
}
}
Error.java:
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);
}
}
PrintUI.java:
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<Book> booklist = null;
PrintUI frame = new PrintUI(booklist);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PrintUI(ArrayList<Book> 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);
}
}
ClearUI.java:
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