Book management system in Java practice (swing version) - Book adding interface and function implementation

Keywords: Java SQL

Summary of this section

In the previous section, we realized the function of book category maintenance, so this section will mainly realize the function of book addition.

 

Book adding interface

Use swing to implement the book adding interface. Change the code of BookAddPanel.java as follows:

package bookManageSystem.view;
​
import bookManageSystem.bean.BookTypeBean;
import bookManageSystem.dao.BookDao;
import bookManageSystem.dao.BookTypeDao;
import bookManageSystem.tools.ComponentTools;
​
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
​
public class BookAddPanel extends JPanel implements ActionListener {
    private ComponentTools componentTools = new ComponentTools();
​
    private Box totalVBox, funcationHBox, nameAndAuthorHBox, sexAndPriceHBox, typeHBox, descriptionHBox, buttonHBox;
    private JLabel bookAddFuncationLabel, bookNameLabel, bookAuthorLabel, authorSexLabel, bookTypeLabel,
            descriptionLabel, bookPriceLabel;
    private JTextField bookNameTextField, bookAuthorTextField, bookPriceTextField;
    private JTextArea descriptionTextArea;
    private JRadioButton femaleRadioButton, maleRadioButton;
    private ButtonGroup radioButtonGroup;
    private JComboBox bookTypeComboBox;
    private JButton addButton, resetButton;
​
    BookAddPanel() {
        // Add control content to book add panel
        this.add(createBookAddBox());
        // Batch set icons for buttons
        componentTools.setIcons(new JButton[]{addButton, resetButton}, new String[]{"src/bookManageSystem/images/add" +
                ".png", "src/bookManageSystem/images/reset.png"});
        // Book category query SQL
        String getBookTypeSQL = "select * from tb_booktype";
        // Get all book category data
        List bookTypeList = new BookTypeDao().getRecordsDataBySql(getBookTypeSQL);
        // Get all book category names
        String[] typeNames = new String[bookTypeList.size()];
        for (int i = 0; i < bookTypeList.size(); i++) {
            BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(i);
            typeNames[i] = bookTypeBean.getBookTypeName();
        }
        // Initialize options in the drop-down list box
        componentTools.addComboBoxItems(bookTypeComboBox, typeNames);
        // Register event listeners for buttons
        addButton.addActionListener(this);
        resetButton.addActionListener(this);
    }
​
    /**
     * Content control of book adding panel
     *
     * @return Return to a Box
     */
    private Box createBookAddBox() {
        totalVBox = Box.createVerticalBox();
​
        funcationHBox = Box.createHorizontalBox();
        bookAddFuncationLabel = new JLabel("Book adding function");
        bookAddFuncationLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 30));
        funcationHBox.add(bookAddFuncationLabel);
        totalVBox.add(funcationHBox);
        totalVBox.add(Box.createVerticalStrut(30));
​
        nameAndAuthorHBox = Box.createHorizontalBox();
        bookNameLabel = new JLabel("Book Name:");
        bookNameTextField = new JTextField(10);
        bookAuthorLabel = new JLabel("Author:");
        bookAuthorTextField = new JTextField(10);
        nameAndAuthorHBox.add(bookNameLabel);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(40));
        nameAndAuthorHBox.add(bookNameTextField);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(40));
        nameAndAuthorHBox.add(bookAuthorLabel);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(40));
        nameAndAuthorHBox.add(bookAuthorTextField);
        totalVBox.add(nameAndAuthorHBox);
        totalVBox.add(Box.createVerticalStrut(30));
​
        sexAndPriceHBox = Box.createHorizontalBox();
        authorSexLabel = new JLabel("Author gender:");
        Box sexHBox = Box.createHorizontalBox();
        femaleRadioButton = new JRadioButton("female");
        maleRadioButton = new JRadioButton("male");
        sexHBox.add(femaleRadioButton);
        sexHBox.add(maleRadioButton);
        radioButtonGroup = new ButtonGroup();
        radioButtonGroup.add(femaleRadioButton);
        radioButtonGroup.add(maleRadioButton);
        bookPriceLabel = new JLabel("Book price:");
        bookPriceTextField = new JTextField(5);
        sexAndPriceHBox.add(authorSexLabel);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(50));
        sexAndPriceHBox.add(sexHBox);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(100));
        sexAndPriceHBox.add(bookPriceLabel);
        nameAndAuthorHBox.add(Box.createHorizontalStrut(10));
        sexAndPriceHBox.add(bookPriceTextField);
        totalVBox.add(sexAndPriceHBox);
        totalVBox.add(Box.createVerticalStrut(30));
​
        typeHBox = Box.createHorizontalBox();
        bookTypeLabel = new JLabel("Book category:");
        // Instantiate drop-down list box control
        bookTypeComboBox = new JComboBox();
        typeHBox.add(bookTypeLabel);
        typeHBox.add(Box.createHorizontalStrut(40));
        typeHBox.add(bookTypeComboBox);
        totalVBox.add(typeHBox);
        totalVBox.add(Box.createVerticalStrut(30));
​
        descriptionHBox = Box.createHorizontalBox();
        descriptionLabel = new JLabel("Book Description:");
        descriptionTextArea = new JTextArea(10, 40);
        descriptionHBox.add(descriptionLabel);
        descriptionHBox.add(Box.createHorizontalStrut(40));
        descriptionHBox.add(descriptionTextArea);
        totalVBox.add(descriptionHBox);
        totalVBox.add(Box.createVerticalStrut(30));
​
        buttonHBox = Box.createHorizontalBox();
        addButton = new JButton("Add to");
        resetButton = new JButton("Reset");
        buttonHBox.add(addButton);
        buttonHBox.add(Box.createHorizontalStrut(80));
        buttonHBox.add(resetButton);
        totalVBox.add(buttonHBox);
​
        return totalVBox;
    }
​
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
}

Run the project, after logging in successfully, jump to the book adding function interface of the book adding menu item under the "book management" menu, and the view is as follows:

 

Book adding function

Then it realizes the function of adding books and resetting buttons.

Add the following code in actionPerformed method to realize the function of adding button in this interface:

        // Event handling of the Add button
        if (e.getSource() == addButton) {
            // Get the book name entered by the user
            String name = bookNameTextField.getText();
            // Get the book author name entered by the user
            String author = bookAuthorTextField.getText();
            // Get the gender of the book author selected by the user
            String sex = "";
            if (maleRadioButton.isSelected()) {
                sex = maleRadioButton.getText();
            } else if (femaleRadioButton.isSelected()) {
                sex = femaleRadioButton.getText();
            }
            // Get the book price entered by the user
            String price = bookPriceTextField.getText();
            // Get the book category selected by the user
            String type = (String) bookTypeComboBox.getModel().getSelectedItem();
            // Get the book description entered by the user
            String description = descriptionTextArea.getText();
​
            // Assemble book category SQL
            String bookTypeSQL = "select * from tb_booktype where btName='" + type + "';";
            // Query according to the book category selected by the user
            List bookTypeList = new BookTypeDao().getRecordsDataBySql(bookTypeSQL);
            // Get query results
            BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(0);
            // Get the id number of the book category selected by the user
            int bookTypeId = bookTypeBean.getBookTypeId();
            // Assemble new SQL statement
            String sql =
                    "insert into tb_book(bBookName, bAuthor, bSex, bPrice, bBookDescription, btId) values('" + name + "'," +
                            "'" + author + "','" + sex + "'," + price + ",'" + description + "'," + bookTypeId + ");";
            // Execute SQL and get operation results
            boolean isOK = new BookDao().dataChange(sql);
            // Judge the result of adding
            if (isOK) {
                // If it is added successfully, the user input will be reset and a prompt box will pop up
                componentTools.reset(bookNameTextField, bookAuthorTextField, bookPriceTextField, descriptionTextArea);
                componentTools.reset(radioButtonGroup);
                componentTools.reset(bookTypeComboBox);
                JOptionPane.showMessageDialog(null, "Added successfully!");
            } else {
                // Add failure also pop up prompt box
                JOptionPane.showMessageDialog(null, "Failed to add!");
            }
        }

Run the project, input the information, and click the "add" button. The effect of adding successfully is as follows:

Next is to realize the function of reset button, very simple.

Continue to add the following code in actionPerformed method, which is the processing event of reset button:

        // Event handling of reset button
        if (e.getSource() == resetButton) {
            // Reset user input and selection
            componentTools.reset(bookNameTextField, bookAuthorTextField, bookPriceTextField, descriptionTextArea);
            componentTools.reset(radioButtonGroup);
            componentTools.reset(bookTypeComboBox);
        }

 

You can search the WeChat public number [Java instance program] or scan the underlying two-dimensional code to pay more attention to the public number.

Note: in the background of public address reply [20200206] can get the source code of this section.

430 original articles published, 47 praised, 100000 visitors+
Private letter follow

Posted by plezops on Tue, 11 Feb 2020 09:05:19 -0800