分页面板
0.为什么要有分页,解决什么问题
在我看来分页的作用就是防止数据查询量太多,减少查询数据压力。
1.主要原理
分页,就是把一堆东西按一定规则显示。
本例子所需规则如下:
- 页数
- 每页显示条数
- 总条数
- 当页数据(交给其他实现)
2.程序实现
/**
* 分页panel
* 需要执行setTotalNumber
* @author Carl Jia
* @since 14-2-19 下午4:46
*/
public abstract class PaginationPanel extends DefaultPanel {
private static final String DEFAULT_LABEL = "0";
//每页可显示的条数
private Integer[] pageSizes = {10,20,30,40,50};
//当前页数
private int currPage = 1;
//显示条数
private int pageSize = pageSizes[0];
//总页数
private int totalPage = 0;
//总数据条数
private int totalNumber = 0;
private JComboBox<Integer> pageSizeComboBox = new JComboBox<Integer>(pageSizes);
private JTextField currPageTextField = new JTextField(4);
private DefaultLabel totalPageLabel = new DefaultLabel(DEFAULT_LABEL);
private DefaultLabel startNumberLabel = new DefaultLabel(DEFAULT_LABEL);
private DefaultLabel endNumberLabel = new DefaultLabel(DEFAULT_LABEL);
private DefaultLabel totalNumberLabel = new DefaultLabel(DEFAULT_LABEL);
private DefaultButton firstButton = new DefaultButton(SwingUtil.getImageIcon(SwingUtil.getImageURL(UIImage.PAGINATION_FIRST_ICON)));
private DefaultButton lastButton = new DefaultButton(SwingUtil.getImageIcon(SwingUtil.getImageURL(UIImage.PAGINATION_LAST_ICON)));
private DefaultButton nextButton = new DefaultButton(SwingUtil.getImageIcon(SwingUtil.getImageURL(UIImage.PAGINATION_NEXT_ICON)));
private DefaultButton prevButton = new DefaultButton(SwingUtil.getImageIcon(SwingUtil.getImageURL(UIImage.PAGINATION_PREV_ICON)));
private DefaultButton reloadButton = new DefaultButton(SwingUtil.getImageIcon(SwingUtil.getImageURL(UIImage.PAGINATION_RELOAD_ICON)));
public PaginationPanel(){
initUI();
}
private void initUI(){
setLayout(new BorderLayout());
DefaultPanel leftPanel = new DefaultPanel();
DefaultPanel rightPanel = new DefaultPanel();
leftPanel.add(pageSizeComboBox);
leftPanel.add(firstButton);
leftPanel.add(prevButton);
DefaultLabel p1 = new DefaultLabel("第");
leftPanel.add(p1);
currPageTextField.setHorizontalAlignment(JTextField.CENTER);
currPageTextField.setText(String.valueOf(currPage));
leftPanel.add(currPageTextField);
DefaultLabel p2 = new DefaultLabel("页 共");
leftPanel.add(p2);
DefaultLabel p3 = new DefaultLabel("页");
leftPanel.add(totalPageLabel);
leftPanel.add(p3);
leftPanel.add(nextButton);
leftPanel.add(lastButton);
leftPanel.add(reloadButton);
add(leftPanel, BorderLayout.WEST);
DefaultLabel m1 = new DefaultLabel("显示");
rightPanel.add(m1);
rightPanel.add(startNumberLabel);
DefaultLabel m2 = new DefaultLabel("到");
rightPanel.add(m2);
rightPanel.add(endNumberLabel);
DefaultLabel m3 = new DefaultLabel("条 共");
rightPanel.add(m3);
rightPanel.add(totalNumberLabel);
DefaultLabel m4 = new DefaultLabel("条");
rightPanel.add(m4);
add(rightPanel, BorderLayout.EAST);
currPageTextField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
//按回车键执行相应操作;
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
try {
currPage = Integer.parseInt(currPageTextField.getText());
} catch (NumberFormatException nfe) {
}
updatePage();
updateData();
}
}
});
firstButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first();
}
});
lastButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
last();
}
});
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
next();
}
});
prevButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
prev();
}
});
reloadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateData();
}
});
pageSizeComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//选中
if (e.getStateChange() == ItemEvent.SELECTED) {
changePageSizeIndex(pageSizeComboBox.getSelectedIndex());
}
//未选中
if (e.getStateChange() == ItemEvent.DESELECTED) {
}
}
});
}
//更新数据记录
private void updateNumber(){
int pageTotal = this.totalNumber%pageSize;
totalNumberLabel.setText(String.valueOf(this.totalNumber));
if(pageTotal==0){
this.totalPage =this.totalNumber/pageSize;
}else{
this.totalPage =(this.totalNumber/pageSize)+1;
}
totalPageLabel.setText(String.valueOf(this.totalPage));
updateUI();
}
//首要录入数据总条数
public void setTotalNumber(int total){
this.totalNumber = total;
updateNumber();
updatePage();
}
private void changePageSizeIndex(int index){
this.pageSize = this.pageSizes[index];
updateNumber();
updatePage();
updateData();
}
public void setPageSizes(Integer[] pageSizes) {
pageSizeComboBox.removeAllItems();
this.pageSizes = pageSizes;
for (Integer size : pageSizes) {
pageSizeComboBox.addItem(size);
}
changePageSizeIndex(0);
}
/**
* 更新显示数据下标及页码
*/
private void updatePage(){
//首先判断是否有数据
if(totalNumber>0){
//如果当前页码大于总页码,当前页码设置为总页码
if(currPage>totalPage){
this.currPageTextField.setText(String.valueOf(totalPage));
this.currPage = totalPage;
}else{
this.currPageTextField.setText(String.valueOf(currPage));
}
//显示开始数据下标
int start = (currPage-1)*pageSize+1;
this.startNumberLabel.setText(String.valueOf(start));
int end = currPage*pageSize;
//如果当前页码大于总页码即显示结束下标为总条数
if(currPage == totalPage){
end = totalNumber;
}
this.endNumberLabel.setText(String.valueOf(end));
}
updateUI();
}
/**
* 第一页
*/
public void first(){
if(totalNumber>0){
currPage=1;
updateData();
updatePage();
}
}
/**
* 最后一页
*/
public void last(){
if(totalNumber>0){
currPage=totalPage;
updateData();
updatePage();
}
}
/**
* 下一页
*/
public void next(){
if(totalNumber>0){
if (++currPage > totalPage){
currPage--;
}
updatePage();
updateData();
}
}
/**
* 前一页
*/
public void prev(){
if(totalNumber>0){
if(--currPage <=0){
currPage++;
}
updatePage();
updateData();
}
}
public int getCurrPage() {
return currPage;
}
public int getPageSize() {
return pageSize;
}
//刷新数据集
abstract public void updateData();
}
/**
* 工具
* @author Carl Jia
*/
public class SwingUtil {
//图片前缀
private static final String IMAGE_URL_PREFIX = "images/";
/**
* 根据图片名称返回图片URL
* @param imageName
* @return
*/
public static String getImageURL(String imageName) {
return IMAGE_URL_PREFIX + imageName;
}
/**
* 根据图片URL返回image icon
* @param imageURL
* @return
*/
public static ImageIcon getImageIcon(String imageURL){
return new ImageIcon(imageURL);
}
/**
*
* @param imageURL
* @return
*/
public static Image getImage(String imageURL){
return Toolkit.getDefaultToolkit().getImage(imageURL);
}
/**
* 获取默认title border
* @param title 标题名
* @return
*/
public static Border getDefaultTitledBorder(String title){
return BorderFactory.createTitledBorder(new Border() {
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.setColor(UIColor.DEFAULT_BORDER);
g.drawRect(x, y, width - 1, height - 1);
}
public Insets getBorderInsets(Component c) {
return new Insets(1, 1, 1, 1);
}
public boolean isBorderOpaque() {
return true;
}
}, title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, UIFont.FONT_12_BOLD, UIColor.DEFAULT_TEXT);
}
/**
* 根据owner居中
* 【注意】需要在setBounds之后执行
* @param window
* @param owner
*/
public static void centerScreen(Window window,Window owner){
window.setLocationRelativeTo(owner);
}
}
说明:UIImage为图片地址常量类,UIFont为字体常量类,UIColor为颜色常量类。
3.使用说明
- 实现updateData数据刷新方法
- 调用setTotalNumber(Int i)方法设置总条数
- 查询数据时paginationPanel.getCurrPage()获取当前页数paginationPanel.getPageSize()获取当前页条数
- 根据3中的参数构造分页查询条件