Java JSE中自己封装的分页栏

很多程序,没有整理的话,不能是你的!如果你发现同一段程序要重复敲多遍,那么就把它封装起来吧。

该博文由两部分组成:效果图 + 源程序


/**   
 * @Title: SubPageBar.java
 * @Package com.graduation.myswing
 * @Description: TODO(自定义分页工具栏,包含如下信息:第i~j条(共有n条).每页显示x条。上一页 第y/z页 下一页)
 * @author 猎空   
 * @date 2012-5-23 下午08:35:43
 * @version V1.0   
 */
package com.graduation.myswing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public abstract class SubPageBar extends JPanel implements ItemListener, ActionListener {
	
	private int allCount, fromRec, endRec, pageSize, pageIndex, pageCount;
	private JLabel allCountLab, curCountLab, pageIndexLab;
	private JComboBox eachCom;
	private JButton preBtn, nextBtn;
	
	public SubPageBar(int recNums) {
		this.allCount = recNums;
		initUI();
		updateData();
	}
	
	private void initUI() {
		// comboBox
		eachCom = new JComboBox(new String[]{"20", "50", "100"});
		// label
		curCountLab = new JLabel();
		allCountLab = new JLabel();
		pageIndexLab = new JLabel();
		// button
		preBtn = new JButton("上一页");
		nextBtn = new JButton("下一页");
		// listener
		eachCom.addItemListener(this);
		preBtn.addActionListener(this);
		nextBtn.addActionListener(this);
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(curCountLab);
		this.add(allCountLab);
		this.add(new JLabel("每页"));
		this.add(eachCom);
		this.add(new JLabel("条"));
		this.add(preBtn);
		this.add(pageIndexLab);
		this.add(nextBtn);
	}
	
	/**
	 * @Description: (更新分页栏的值)
	 */
	private void updateData() {
		// 必须知道的参数值:allCount pageIndex pageSize
		pageSize = Integer.valueOf(eachCom.getSelectedItem().toString()); 
		fromRec = pageIndex*pageSize + 1;
		if(0 == allCount) {
			fromRec = 0;
		}
		endRec = (pageIndex+1)*pageSize;
		if(endRec > allCount){
			endRec = allCount;
		}
		pageSize = (0==pageSize)?1:pageSize;
		pageCount = allCount/pageSize - ((0==allCount%pageSize && 0!=allCount)?1:0);
		curCountLab.setText("第 " + fromRec + "~" + endRec + " 条");
		allCountLab.setText("(共有 " + allCount + " 条).");
		pageIndexLab.setText(" 第 " + (pageIndex+1) + "/" + (pageCount+1) + " 页 ");
		preBtn.setEnabled(pageIndex>0);
		nextBtn.setEnabled(pageIndex<pageCount);
	}
	
	/**
	 * @Description: 有新数据载入时,需要重载
	 */
	public void fresh(int recNums) {
		this.allCount = recNums;
		updateData();
	}
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		// 这边之所以要加上这个判断,是因为“选中”和“取消选中”都会触发ItemEvent.
		if(e.getStateChange() == ItemEvent.SELECTED) {
			pageIndex = 0;
			pageSize = Integer.valueOf(eachCom.getSelectedItem().toString());
			updateData();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == preBtn) {
			pageIndex --;
			onClickPreButton(pageIndex);
		}
		else if(e.getSource() == nextBtn) {
			pageIndex ++;
			onClickNextButton(pageIndex);
		}
		updateData();
	}
	
	public abstract void onPageSizeChange(int newPageSize);
	public abstract void onClickPreButton(int pageIndex);
	public abstract void onClickNextButton(int pageIndex);
}


你可能感兴趣的:(java,Date,String,Class,工具)