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

Continue with the project in the previous section, this section will implement the main interface, and create class files as follows:

The contents of each type of file are as follows:

AboutSoftDialog.java

package bookManageSystem.view;
​
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
​
public class AboutSoftDialog extends JDialog implements ActionListener, MouseListener {
​
    @Override
    public void actionPerformed(ActionEvent e) {
​
    }
​
    @Override
    public void mouseClicked(MouseEvent e) {
​
    }
​
    @Override
    public void mousePressed(MouseEvent e) {
​
    }
​
    @Override
    public void mouseReleased(MouseEvent e) {
​
    }
​
    @Override
    public void mouseEntered(MouseEvent e) {
​
    }
​
    @Override
    public void mouseExited(MouseEvent e) {
​
    }
}

BookAddPanel.java

package bookManageSystem.view;
​
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class BookAddPanel extends JPanel implements ActionListener {
​
    @Override
    public void actionPerformed(ActionEvent e) {
​
    }
}

BookManagePanel.java

package bookManageSystem.view;
​
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class BookManagePanel extends JPanel implements ActionListener, ListSelectionListener {
​
    @Override
    public void actionPerformed(ActionEvent e) {
​
    }
​
    @Override
    public void valueChanged(ListSelectionEvent e) {
​
    }
}

BookTypeAddPanel.java

package bookManageSystem.view;
​
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class BookTypeAddPanel extends JPanel implements ActionListener {
​
    @Override
    public void actionPerformed(ActionEvent e) {
​
    }
}

BookTypeManagePanel.java

package bookManageSystem.view;
​
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class BookTypeManagePanel extends JPanel implements ActionListener, ListSelectionListener {
​
    @Override
    public void actionPerformed(ActionEvent e) {
​
    }
​
    @Override
    public void valueChanged(ListSelectionEvent e) {
​
    }
}

Except for Main.java and LogupFrame.java, which are the codes written in the first section, there is no need to add or delete at this stage.

Then there is MainFrame.java, which is the main interface class. The code is as follows:

package bookManageSystem.view;
​
import bookManageSystem.tools.ComponentTools;
​
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
public class MainFrame extends JFrame implements ActionListener {
    private ComponentTools componentTools = new ComponentTools();
​
    private JMenuBar menuBar;
    private JMenu bookTypeManageMenu, bookManageMenu, otherMenu;
    private JMenuItem bookTypeAddMenuItem, bookTypeManageMenuItem, bookAddMenuItem, bookManageMenuItem, exitMenuItem,
            aboutSoftMenuItem;
​
    MainFrame() {
        this.setTitle("Library management system");
        this.setBounds(400, 400, 800, 700);
​
        // Add menu bar to main interface
        this.setJMenuBar(this.createMenuBar());
        // Set the content panel of the main interface
        this.setContentPane(createMainPanel());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Set not to display by default
        this.setVisible(false);
​
        // Register events for menu items in the menu bar
        bookTypeAddMenuItem.addActionListener(this);
        bookTypeManageMenuItem.addActionListener(this);
        bookAddMenuItem.addActionListener(this);
        bookManageMenuItem.addActionListener(this);
        exitMenuItem.addActionListener(this);
        aboutSoftMenuItem.addActionListener(this);
    }
​
    /**
     * Create a menu bar for the main interface
     *
     * @return Back to menu bar
     */
    private JMenuBar createMenuBar() {
        // Instantiate a menu bar object
        menuBar = new JMenuBar();
​
        // [book category management] menu
        bookTypeManageMenu = new JMenu("Book category management");
        bookTypeAddMenuItem = new JMenuItem("Book category add");
        bookTypeManageMenuItem = new JMenuItem("Book category maintenance");
        bookTypeManageMenu.add(bookTypeAddMenuItem);
        bookTypeManageMenu.add(bookTypeManageMenuItem);
        menuBar.add(bookTypeManageMenu);
​
        // [book management] menu
        bookManageMenu = new JMenu("Book management");
        bookAddMenuItem = new JMenuItem("Book addition");
        bookManageMenuItem = new JMenuItem("Book Maintenance");
        bookManageMenu.add(bookAddMenuItem);
        bookManageMenu.add(bookManageMenuItem);
        menuBar.add(bookManageMenu);
​
        // [other] menu
        otherMenu = new JMenu("Other");
        exitMenuItem = new JMenuItem("Sign out");
        aboutSoftMenuItem = new JMenuItem("About software");
        otherMenu.add(exitMenuItem);
        otherMenu.add(aboutSoftMenuItem);
        menuBar.add(otherMenu);
​
        // Batch setting icons for menu items
        componentTools.setIcons(
                new JMenuItem[]{bookTypeAddMenuItem, bookTypeManageMenuItem, bookAddMenuItem, bookManageMenuItem, exitMenuItem, aboutSoftMenuItem},
                new String[]{"src/bookManageSystem/images/add.png", "src/bookManageSystem/images/edit.png", "src" +
                        "/bookManageSystem/images/add.png", "src/bookManageSystem/images/edit.png", "src" +
                        "/bookManageSystem/images/exit.png", "src/bookManageSystem/images/about.png"}
        );
​
        return menuBar;
    }
​
    /**
     * Create content panel for main interface
     *
     * @return Back to a panel
     */
    private JPanel createMainPanel() {
        // Instantiate a panel
        JPanel panel = new JPanel();
        // Set layout in panel
        panel.setLayout(new BorderLayout());
        JLabel label = new JLabel();
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setVerticalAlignment(SwingConstants.CENTER);
        ImageIcon ii = new ComponentTools().iconSize(new ImageIcon("src/bookManageSystem/images/bookmanagesystem.png"), 600, 400);
        // Set a picture label
        label.setIcon(ii);
        // Add label to panel
        panel.add(label);
        return panel;
    }
​
    @Override
    public void actionPerformed(ActionEvent e) {
        // "Book category add" menu item event handling
        if (e.getSource() == bookTypeAddMenuItem) {
            // Replace the content panel of the main interface with the book category add panel
            this.setContentPane(new BookTypeAddPanel());
            // And set the panel display
            this.setVisible(true);
        }
        // Event handling of "book category maintenance" menu item
        if (e.getSource() == bookTypeManageMenuItem) {
            // Replace the content panel of the main interface with the book category maintenance panel
            this.setContentPane(new BookTypeManagePanel());
            // And set the panel display
            this.setVisible(true);
        }
        // Event handling of "book add" menu item
        if (e.getSource() == bookAddMenuItem) {
            // Replace the content panel of the main interface with the book adding panel
            this.setContentPane(new BookAddPanel());
            // And set the panel display
            this.setVisible(true);
        }
        // Event handling of "Book Maintenance" menu item
        if (e.getSource() == bookManageMenuItem) {
            // Replace the content panel of the main interface with the book maintenance panel
            this.setContentPane(new BookManagePanel());
            // And set the panel display
            this.setVisible(true);
        }
        // "Exit" menu item event handling
        if (e.getSource() == exitMenuItem) {
            // End the whole program
            System.exit(0);
        }
        // About software menu item event handling
        if (e.getSource() == aboutSoftMenuItem) {
            // Set up the display of about software prompt box
            new AboutSoftDialog().setVisible(true);
        }
    }
}

Although the code is completed here, the main interface will not appear if the login is successful, because this line of code is commented in the login in Section 1,

To cancel the line change comment, the code is as follows:

// When the login is successful, jump to the main interface
new MainFrame().setVisible(true);

Click the program to run, and the result is as follows:

The top menu item is clickable

After clicking, the main interface will be as follows, because there is only one blank without adding controls for these panels.

 

 

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 [20200130] can get the source code of this section.

 

Published 409 original articles, won 43 praises and 100000 visitors+
Private letter follow

Tags: Java

Posted on Sat, 01 Feb 2020 10:09:12 -0500 by grantc2