java swing 分页,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

5X2V4.jpg

q4SAb.jpg

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("

" + a1 + "

" + getid + "

");

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) {

}

}

解决方案

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

}

}

SqNGn.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值