java swing make Pagination Next/Prev , Next button Forword Record 或 Prev goback on 按钮按下

java swing make Pagination Next/Prev , Next button Forword Record or Prev goback on button press

我正在为餐厅设计 swing 桌面应用程序,其中我需要菜单组前进或后退需要两个按钮 Next/Prev 只是我制作了 Next/Prev 前进或后退的按钮,这不能正常工作我的代码,比如制作分页按钮,我的菜单组不能正常工作。

following image screen shot

Global Variables

public Connection con;
    public PreparedStatement pst;
    public ResultSet rs;
    int totalRecord;
    private int pageIndex = 0;
    int pageSize = 5;
    int pageSize1 = 6;

Menu Group Function code

public void loadgroup(int category_id) {

        // this.pageIndex1 = page;
        try {

            Connection con5 = connectionDB.conDb();
            String s2 = "SELECT id, name,btn_color,text_color FROM `menu_group` where cat_id='" + category_id + "'";
            PreparedStatement pst2 = con5.prepareStatement(s2);
            ResultSet rs2 = pst2.executeQuery();
            groupPnl.removeAll();
            revalidate();

           // Connection con4 = connectionDB.conDb();
           // String test1 = "SELECT COUNT(*)as rowCounts from menu_group";
           // PreparedStatement pst3 = con5.prepareStatement(test1);
           // ResultSet rs3 = pst3.executeQuery();
           // int dbss = rs3.getInt("rowCounts");

            if (rs2.next() == false) {

                groupPnl.removeAll();
                groupPnl.revalidate();
                groupPnl.repaint();
                groupPnl.setVisible(false);
                right.setVisible(false);
                left.setVisible(false);

            } else {

                groupPnl.revalidate();
                groupPnl.setVisible(true);
                right.setVisible(true);
                left.setVisible(true);

                do {
                    //  rs2.first();
                    String a1 = rs2.getString(2);
                    String a2 = rs2.getString(1);
                    int a3 = rs2.getInt("btn_color");
                    int a4 = rs2.getInt("text_color");

                    PosButton btn = new PosButton();

                    btn.setText(a1);
                    btn.setVerticalAlignment((int) JButton.CENTER_ALIGNMENT);
                    btn.applyComponentOrientation(getComponentOrientation());
                    btn.setFocusPainted(false);
                    btn.setFocusable(false);
                    btn.setRequestFocusEnabled(false);
                    btn.setMaximumSize(new Dimension(80, 80));
                    btn.setPreferredSize(new Dimension(40, 35));
                    btn.setMinimumSize(new Dimension(80, 80));
                    btn.setFocusPainted(true);
                    btn.setBorderPainted(true);
                    btn.setBackground(new Color(a3, true));
                    btn.setForeground(new Color(a4, true));

                    btn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            btn.setSelected(true);
                            System.out.println(a1 + ":");
                            btn.isSelected();
                            int idss = Integer.parseInt(a2);
                            // loadbtn();
                        }
                    });

                    //String a2 = rs.getString(2);
                    btn.setHorizontalAlignment(SwingConstants.CENTER);
                    btn.setHorizontalTextPosition(AbstractButton.CENTER);
                    btn.setVerticalTextPosition(AbstractButton.BOTTOM);
                    groupPnl.add(btn);
                    groupPnl.repaint();
                    groupPnl.revalidate();

                }while (rs2.next());
            }

        } catch (SQLException e) {
            e.getStackTrace();
        }

    }

Next Button Event Code

 try {

            Connection con = connectionDB.conDb();
            String sql = "select COUNT(id) as ids from menu_cat";
            PreparedStatement pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {

                gid.setText(rs.getString("ids"));
            }
            double ab = Double.parseDouble(gid.getText());
            double sm = ab / pageSize1;
            int ch = (int) sm;

            if (ch >= pageIndex1) {
                loadgroup(pageIndex1++);
                System.out.println(pageIndex1++);

            } else {

                pageIndex1 = 0;
                loadgroup(pageIndex1++);
                //nxt.setEnabled(false);
                // pageIndex = 0;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

Prev Button Event Required

pageIndex1 = 0;

loadPage(pageIndex1--);
nxt.setEnabled(true);

Menu Category Load

private void loadPage(int page) {

        this.pageIndex = page;
        try {
            // Customize page size here
            Connection con = (Connection) connectionDB.conDb();
            String sl = "SELECT id , name,translated_name ,btn_color,text_color FROM `menu_cat` LIMIT ? OFFSET ? ";
            PreparedStatement pst = (PreparedStatement) con.prepareStatement(sl);

            pst.setInt(1, pageSize);
            pst.setInt(2, pageSize * page);

            ResultSet rs = pst.executeQuery();

            cat_panel.removeAll();

            while (rs.next()) {
                totalRecord = rs.getInt(1);
                String a1 = rs.getString("name");
                int getid = rs.getInt(1);
                String a00 = rs.getString("translated_name");

                int a3 = rs.getInt("BTN_COLOR");
                int a4 = rs.getInt("text_COLOR");
                PosButton btn = new PosButton();

                btn.setText("<html><body><center><p>" + a1 + "</center><p>" + getid + "</p></body></html>");
                btn.setVerticalAlignment((int) CENTER_ALIGNMENT);
                btn.applyComponentOrientation(getComponentOrientation());
                btn.setFocusPainted(false);
                btn.setFocusable(false);
                btn.setToolTipText(a00);
                btn.setRequestFocusEnabled(false);
                btn.setMaximumSize(new Dimension(80, 80));
                btn.setPreferredSize(new Dimension(60, 60));
                btn.setMinimumSize(new Dimension(80, 80));
                btn.setBackground(new Color(a3, true));
                btn.setForeground(new Color(a4, true));

                btn.setFocusPainted(true);
                btn.setBorderPainted(true);
                btn.setSelected(true);
                //String a2 = rs.getString(2);
                btn.setHorizontalAlignment(SwingConstants.CENTER);
                btn.setHorizontalTextPosition(AbstractButton.CENTER);
                btn.setVerticalTextPosition(AbstractButton.BOTTOM);
                //  cat_panel.setLayout(new MigLayout("fill, wrap 1 0"));
                // cat_panel = new JPanel(new MigLayout("fill, wrap 1, insets 0"));

                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        btn.setSelected(true);
                        btn.isSelected();
                        loadgroup(getid);
                        //loadgroupss(getid);

                        System.out.println(a1 + ":\t");

                    }
                });

                cat_panel.add(btn, "center");
                //add(cat_panel, "grow y");
                cat_panel.repaint();
                cat_panel.revalidate();
            }
        } catch (SQLException e) {
        }

    }

当您需要学习新东西或修复无法按预期工作的东西时,在较小的规模上进行通常比在您的应用程序上进行要容易得多。
这符合在询问或回答时发布 mre 的 SO 要求。
您的问题本质上是如何更改容器中的 JPanels,下面的代码仅演示了这一点,仅此而已:

import java.awt.*;
import javax.swing.*;

public class CardLayoutPrevNextDemo extends JPanel {

    private final CardLayout cLayout;
    private final JPanel cardsPane;
    private static int MAX_PAGES = 5;
    private int topPageIndex = 0;

    public CardLayoutPrevNextDemo(){

        cardsPane = new JPanel();
        cLayout = new CardLayout();
        cardsPane.setLayout(cLayout);
        addPages();
        changePane();

        JButton prevPage = new JButton("Prev");
        prevPage.addActionListener(e -> showPrevPane());
        JButton nextPage = new JButton("Next");
        nextPage.addActionListener(e -> showNextPane());

        JPanel buttonsPane = new JPanel(new BorderLayout());
        buttonsPane.add(prevPage,BorderLayout.WEST);
        buttonsPane.add(nextPage,BorderLayout.EAST);

        setLayout(new BorderLayout());
        add(cardsPane,BorderLayout.CENTER);
        add(buttonsPane,BorderLayout.NORTH);
    }

    private void addPages() {
        for(int i = 0; i < MAX_PAGES ; i++){
            JPanel pane = new JPanel();
            String pageName = String.valueOf(i);
            setPreferredSize(new Dimension(400,200));
            pane.add(new JLabel("Page # "+pageName));
            cardsPane.add(pageName, pane);
        }
    }

    void showNextPane() {
        topPageIndex = topPageIndex + 1 >= MAX_PAGES ?  0: topPageIndex + 1;
        changePane();
    }

    void showPrevPane() {
        topPageIndex = topPageIndex - 1 < 0  ?  MAX_PAGES -1 : topPageIndex - 1;
        changePane();
    }

    void changePane() {
        cLayout.show(cardsPane, String.valueOf(topPageIndex));
    }

    public static void main(String[] args) {

        JFrame frame =  new JFrame("Card Layout Prev  Next Demo");
        frame.setSize(400,250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.add(new CardLayoutPrevNextDemo());
        frame.pack();
        frame.setVisible(true);
    }
}