| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- package com.mes.ui;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.swing.*;
- import javax.swing.table.DefaultTableCellRenderer;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableColumn;
- import java.awt.*;
- import java.awt.event.ComponentAdapter;
- import java.awt.event.ComponentEvent;
- import java.util.List;
- /**
- * 点检记录面板组件
- * 支持分页查询和展示点检记录数据
- */
- public class CheckRecordPanel extends JPanel {
- private static final Logger log = LoggerFactory.getLogger(CheckRecordPanel.class);
- // 表格列名
- private static final String[] COLUMN_NAMES = {"检查项", "检查结果", "作业人员", "日期"};
- // 分页配置
- private static final int PAGE_SIZE = 20;
- // 查询条件
- private String oprno; // 工位号
- private String lineSn; // 产线编号
- // 分页状态
- private int pageNo = 1;
- private long totalCount = 0;
- private int totalPages = 0;
- // UI组件
- private JTable table;
- private DefaultTableModel tableModel;
- private JLabel pageLabel;
- private JButton btnPrevious;
- private JButton btnNext;
- private JButton btnRefresh;
- private JLabel statusLabel;
- /**
- * 构造函数
- * @param oprno 工位号
- * @param lineSn 产线编号
- */
- public CheckRecordPanel(String oprno, String lineSn) {
- this.oprno = oprno;
- this.lineSn = lineSn;
- initUI();
- }
- /**
- * 初始化UI
- */
- private void initUI() {
- setLayout(null);
- setBackground(Color.WHITE);
- // 顶部控制区
- JPanel topPanel = new JPanel();
- topPanel.setLayout(null);
- topPanel.setBounds(0, 0, 1200, 70);
- topPanel.setBackground(new Color(248, 248, 248));
- add(topPanel);
- int xPos = 10;
- // 状态标签
- statusLabel = new JLabel("工位:" + oprno);
- statusLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- statusLabel.setBounds(xPos, 10, 250, 30);
- topPanel.add(statusLabel);
- xPos += 260;
- // 刷新按钮
- btnRefresh = new JButton("刷新");
- btnRefresh.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- btnRefresh.setBounds(xPos, 10, 120, 40);
- btnRefresh.addActionListener(e -> {
- refreshData();
- });
- topPanel.add(btnRefresh);
- xPos += 130;
- // 上一页按钮
- btnPrevious = new JButton("上一页");
- btnPrevious.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- btnPrevious.setBounds(xPos, 10, 120, 40);
- btnPrevious.addActionListener(e -> {
- if (pageNo > 1) {
- pageNo--;
- loadData();
- }
- });
- topPanel.add(btnPrevious);
- xPos += 130;
- // 页码显示
- pageLabel = new JLabel("0/0");
- pageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- pageLabel.setHorizontalAlignment(SwingConstants.CENTER);
- pageLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
- pageLabel.setBounds(xPos, 10, 120, 40);
- topPanel.add(pageLabel);
- xPos += 130;
- // 下一页按钮
- btnNext = new JButton("下一页");
- btnNext.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- btnNext.setBounds(xPos, 10, 120, 40);
- btnNext.addActionListener(e -> {
- if (pageNo < totalPages) {
- pageNo++;
- loadData();
- }
- });
- topPanel.add(btnNext);
- // 表格面板
- JPanel tablePanel = new JPanel();
- tablePanel.setBounds(0, 50, 1200, 518);
- tablePanel.setLayout(new GridLayout(1, 1, 0, 0));
- add(tablePanel);
- // 创建表格模型
- tableModel = new DefaultTableModel(COLUMN_NAMES, 0) {
- @Override
- public boolean isCellEditable(int row, int column) {
- return false;
- }
- };
- // 创建表格
- table = new JTable(tableModel);
- table.setRowHeight(32);
- table.setFont(new Font("微软雅黑", Font.PLAIN, 25));
- table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 30));
- table.getTableHeader().setReorderingAllowed(false);
- // 设置列宽
- setColumnWidths();
- // 设置居中渲染器
- DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
- centerRenderer.setHorizontalAlignment(JLabel.CENTER);
- for (int i = 0; i < table.getColumnCount(); i++) {
- table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
- }
- // 添加滚动面板
- JScrollPane scrollPane = new JScrollPane(table);
- tablePanel.add(scrollPane);
- // 添加组件大小变化监听器
- addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent e) {
- int w = getWidth();
- int h = getHeight();
- // 调整顶部面板宽度
- topPanel.setBounds(0, 0, w, 60);
- // 调整表格面板
- if (h > 50) {
- tablePanel.setBounds(0, 60, w, h - 60);
- }
- topPanel.revalidate();
- tablePanel.revalidate();
- }
- });
- }
- /**
- * 设置列宽
- */
- private void setColumnWidths() {
- TableColumn column;
- // 检查项
- column = table.getColumnModel().getColumn(0);
- column.setPreferredWidth(300);
- // 检查结果
- column = table.getColumnModel().getColumn(1);
- column.setPreferredWidth(100);
- // 作业人员
- column = table.getColumnModel().getColumn(2);
- column.setPreferredWidth(100);
- // 日期
- column = table.getColumnModel().getColumn(3);
- column.setPreferredWidth(150);
- }
- /**
- * 刷新数据(重置到第一页)
- */
- public void refreshData() {
- pageNo = 1;
- loadData();
- }
- /**
- * 加载数据
- */
- public void loadData() {
- // 在后台线程中执行数据加载
- SwingWorker<CheckRecordResp, Void> worker = new SwingWorker<CheckRecordResp, Void>() {
- @Override
- protected CheckRecordResp doInBackground() throws Exception {
- // 更新状态
- SwingUtilities.invokeLater(() -> {
- statusLabel.setText("正在加载...");
- setButtonsEnabled(false);
- });
- // 调用接口查询数据
- return DataUtil.getCheckRecordList(oprno, pageNo, PAGE_SIZE);
- }
- @Override
- protected void done() {
- try {
- CheckRecordResp resp = get();
- updateUI(resp);
- } catch (Exception e) {
- log.error("加载点检记录失败: {}", e.getMessage());
- updateUIError("加载失败: " + e.getMessage());
- }
- }
- };
- worker.execute();
- }
- /**
- * 设置按钮启用状态
- */
- private void setButtonsEnabled(boolean enabled) {
- btnRefresh.setEnabled(enabled);
- btnPrevious.setEnabled(enabled && pageNo > 1);
- btnNext.setEnabled(enabled && pageNo < totalPages);
- }
- /**
- * 更新UI显示数据
- */
- private void updateUI(CheckRecordResp resp) {
- // 清空表格
- tableModel.setRowCount(0);
- if (resp == null || !resp.isResult()) {
- String msg = resp != null ? resp.getMessage() : "请求失败";
- updateUIError(msg);
- return;
- }
- // 更新分页信息
- totalCount = resp.getCount();
- totalPages = (int) Math.ceil((double) totalCount / PAGE_SIZE);
- if (totalPages == 0) totalPages = 1;
- // 更新页码显示
- pageLabel.setText(pageNo + "/" + totalPages);
- // 更新按钮状态
- setButtonsEnabled(true);
- // 更新状态标签
- statusLabel.setText("工位:" + oprno + " 共" + totalCount + "条");
- // 填充表格数据
- List<CheckRecordData> list = resp.getList();
- if (list != null && !list.isEmpty()) {
- for (CheckRecordData data : list) {
- Object[] row = {
- data.getCheckItem(),
- data.getCheckResult(),
- data.getUpdateBy(),
- data.getUpdateDate()
- };
- tableModel.addRow(row);
- }
- }
- // 刷新表格
- table.repaint();
- }
- /**
- * 更新UI显示错误信息
- */
- private void updateUIError(String message) {
- statusLabel.setText("错误: " + message);
- pageLabel.setText("0/0");
- setButtonsEnabled(true);
- btnPrevious.setEnabled(false);
- btnNext.setEnabled(false);
- tableModel.setRowCount(0);
- }
- /**
- * 获取当前工位号
- */
- public String getOprno() {
- return oprno;
- }
- }
|