📄 booksinformation.java
字号:
package lib;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class BooksInformation extends JInternalFrame {
//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creaing the North Label
private JLabel northLabel = new JLabel("图书信息");
//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating the edit Panel
private JPanel booksIDPanel = new JPanel();
//for creating the edit information Panel
private JPanel booksInformationPanel = new JPanel();
//for creating the edit label panel
private JPanel booksInformationLabelPanel = new JPanel();
//for creating the edit textField panel
private JPanel booksInformationTextFieldPanel = new JPanel();
//for creating the edit button panel
private JPanel booksInformationButtonPanel = new JPanel();
//for creating the label
private JLabel booksIDLabel = new JLabel("图书ID: ");
//for creating the textField
private JTextField booksIDTextField = new JTextField(25);
//for creating the button
private JButton OKButton = new JButton("确定");
//for creating the information Panel
private JPanel informationPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationLabelPanel = new JPanel();
//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[10];
//for creating an array of String
private String[] informationString = {
" 图书主题: ", " 书名: ",
" 作者: ", " 出版社: ",
" Copyright: ", " 版本号: ", " 页数: ",
" ISBN: ", " 总数: ", " 图书馆名: "
};
//for creating an Internal Panel in the center panel
private JPanel informationTextFieldPanel = new JPanel();
//for creating an array of JTextField
private JTextField[] informationTextField = new JTextField[10];
//for creating an Internal Panel in the center panel
private JPanel InformationButtonPanel = new JPanel();
//for creating a button
private JButton fButton = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/next.gif")));
//for creating a button
private JButton bButton = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/before.gif")));
//for creating South Panel
private JPanel southPanel = new JPanel();
//for creating a button
private JButton exitButton = new JButton("退出",new ImageIcon(ClassLoader.getSystemResource("images/exit.gif")));
private boolean found =false;
private boolean isEditCorrect() {
if (booksIDTextField.getText().equals(""))
return false;
return true;
}
private void clearTextField() {
booksIDTextField.setText(null);
for (int i = 0; i < informationTextField.length; i++)
informationTextField[i].setText(null);
}
//constructor of BooksInformation
public BooksInformation() {
//for setting the title for the internal frame
super("图书信息", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Information16.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 for the North Panel
northLabel.setFont(new Font("Default", Font.BOLD, 13));
//for adding the label in the North Panel
northPanel.add(northLabel);
//for adding the north panel to the container
cp.add("North", northPanel);
//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the layout
booksIDPanel.setLayout(new BorderLayout());
//for setting the border to the panel
booksIDPanel.setBorder(BorderFactory.createTitledBorder("图书ID: "));
//for setting the layout
booksInformationPanel.setLayout(new BorderLayout());
//for setting the layout
booksInformationLabelPanel.setLayout(new GridLayout(1, 1, 1, 1));
//for adding the label to the panel
booksInformationLabelPanel.add(booksIDLabel);
//for setting the font to the label
booksIDLabel.setFont(new Font("Default", Font.BOLD, 13));
//for adding the editInformationLabelPanel to the editInformationLabel
booksInformationPanel.add("West", booksInformationLabelPanel);
//for setting the layout
booksInformationTextFieldPanel.setLayout(new GridLayout(1, 1, 1, 1));
//for adding the textField to the panel
booksInformationTextFieldPanel.add(booksIDTextField);
//for setting the font to the textField
booksIDTextField.setFont(new Font("Default", Font.PLAIN, 13));
//for adding the editInformationTextField to the editInformationPanel
booksInformationPanel.add("East", booksInformationTextFieldPanel);
//for adding the editInformationPanel to the editPanel
booksIDPanel.add("North", booksInformationPanel);
//for setting the layout
booksInformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button to the panel
booksInformationButtonPanel.add(OKButton);
//for setting the fonr to the button
OKButton.setFont(new Font("Default", Font.BOLD, 13));
//for adding the editInformationButtonPanel to the editPanel
booksIDPanel.add("Center", booksInformationButtonPanel);
//for adding the editPanel to the centerPanel
centerPanel.add("North", booksIDPanel);
//for setting the layout
informationPanel.setLayout(new BorderLayout());
//for setting the border to the panel
informationPanel.setBorder(BorderFactory.createTitledBorder("图书信息: "));
//for setting the layout
informationLabelPanel.setLayout(new GridLayout(10, 1, 1, 1));
/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for (int i = 0; i < informationLabel.length; i++) {
informationLabelPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Default", Font.BOLD, 13));
}
informationPanel.add("West", informationLabelPanel);
//for setting the layout
informationTextFieldPanel.setLayout(new GridLayout(10, 1, 1, 1));
/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for (int i = 0; i < informationTextField.length; i++) {
informationTextFieldPanel.add(informationTextField[i] = new JTextField(25));
informationTextField[i].setFont(new Font("Default", Font.PLAIN, 13));
informationTextField[i].setEditable(false);
}
informationPanel.add("East", informationTextFieldPanel);
/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* and adding the button to the panel. *
* finally adding the panel to the container *
***********************************************************************/
InformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
fButton.setFont(new Font("Default", Font.BOLD, 13));
bButton.setFont(new Font("Default", Font.BOLD, 13));
InformationButtonPanel.add(bButton);
InformationButtonPanel.add(fButton);
informationPanel.add("South", InformationButtonPanel);
centerPanel.add("Center", informationPanel);
cp.add("Center", centerPanel);
/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* adding the button to the panel & setting the border. *
* finally adding the panel to the container *
***********************************************************************/
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
exitButton.setFont(new Font("Default", Font.BOLD, 13));
southPanel.add(exitButton);
southPanel.setBorder(BorderFactory.createEtchedBorder());
cp.add("South", southPanel);
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
if (isEditCorrect()) {
Thread runner = new Thread() {
public void run() {
Books book = new Books();
//for checking if there is no same information in the database
book.connection("SELECT * FROM Books WHERE BookID = " + booksIDTextField.getText());
int copyright = book.getCopyright();
if (copyright > 0) {
found = true;
informationTextField[0].setText(book.getSubject());
informationTextField[1].setText(book.getTitle());
informationTextField[2].setText(book.getAuthor());
informationTextField[3].setText(book.getPublisher());
informationTextField[4].setText(book.getCopyright() + "");
informationTextField[5].setText(book.getEdition() + "");
informationTextField[6].setText(book.getPages() + "");
informationTextField[7].setText(book.getISBN());
informationTextField[8].setText(book.getNumberOfBooks() + "");
informationTextField[9].setText(book.getLibrary());
}
else {
found = false;
JOptionPane.showMessageDialog(null, "请输入正确的图书ID号码", "出错", JOptionPane.ERROR_MESSAGE);
clearTextField();
}
}
};
runner.start();
}
else{
JOptionPane.showMessageDialog(null, "请输入正确的图书ID号码", "警告", JOptionPane.WARNING_MESSAGE);
found = false;
}
}
});
bButton.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent ae){
if(found) findBefore(Integer.parseInt(booksIDTextField.getText()));
}
});
fButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(found) findAfter(Integer.parseInt(booksIDTextField.getText()));
}
});
//for adding the action listener for the button to dispose the frame
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}
private void findBefore(int ID){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe) {
System.out.println("BooksInformation.java\n" + cnfe.toString());
}
catch (Exception e) {
System.out.println("BooksInformation.java\n" + e.toString());
}
try {
Connection connection = DriverManager.getConnection( "jdbc:odbc:JLibrary");
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery("SELECT * FROM Books");
resultSet.beforeFirst();
while (resultSet.next())
if(ID == resultSet.getInt(1)) break;
if(resultSet.previous()) {
booksIDTextField.setText(Integer.toString(resultSet.getInt(1)));
informationTextField[0].setText( resultSet.getString(2));
informationTextField[1].setText( resultSet.getString(3));
informationTextField[2].setText( resultSet.getString(4));
informationTextField[3].setText( resultSet.getString(5));
informationTextField[4].setText( Integer.toString(resultSet.getInt(6)));
informationTextField[5].setText( Integer.toString(resultSet.getInt(7)));
informationTextField[6].setText( Integer.toString(resultSet.getInt(8)));
informationTextField[7].setText( resultSet.getString(9));
informationTextField[8].setText(Integer.toString(resultSet.getInt(10)));
informationTextField[9].setText(resultSet.getString(13));
};
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException SQLe) {
System.out.println("BooksInformation.java\n" + SQLe.toString());
}
}
private void findAfter(int ID){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe) {
System.out.println("BooksInformation.java\n" + cnfe.toString());
}
catch (Exception e) {
System.out.println("BooksInformation.java\n" + e.toString());
}
try {
Connection connection = DriverManager.getConnection( "jdbc:odbc:JLibrary");
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery("SELECT * FROM Books");
resultSet.beforeFirst();
while (resultSet.next())
if(ID == resultSet.getInt(1)) break;
if(resultSet.next()) {
booksIDTextField.setText(Integer.toString(resultSet.getInt(1)));
informationTextField[0].setText( resultSet.getString(2));
informationTextField[1].setText( resultSet.getString(3));
informationTextField[2].setText( resultSet.getString(4));
informationTextField[3].setText( resultSet.getString(5));
informationTextField[4].setText( Integer.toString(resultSet.getInt(6)));
informationTextField[5].setText( Integer.toString(resultSet.getInt(7)));
informationTextField[6].setText( Integer.toString(resultSet.getInt(8)));
informationTextField[7].setText( resultSet.getString(9));
informationTextField[8].setText(Integer.toString(resultSet.getInt(10)));
informationTextField[9].setText(resultSet.getString(13));
};
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException SQLe) {
System.out.println("BooksInformation.java\n" + SQLe.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -