java swing make分页Next / Prev,按按钮时的下一个按钮Forword Record或Prev goback

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java swing make分页Next / Prev,按按钮时的下一个按钮Forword Record或Prev goback相关的知识,希望对你有一定的参考价值。

我是餐厅的设计秋千桌面应用程序,其中我需要前进或后退菜单组,需要两个按钮下一个/上一个仅当我使前进/后退的“下一步” /“上一步”按钮无法正常工作时,我的代码(例如,“分页”按钮,我的菜单组无法正常工作)。

以下图像截图

“

“

全局变量

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

菜单组功能代码

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();
        }

    }

下一个按钮事件代码

 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());
        }

需要上一个按钮事件

pageIndex1 = 0;

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

[菜单类别加载

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 + ":	");

                    }
                });

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

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

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); } }


enter image description here

以上是关于java swing make分页Next / Prev,按按钮时的下一个按钮Forword Record或Prev goback的主要内容,如果未能解决你的问题,请参考以下文章

我的java分页查询出现这样的异常,请问怎么解决?java.sql.SQLException: 关闭的 Resultset: next

SQL Server 2012使用Offset/Fetch Next实现分页

java 怎么分页?

Java的Swing插件的使用

java 分页ID显示问题

Laravel 5 - 手动分页