java swing使分页显示Next/Prev,Next按钮Forword Record或Prev goback按下按钮 [英] java swing make Pagination Next/Prev , Next button Forword Record or Prev goback on button press

查看:178
本文介绍了java swing使分页显示Next/Prev,Next按钮Forword Record或Prev goback按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am design swing desktop application for restaurant in which i need menu groups forward or go back required two button Next/Prev just i make Next/Prev buttons that forward or go back this is not work correctly my code like a make pagination buttons my menu group not work correct.

以下图像截图

following image screen shot

全局变量

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

                    }
                });

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

    }

推荐答案

当您需要学习新知识或修复无法正常工作的东西时,通常更容易以较小的规模而不是您自己应用.
这符合在提问或回答时发布 mre 的要求.
您的问题本质上是如何更改其容器中的JPanel,下面的代码演示了这一点,而没有其他内容:

When you need to learn something new or fix something which is not working as expected it is often much easier to do it on a smaller scale, rather than on you application.
This goes in line with SO requirement of posting mre when asking or answering.
Your question is essentially how to change JPanels in their container, and the following code demonstrates just that, and nothing else:

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



这篇关于java swing使分页显示Next/Prev,Next按钮Forword Record或Prev goback按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆