Java swing实现分页显示代码

Java swing实现分页显示



效果如下:


                                                       Java swing实现分页显示代码_第1张图片




代码:

      

package com.zp.test1;

import java.util.ArrayList;
import java.util.List;

public class FenYeTest {

	static List list=new ArrayList();
    
	static{
		User user=new User("0","0");
		User user1=new User("1","1");
		User user2=new User("2","2");
		User user3=new User("3","3");
		User user4=new User("4","4");
		User user5=new User("5","5");
		User user6=new User("6","6");
		User user7=new User("7","7");
		User user8=new User("8","8");
		list.add(user);
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);
		list.add(user5);
		list.add(user6);
		list.add(user7);
		list.add(user8);
	}

	public FenYeTest() {
	
	}


	public static void main(String[] args) {
		
		
	}
	
	
	public static List findUsers(int currentPage,int pageSize){ 
	
		
       List list1=new ArrayList();
       int listLength=list.size();
       if(currentPage<1){
    	   currentPage=1;
       }
       int startIndex=(currentPage-1)*pageSize;
       int endIndex=startIndex+pageSize;
       
       if(endIndex>=listLength){
    	   endIndex=listLength;
       }
	   list1=  list.subList(startIndex, endIndex);  
        return list1;  
    }  
	
	
}

package com.zp.test1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.JPanel;
import javax.swing.JButton;

public class JTableDefineTest extends JFrame{   

	 private int currentPage=1;
	 private  int pageSize=2;
	 private int lastPage;
	 JTable table=null;
	 DefaultTableModel dtm=null;
	 JScrollPane jsp=null;
	 JTableDefineTest jTableDefineTest=null;
	 List list,list1; 
	 JButton button1 =null;
	   
	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public JTableDefineTest(){  
		
		list=FenYeTest.list;
		
        if(list.size()%pageSize==0){
        	setLastPage(list.size()/getPageSize());
        }else{
        	setLastPage(list.size()/getPageSize()+1);
        }
		
		
		String[] columnNames = {"用户名","密码"}; 
		dtm=new DefaultTableModel(columnNames, 0);
		
		 table=new JTable(dtm);
		 jsp = new JScrollPane();
		 jsp.setViewportView(table);
		 getContentPane().add(jsp);
	
		setTitle("表格");       
		setBounds(100,100,500,500);       
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
	
		showTable(currentPage);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		
		JButton button = new JButton("首页");
		button.addActionListener(new MyTable());
		button.setActionCommand("首页");
		panel.add(button);
		 button1 = new JButton("上一页");
		button1.addActionListener(new MyTable());
		panel.add(button1);
		JButton button2 = new JButton("下一页");
		button2.addActionListener(new MyTable());
		panel.add(button2);
		JButton button3 = new JButton("末页");
		button3.addActionListener(new MyTable());
		panel.add(button3);
		setVisible(true);    
		
		}   
	
	public void showTable(int currentPage){
		dtm.setRowCount(0);// 清除原有行
		FenYeTest f=new FenYeTest();
		setCurrentPage(currentPage);
	   list1=f.findUsers(currentPage, pageSize);
		for(int row = 0;row




你可能感兴趣的:(Java)