📄 searchbooksandmembers.java
字号:
package lib;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*A public class
*/
public class SearchBooksAndMembers extends JInternalFrame {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/
//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creating the label
private JLabel title = new JLabel("搜索图书和用户");
//for creating the center
private JPanel center = new JPanel();
//for creating the Center Panel
private JPanel centerBooksPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchBooksPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchBooksButtonPanel = new JPanel();
//for creating the table
private JLabel searchBooksLabel = new JLabel(" 搜索内容: ");
//for creating JComboBox
private JComboBox searchBooksTypes;
//for creating String[]
private String[] booksTypes = {"BookID", "Subject", "Title", "Author", "Publisher", "ISBN"};
//for creating the label
private JLabel booksKey = new JLabel(" 填写关键字: ");
//for cearting the text field
private JTextField booksKeyTextField = new JTextField();
//for creating the button
private JButton searchBooksButton = new JButton("搜索",new ImageIcon(ClassLoader.getSystemResource("images/search.gif")));
//for creating the Center Panel
private JPanel centerMembersPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchMembersPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel searchMembersButtonPanel = new JPanel();
//for creating the table
private JLabel searchMembersLabel = new JLabel(" 搜索内容: ");
//for creating JComboBox
private JComboBox searchMembersTypes;
//for creating String[]
private String[] membersTypes = {"MemberID","ID", "Name", "E-Mail", "Major"};
//for creating the label
private JLabel membersKey = new JLabel(" 填写关键字: ");
//for cearting the text field
private JTextField membersKeyTextField = new JTextField();
//for creating the button
private JButton searchMembersButton = new JButton("搜索",new ImageIcon(ClassLoader.getSystemResource("images/search.gif")));
//for creating the south panel
private JPanel southPanel = new JPanel();
//for creating the button
private JButton cancelButton = new JButton("取消",new ImageIcon(ClassLoader.getSystemResource("images/exit.gif")));
//for creating an array of string to store the data
private String[] booksData;
private String[] membersData;
//create objects from another classes for using them in the ActionListener
private ListSearchBooks listBooks;
private ListSearchMembers listMembers;
private Books book;
private Members member;
//for checking the information from the text field
public boolean isBooksDataCorrect() {
booksData = new String[2];
booksData[0] = searchBooksTypes.getSelectedItem().toString();
for (int i = 1; i < booksData.length; i++) {
if (!booksKeyTextField.getText().equals("")) {
if (searchBooksTypes.getSelectedItem().toString().equals("BookID")) {
booksData[i] = booksKeyTextField.getText();
}
else
booksData[i] = "'%" + booksKeyTextField.getText() + "%'";
}
else
return false;
}
return true;
}
//for checking the information from the text field
public boolean isMembersDataCorrect() {
membersData = new String[2];
membersData[0] = searchMembersTypes.getSelectedItem().toString();
for (int i = 1; i < membersData.length; i++) {
if (!membersKeyTextField.getText().equals("")) {
if (searchMembersTypes.getSelectedItem().toString().equals("MemberID")) {
membersData[i] = membersKeyTextField.getText();
}
else
membersData[i] = "'%" + membersKeyTextField.getText() + "%'";
}
else
return false;
}
return true;
}
//constructor of searchBooksAndMembers
public SearchBooksAndMembers() {
//for setting the title for the internal frame
super("搜索", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Find16.gif")));
//for getting the graphical user interface components display area
Container cp = getContentPane();
//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font
title.setFont(new Font("Default", Font.BOLD, 14));
//for adding the label
northPanel.add(title);
//for adding the north panel to the container
cp.add("North", northPanel);
//for setting the layout
center.setLayout(new BorderLayout());
//for setting the layout
centerBooksPanel.setLayout(new BorderLayout());
//for setting the layout
searchBooksPanel.setLayout(new GridLayout(2, 2, 1, 1));
//for adding the label
searchBooksPanel.add(searchBooksLabel);
//for adding the JComboBos[]
searchBooksPanel.add(searchBooksTypes = new JComboBox(booksTypes));
//for adding the label
searchBooksPanel.add(booksKey);
//for adding the text field
booksKeyTextField.setFont(new Font("Default",Font.PLAIN,12));
searchBooksPanel.add(booksKeyTextField);
//for adding the internal panel to the panel
centerBooksPanel.add("North", searchBooksPanel);
//for setting the layout
searchBooksButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button
searchBooksButtonPanel.add(searchBooksButton);
//for adding the internal panel to the center panel
centerBooksPanel.add("South", searchBooksButtonPanel);
//for setting the border
centerBooksPanel.setBorder(BorderFactory.createTitledBorder("搜索图书:"));
//for adding center panel to the center
center.add("West", centerBooksPanel);
//for setting the layout
centerMembersPanel.setLayout(new BorderLayout());
//for setting the layout
searchMembersPanel.setLayout(new GridLayout(2, 2, 1, 1));
//for adding the label
searchMembersPanel.add(searchMembersLabel);
//for adding the JComboBos[]
searchMembersPanel.add(searchMembersTypes = new JComboBox(membersTypes));
//for adding the label
searchMembersPanel.add(membersKey);
//for adding the text field
membersKeyTextField.setFont(new Font("Default",Font.PLAIN,12));
searchMembersPanel.add(membersKeyTextField);
//for adding the internal panel to the panel
centerMembersPanel.add("North", searchMembersPanel);
//for setting the layout
searchMembersButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button
searchMembersButtonPanel.add(searchMembersButton);
//for adding the internal panel to the center panel
centerMembersPanel.add("South", searchMembersButtonPanel);
//for setting the border
centerMembersPanel.setBorder(BorderFactory.createTitledBorder("搜索用户:"));
//for adding center panel to the center
center.add("East", centerMembersPanel);
//for adding the center to the container
cp.add("Center", center);
/**
*for setting the font to the lables & buttons
*/
searchBooksLabel.setFont(new Font("Default", Font.BOLD, 13));
searchBooksTypes.setFont(new Font("Default", Font.BOLD, 13));
booksKey.setFont(new Font("Default", Font.BOLD, 13));
booksKeyTextField.setFont(new Font("Default", Font.PLAIN, 13));
searchBooksButton.setFont(new Font("Default", Font.BOLD, 13));
cancelButton.setFont(new Font("Default", Font.BOLD, 13));
searchMembersLabel.setFont(new Font("Default", Font.BOLD, 13));
searchMembersTypes.setFont(new Font("Default", Font.BOLD, 13));
membersKey.setFont(new Font("Default", Font.BOLD, 13));
membersKeyTextField.setFont(new Font("Default", Font.PLAIN, 13));
searchMembersButton.setFont(new Font("Default", Font.BOLD, 13));
cancelButton.setFont(new Font("Default", Font.BOLD, 13));
//for setting the layout
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button
southPanel.add(cancelButton);
//for setting the border
southPanel.setBorder(BorderFactory.createEtchedBorder());
//for adding the south panel to the container
cp.add("South", southPanel);
/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField and passing them to listSearchBooks object*
***********************************************************************/
searchBooksButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//for checking if there is a missing information
if (isBooksDataCorrect()) {
book = new Books();
String query ="SELECT * FROM Books WHERE " + booksData[0] + " LIKE " + booksData[1];
String bookQuery = "SELECT BookID, Subject, Title, Author, Publisher," +
"Copyright, Edition, Pages, NumberOfBooks,ISBN,Library,Availble FROM Books" +
" WHERE " + booksData[0] + " LIKE " + booksData[1];
book.connection(query);
int bookID = book.getBookID();
if (bookID != 0) {
listBooks = new ListSearchBooks(bookQuery);
getParent().add(listBooks);
try {
listBooks.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
dispose();
}
else {
JOptionPane.showMessageDialog(null, "没有匹配的图书!", "出错", JOptionPane.ERROR_MESSAGE);
booksKeyTextField.setText(null);
}
}
else
JOptionPane.showMessageDialog(null, "请填写关键字", "警告", JOptionPane.WARNING_MESSAGE);
}
});
/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField and passing them to listSearchBooks object*
***********************************************************************/
searchMembersButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (isMembersDataCorrect()) {
member = new Members();
String query = "SELECT * FROM Members WHERE "+membersData[0] + " LIKE " + membersData[1];
String memberQuery = "SELECT MemberID, ID, Name, EMail, Major, Expired" +
" FROM Members WHERE " + membersData[0] + " LIKE " + membersData[1];
member.connection(query);
int memberID = member.getMemberID();
if (memberID != 0) {
listMembers = new ListSearchMembers(memberQuery);
getParent().add(listMembers);
try {
listMembers.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
dispose();
}
else {
JOptionPane.showMessageDialog(null, "没有匹配的用户!", "出错", JOptionPane.ERROR_MESSAGE);
membersKeyTextField.setText(null);
}
}
else
JOptionPane.showMessageDialog(null, "请填写关键字", "警告", JOptionPane.WARNING_MESSAGE);
}
});
//for adding the action listener for the button to dispose the frame
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -