仿照未必是一件坏事,先要学会仿照,接着理解他,吃透他,等你懂了一定原理后,发现以前的东西不怎么够好,需要改进,那么通过你的改进,使你的软件用户体验更好,我想这应该就是微创新吧。
这个例子的源码是CSDN的一位开发者所分享且一直珍藏在我的浏览器收藏夹多年,由于历史原因现在CSDN已经找不到原文。本着开源分享互助的精神我将源码完善再次公布如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
package com.github.xuchengen.windows; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * 仿网页分页 * 作者:徐承恩 * 邮箱:xuchengen@gmail.com * 日期:2019/10/30 */ public class Pagination extends JFrame { /** * 总的页码数 */ private int pageTotal = 35; /** * 当前页 */ private int currentPage = 1; /** * 存放一系列页码按钮,默认流布局 */ private JPanel pageBtns = new JPanel(); /** * 无参数构造方法 */ private Pagination() { //设置窗口标题 this.setTitle("仿网页分页By徐承恩"); //设置JFrame的布局 this.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20)); //设置窗口可拉伸 this.setResizable(true); //档点击叉叉时,窗口可关闭 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认显示第一页 ,设置页码布局 setPageBottom(currentPage); //自适应子控件宽和高度度来调整窗口大小 this.pack(); //窗口居中 this.setLocationRelativeTo(null); //窗口可见 this.setVisible(true); } /** * 设置页码按钮 * * @param currentPage 当前页码 */ private void setPageBottom(int currentPage) { pageBtns.removeAll(); if (currentPage <= 0 || currentPage > pageTotal) { return; } //这个是显示数字按钮的总个数,不包括首页 尾页等其他按钮 int countNumBtn = 9; // 首页 1 2 3 4 5 6 7 8 9 尾页 int half = countNumBtn / 2; int startNum = 0; int endNum = 0; JButton btnFistPage = new JButton("首页"); btnFistPage.setActionCommand("首页"); btnFistPage.setToolTipText("首页"); btnFistPage.setToolTipText("首页"); btnFistPage.addActionListener(new BottomPageButtonAction()); JButton btnLastPage = new JButton("末页"); btnLastPage.setActionCommand("末页"); btnLastPage.addActionListener(new BottomPageButtonAction()); btnLastPage.setToolTipText("共" + pageTotal + "页"); Container con = this.getContentPane(); con.invalidate(); pageBtns.add(btnFistPage); if (currentPage != 1) { JButton btnPrePage = new JButton("上一页"); btnPrePage.setActionCommand("上一页"); btnPrePage.setToolTipText("上一页是第" + (Math.max(currentPage - 1, 1)) + "页"); btnPrePage.addActionListener(new BottomPageButtonAction()); pageBtns.add(btnPrePage); } // 下面开始计算从左至右数字键(JButton)上的text int minBtnNum = currentPage - half; int maxBtnNum = currentPage + half; if (minBtnNum > 0 && maxBtnNum <= pageTotal) { startNum = minBtnNum; endNum = maxBtnNum; } else if (minBtnNum <= 0) { startNum = 1; endNum = Math.min(countNumBtn, pageTotal); } else { startNum = pageTotal > countNumBtn ? pageTotal - (countNumBtn - 1) : 1; endNum = pageTotal; } for (int i = startNum; i <= endNum; i++) { JButton btn = new JButton(); btn.addActionListener(new BottomPageButtonAction(i)); btn.setActionCommand("数字"); btn.setToolTipText("第" + i + "页"); btn.setText(i + ""); if (i == currentPage) { btn.setBackground(Color.red); } else { btn.setBackground(Color.white); } pageBtns.add(btn); } if (currentPage != pageTotal) { JButton btnNextPage = new JButton("下一页"); btnNextPage.setActionCommand("下一页"); btnNextPage.setToolTipText("下一页是第" + (Math.min(currentPage + 1, pageTotal)) + "页"); btnNextPage.addActionListener(new BottomPageButtonAction()); pageBtns.add(btnNextPage); } pageBtns.add(btnLastPage); con.validate(); this.add(pageBtns); } //页码事件处理 class BottomPageButtonAction implements ActionListener { int btnNumText = 0; BottomPageButtonAction() { } BottomPageButtonAction(int num) { btnNumText = num; } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); System.out.println(command); switch (command) { case "首页": // setPageBottom(1); // 首页就是第一页,所以直接传个1 currentPage = 1; break; case "上一页": setPageBottom(currentPage - 1 >= 1 ? --currentPage : 1); break; case "下一页": setPageBottom(currentPage + 1 <= pageTotal ? ++currentPage : pageTotal); break; case "末页": setPageBottom(pageTotal); // 末页是最后的页数 currentPage = pageTotal; break; case "数字": setPageBottom(btnNumText); currentPage = btnNumText; break; } //自适应子控件宽和高度度来调整窗口大小 pack(); System.out.println("当前是第 " + currentPage + "页"); } } public static void main(String[] args) { new Pagination(); } } |
JAVA仿网页分页原理
转载请注明:思码老徐 » Java窗口编程仿网页分页原理实现