static void commodityMenu(){
System.out.println("Select: "+"\n"+"|Commodity|"+"|Price|"+""+"|SerieNo|");
for (int i = 0; i commodity.length ; i++) {
System.out.print((i + 1) + ":");
for (int j = 0; j commodity[i].length; j++) {
System.out.print(commodity[i][j] + "\t\t");
}
if(i%itemPerPage==1){
System.out.println();
System.out.println("----------------------------------");
System.out.println("*: 下一页");
System.out.println("#: 返回");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
if(input.equals("*")){
continue;
}else if(input.equals("#")){
break;
}catch(IOException ioe){
分页的一般思想:
①记录总数
然后在页面控制好当前页的页码去后台拿你想要的数据
以上纯是思路,楼主可以参考一下,如果你想要代码,你自己去网上搜,这种例子到处都是,我就不今天这一节现眼了.呵...
//改编的,CopyOfImageViewer.java?打开一个有图片的文件夹就可浏览了.
import?java.awt.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.swing.*;
import?javazoom.jl.player.Player;
public?class?CopyOfImageViewer?implements?ActionListener,Runnable?{
JPanel?bts;
JLabel?pl;
JScrollPane?jsp;
JButton?cf,start,next,prev,stop;
JFrame?f;
JFileChooser?fc;
File?[]?sf;
int?index;
Thread?auto;
boolean?autoFlag;
//这里就是GUI布局
CopyOfImageViewer(){
pl=new?JLabel();
pl.setHorizontalAlignment(JLabel.CENTER);
jsp=new?JScrollPane(pl);
start=new?JButton("start");
next=new?JButton("");
prev=new?JButton("");
stop=new?JButton("stop");
bts=new?JPanel(new?FlowLayout(FlowLayout.CENTER));
bts.add(start);
bts.add(prev);
bts.add(next);
bts.add(stop);
cf=new?JButton("Select?a?picture?folder");
fc=new?JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f=new?JFrame();
f.getContentPane().add(cf,"North");
f.getContentPane().add(jsp,"Center");
f.getContentPane().add(bts,"South");
f.setLocationRelativeTo(null);
f.setVisible(true);
//给按钮加入事件侦听器
start.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
stop.addActionListener(this);
cf.addActionListener(this);
auto=new?Thread(this);
auto.start();
public?static?void?main(String[]?args)?{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception?e){
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new?CopyOfImageViewer();
Player?p?=?new?Player(new?FileInputStream(file));
p.play();
}catch(Exception?e){}
//处理各按键事件
public?void?actionPerformed(ActionEvent?e)?{
Object?src=e.getSource();
if(src==cf){
int?o=fc.showOpenDialog(f);
if(o==JFileChooser.APPROVE_OPTION){
sf=fc.getSelectedFile().listFiles(new?FilenameFilter(){
//合法的文件后缀
String[]?suf={".PNG",".GIF",".JPG",};
public?boolean?accept(File?dir,?String?name)?{
name=name.toUpperCase();
for(int?i=0;?isuf.length;?i++)
if(name.endsWith(suf[i]))return?true;
return?false;
});
if(sf.length0){
index=0;
showPic();
if(sf==null||sf.length==0)return;
if(src==start)startB();
else?if(src==stop)stopB();
else?if(src==next)next();
else?if(src==prev)prev();
void?prev(){
index=--index0?sf.length-1:index;
void?next(){
index=++indexsf.length-1?0:index;
public?void?run(){
while(true){
if(sf!=null?sf.length0?autoFlag){
try?{Thread.sleep(delay);}?catch?(Exception?e)?{}
next();
try?{Thread.sleep(100);}?catch?(Exception?e)?{}
private?void?stopB()?{
autoFlag=false;
private?void?startB()?{
autoFlag=true;
//显示图片
private?void?showPic()?{
if(sf==null?||?sf.length==0)return;
pl.setIcon(new?ImageIcon(sf[index].getAbsolutePath()));
System.out.println(sf[index].getAbsolutePath());
这个要配合数据库的分页的,前台异步发送请求,后台根据你的参数,如当前页,是下一页还是什么的,然后传数据到前台.
package dl.wsxx.base;
public class Pager {
private int totalRows; // 总行数
private int pageSize; // 每页显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据库中的起始行
private int pageStartRow; // 当前页开始行
private int pageEndRow; // 当前页结束行
private int hasNextPage; // 下一页存在标识[0:不存在,1:存在]
private int hasPreviousPage; // 前一页存在标识[0:不存在,1:存在]
public Pager() {
public Pager(int _totalRows,int _pageSize) {
pageSize = _pageSize;
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod 0) {
totalPages++;
currentPage = 1;
startRow = 0;
public int getStartRow() {
return startRow;
public int getpageStartRow() {
return pageStartRow;
public int getpageEndRow() {
return pageEndRow;
public int getTotalPages() {
return totalPages;
public int getCurrentPage() {
return currentPage;
public int getPageSize() {
return pageSize;
public int getHasNextPage() {
return hasNextPage;
public int getHasPreviousPage() {
return hasPreviousPage;
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
public void setStartRow(int startRow) {
this.startRow = startRow;
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
public void setHasNextPage(int hasNextPage) {
this.hasNextPage = hasNextPage;
public void setHasPreviousPage(int hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
public int getTotalRows() {
return totalRows;
public void first() {
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
public void previous() {
if (currentPage == 1) {
return;
currentPage--;
startRow = (currentPage - 1) * pageSize;
public void next() {
if (currentPage totalPages) {
currentPage++;
public void last() {
currentPage = totalPages;
public void refresh(int _currentPage) {
currentPage = _currentPage;
last();
private void hasFlagSet(int currentPage, int totalPages) {
if (currentPage == totalPages) {
this.hasPreviousPage = 0;
this.hasNextPage = 0;
this.hasPreviousPage = 1;
this.hasNextPage = 1;
这是我的工程里的分页核心代码,希望对你有用,还有ssh分页文档,可以参照研究一下.
读取你数据库的所有记录,这是总行数,然后你规定一下没页显示几条记录,然后还要记录一下当前的页数!