锁定老帖子 主题:分页封装1
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-29
public interface PageInterface { public List getTableList(int page, int rows); } public class PageFactory { public static PageInterface getClass(String className) throws InstantiationException, IllegalAccessException { PageInterface production = null; try { production = (PageInterface) Class.forName(className).newInstance(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } return production; } } import java.util.List; public class PageList { public List showListOfTable(String a, int page, int rows) throws InstantiationException, IllegalAccessException { PageInterface cat = PageFactory.getClass(a); List list = cat.getTableList(page, rows); return list; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-04-29
package aPage;
import java.util.ArrayList; import java.util.List; import javax.swing.table.DefaultTableModel; public class PageTabelModel extends DefaultTableModel{ /** * */ private static final long serialVersionUID = 1L; String header[]; List<Object[]> data = new ArrayList<Object[]>(); public PageTabelModel(String header[]){ this.header=header; } public List<Object[]> getData() { return data; } public void setData(List<Object[]> data) { this.data = data; } public int getColumnCount() { return header.length; } public String getColumnName(int c) { return header[c]; } public Object getValueAt(int row, int column) { return data.get(row)[column]; } public boolean isCellEditable(int r, int c) { return false; } public int getRowCount() { if (data != null) { return data.size(); } else { return 0; } } } |
|
返回顶楼 | |
发表时间:2010-04-29
公共模板
import java.awt.Rectangle; import java.util.List; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JButton; import javax.swing.JLabel; public class PagePanel extends JPanel { private static final long serialVersionUID = 1L; private JScrollPane jScrollPane = null; private JTable jTable = null; // ====================== private String page; // @jve:decl-index=0: private PageTabelModel model; private List<Object[]> tableData; // @jve:decl-index=0: private int totalPage = 20; private int numberOfPage = 1; private String className; private PageList pList = new PageList(); // @jve:decl-index=0: private int rows; private String header[]; private int width = 400; private int height = 300; private int leftWidth = 5; // =0: private JButton firstButton = null; private JButton preButton = null; private JButton nextButton = null; private JButton lastButton = null; private JLabel jLabel = null; /** * * @param className 类名 * @param header 表头数组 * @param tableData 表格初始显示信息 * @param rows 每页显示行数 * @param width 表格宽度 * @param height 表格高度 * @param leftWidth 分页按钮距离左边宽度 */ public PagePanel(String className, String header[], List<Object[]> tableData, int rows, int width, int height, int leftWidth) { super(); this.className = className; this.header = header; this.tableData = tableData; this.rows = rows; this.width = width; this.height = height; this.leftWidth = leftWidth; page = "共 " + totalPage + " 页 当前第 " + numberOfPage + " 页"; initialize(); } /** * This method initializes this * * @return void */ private void initialize() { jLabel = new JLabel(); jLabel.setBounds(new Rectangle(leftWidth + 270, height - 30, 124, 24)); jLabel.setText(page); this.setSize(width, height); this.setLayout(null); this.add(getJScrollPane(), null); this.add(getFirstButton(), null); this.add(getPreButton(), null); this.add(getNextButton(), null); this.add(getLastButton(), null); this.add(jLabel, null); } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setBounds(new Rectangle(1, 1, width - 2, height - 40)); jScrollPane.setViewportView(getJTable()); jScrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); } return jScrollPane; } /** * This method initializes jTable * * @return javax.swing.JTable */ private JTable getJTable() { if (jTable == null) { model = new PageTabelModel(header); jTable = new JTable(); model.setData(tableData); jTable.setModel(model); } return jTable; } /** * This method initializes firstButton * * @return javax.swing.JButton */ private JButton getFirstButton() { if (firstButton == null) { firstButton = new JButton(); firstButton .setBounds(new Rectangle(leftWidth, height - 30, 60, 24)); firstButton.setText("首页"); firstButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { numberOfPage = 1; try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(); } }); } return firstButton; } /** * This method initializes preButton * * @return javax.swing.JButton */ private JButton getPreButton() { if (preButton == null) { preButton = new JButton(); preButton.setText("上一页"); preButton.setBounds(new Rectangle(leftWidth + 60, height - 30, 75, 24)); preButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { if (numberOfPage <= totalPage && numberOfPage > 1) { numberOfPage = numberOfPage - 1; } try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(); } }); } return preButton; } /** * This method initializes nextButton * * @return javax.swing.JButton */ private JButton getNextButton() { if (nextButton == null) { nextButton = new JButton(); nextButton.setText("下一页"); nextButton.setBounds(new Rectangle(leftWidth + 135, height - 30, 75, 24)); nextButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { if (numberOfPage < totalPage) { numberOfPage = numberOfPage + 1; } try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(); } }); } return nextButton; } /** * This method initializes lastButton * * @return javax.swing.JButton */ private JButton getLastButton() { if (lastButton == null) { lastButton = new JButton(); lastButton.setText("尾页"); lastButton.setBounds(new Rectangle(leftWidth + 210, height - 30, 60, 24)); lastButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { numberOfPage = totalPage; try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(); } }); } return lastButton; } public void setDataOfTable() { model.setData(tableData); jTable.updateUI(); page = "共 " + totalPage + " 页 当前第 " + numberOfPage + " 页"; jLabel.setText(page); if (numberOfPage == 1) { firstButton.setEnabled(false); preButton.setEnabled(false); nextButton.setEnabled(true); lastButton.setEnabled(true); } else if (numberOfPage == totalPage) { firstButton.setEnabled(true); preButton.setEnabled(true); nextButton.setEnabled(false); lastButton.setEnabled(false); } else { firstButton.setEnabled(true); preButton.setEnabled(true); nextButton.setEnabled(true); lastButton.setEnabled(true); } } } |
|
返回顶楼 | |
发表时间:2010-04-29
实体类
package aPage; public class PageTableBean { String name; String address; int age; String sex; String photo; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } } |
|
返回顶楼 | |
发表时间:2010-04-29
测试
package aPage; import java.awt.Rectangle; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class PageFrame extends JFrame implements PageInterface { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JPanel jPanel = null; List listData; String className = "aPage.PageFrame"; PagePanel panel; /** * This is the default constructor */ public PageFrame() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(800, 700); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJPanel(), null); } return jContentPane; } /** * This method initializes jPanel * * @return javax.swing.JPanel */ private JPanel getJPanel() { if (jPanel == null) { jPanel = new JPanel(); jPanel.setLayout(null); jPanel.setBounds(new Rectangle(17, 6, 700, 600)); String header[] = { "姓名", "地址", "年龄", "性别", "电话" }; int rows = 20; List firstList = this.getTableList(1, rows); panel = new PagePanel(className, header, firstList, rows,700,500,100); jPanel.add(panel); } return jPanel; } public List getTableList(int page, int rows) { List list = getList(page, rows); listData = new ArrayList(); if (list.size() > 0 && list != null) { for (Object bean : list) { if (bean instanceof PageTableBean) { PageTableBean pageBean = (PageTableBean) bean; String name = ""; if (pageBean.getName() != null) { name = pageBean.getName(); } String address = ""; if (pageBean.getAddress() != null) { address = pageBean.getAddress(); } int age = 0; if (pageBean.getAge() != 0) { age = pageBean.getAge(); } String sex = ""; if (pageBean.getSex() != null) { sex = pageBean.getSex(); } String photo = ""; if (pageBean.getPhoto() != null) { photo = pageBean.getPhoto(); } Object[] ob = new Object[5]; ob[0] = name; ob[1] = address; ob[2] = age; ob[3] = sex; ob[4] = photo; listData.add(ob); } } } return listData; } public static List<PageTableBean> getList(int page, int rows) { List<PageTableBean> list = new ArrayList<PageTableBean>(); for (int i = page; i < rows + page; i++) { PageTableBean bean = new PageTableBean(); bean.setName("name" + i); bean.setAddress("address" + i); bean.setAge(i); if (i % 2 == 0) { bean.setSex("男"); } else { bean.setSex("女"); } bean.setPhoto("photo" + i); list.add(bean); } return list; } } // @jve:decl-index=0:visual-constraint="10,10" |
|
返回顶楼 | |
发表时间:2010-04-29
因公司不给上次文件,只能这样分享下咯
|
|
返回顶楼 | |
发表时间:2010-04-29
cs的呀 .... 好么
|
|
返回顶楼 | |
发表时间:2010-04-29
感觉还可以吧
|
|
返回顶楼 | |
发表时间:2010-04-29
注释少了点哦
|
|
返回顶楼 | |
发表时间:2010-04-29
重新排版吧
|
|
返回顶楼 | |