swing表格分页,并且可以显示页数和跳转到知道页
时间: 2024-03-17 13:44:32 浏览: 191
表格显示并具有分页功能
以下是一个实现JTable分页,并且可以显示页数和跳转到指定页的示例代码:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TablePaginationExample extends JFrame {
private JTable table;
private JLabel lblPage;
private JTextField txtPage;
private JButton btnPrev;
private JButton btnNext;
private JButton btnGo;
private int currentPage = 1;
private int totalPage = 1;
public TablePaginationExample() {
// 初始化表格
DefaultTableModel model = new DefaultTableModel(getData(currentPage), getColumnNames());
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// 初始化分页按钮
btnPrev = new JButton("上一页");
btnNext = new JButton("下一页");
btnPrev.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentPage > 1) {
currentPage--;
refreshTable();
}
}
});
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentPage < totalPage) {
currentPage++;
refreshTable();
}
}
});
// 初始化页数和跳转控件
lblPage = new JLabel("第1页,共1页");
txtPage = new JTextField(5);
txtPage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int page = Integer.parseInt(txtPage.getText());
if (page >= 1 && page <= totalPage) {
currentPage = page;
refreshTable();
}
} catch (NumberFormatException ex) {
// 忽略非数字输入
}
}
});
btnGo = new JButton("跳转");
btnGo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int page = Integer.parseInt(txtPage.getText());
if (page >= 1 && page <= totalPage) {
currentPage = page;
refreshTable();
}
} catch (NumberFormatException ex) {
// 忽略非数字输入
}
}
});
// 添加组件到窗口
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
JPanel paginationPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
paginationPanel.add(btnPrev);
paginationPanel.add(btnNext);
paginationPanel.add(lblPage);
paginationPanel.add(txtPage);
paginationPanel.add(btnGo);
panel.add(paginationPanel, BorderLayout.SOUTH);
add(panel);
// 设置窗口属性
setTitle("JTable分页示例");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
// 刷新表格数据
private void refreshTable() {
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.setDataVector(getData(currentPage), getColumnNames());
lblPage.setText(String.format("第%d页,共%d页", currentPage, totalPage));
txtPage.setText(String.valueOf(currentPage));
}
// 获取表格数据
private Object[][] getData(int page) {
int pageSize = 10; // 每页显示10行数据
int start = (page - 1) * pageSize + 1;
int end = start + pageSize - 1;
totalPage = 20; // 假设总共有20页数据
if (end > totalPage * pageSize) {
end = totalPage * pageSize;
}
Object[][] data = new Object[pageSize][3];
for (int i = start; i <= end; i++) {
data[i - start][0] = "数据" + i;
data[i - start][1] = "数据" + i;
data[i - start][2] = "数据" + i;
}
return data;
}
// 获取表格列名
private String[] getColumnNames() {
String[] columnNames = {"列1", "列2", "列3"};
return columnNames;
}
public static void main(String[] args) {
new TablePaginationExample();
}
}
```
在这个示例中,我们添加了一个JLabel来显示当前页码和总页数,以及一个JTextField和一个JButton来实现跳转到指定页码。当用户点击跳转按钮或在JTextField中按下Enter键时,我们尝试将输入的页码转换为数字并进行跳转。注意,为了避免用户输入非数字字符,我们使用了try-catch结构来捕获NumberFormatException异常。
阅读全文