package com.mes.ui; import com.alibaba.fastjson2.JSONObject; import com.mes.component.MesRadio; import com.mes.component.MesWebView; import com.mes.netty.NettyClient; import com.mes.netty.ProtocolParam; import com.mes.util.DateLocalUtils; import com.mes.util.ErrorMsg; import com.mes.util.HttpUtils; import com.mes.util.JdbcUtils; import javafx.embed.swing.JFXPanel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.*; import java.io.*; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDateTime; import java.util.List; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MesClient extends JFrame { public static final Logger log = LoggerFactory.getLogger(MesClient.class); public static int mes_auth = 0; // 权限级别 0=无权限 1=操作工人 2=管理员 public static String mes_gw = ""; // 工位号 public static String mes_gw_des = ""; // 工位名称 public static String mes_server_ip = ""; // 服务器IP地址 public static int mes_tcp_port = 3000; // TCP服务端口 public static int mes_heart_beat_cycle = 10; // 心跳周期 public static int mes_heart_icon_cycle = 1; public static String mes_line_sn = ""; // 产线编号 //TCP连接 public static NettyClient nettyClient; //TCP连接状态 public static boolean tcp_connect_flag = false; //TCP连接请求 public static boolean connect_request_flag = false; //session public static String sessionid = ""; public static JPanel contentPane; public static MesClient mesClientFrame; public static JTabbedPane tabbedPane; public static JScrollPane indexScrollPaneA; // public static JScrollPane searchScrollPane; public static JScrollPane searchScrollPaneDj; public static Boolean check_quality_result = false; public static Integer work_status = 0; public static JButton heart_beat_menu; public static JButton status_menu; public static JButton user_menu; public static int scan_type = 0; public static JButton finish_ok_bt; public static JButton finish_ng_bt; public static JTextField product_sn; public static JButton f_scan_data_bt_1; public static JButton nextBtn1; // 面板1的下一步 public static JButton prevBtn2; // 面板2的上一步 public static String user20 = ""; public static JFrame welcomeWin; // public static JPanel indexPanelB; public static MesWebView jfxPanel = null; public static JPanel indexPanelC; public static MesWebView jfxPanel2 = null; public static MesRadio mesRadioHj; public static JTable table; public static Object[] columnNames = { "物料名称", "绑定批次", "剩余次数", "操作" }; public static Object[][] rowData = null; public static JLabel fxlabel; public static JLabel timer_label; // 倒计时显示 public static boolean timer_flag = false; // 开始计时标志 false=非工作开始计时 true=工件开始计时 public static long timer_nums = 0; // 秒数 public static JButton[] weldButtons = new JButton[116]; // 存储按钮对象 public static boolean[] weldClickStates = new boolean[116]; // 存储点击状态 public static JButton btnNormal; public static JButton btnRepair; public static int isRepair = 0; // 默认 0:正常, 1:返修 public static void main(String[] args) { if (LockUtil.getInstance().isAppActive() == true){ // JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出"); return; }else{ EventQueue.invokeLater(new Runnable() { @Override public void run() { try{ //读文件配置 readProperty(); // 显示界面 mesClientFrame = new MesClient(); mesClientFrame.setVisible(false); JdbcUtils.getConn(); welcomeWin = new LoginFarme(); welcomeWin.setVisible(true); getMaterailData(); }catch (Exception e){ e.printStackTrace(); } } }); } } //读配置文件 private static void readProperty() throws IOException{ String enconding = "UTF-8"; InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties"); Properties pro = new Properties(); BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding)); pro.load(br); mes_gw = pro.getProperty("mes.gw"); mes_server_ip = pro.getProperty("mes.server_ip"); mes_tcp_port = Integer.parseInt(pro.getProperty("mes.tcp_port")); mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle")); mes_line_sn = pro.getProperty("mes.line_sn"); mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw); log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle); } // // 初始化TCP // public static void initTcpConnection() { // try { // if(nettyClient==null) { // //初始化TCP连接 // nettyClient = new NettyClient(); // //TCP连接状态 // tcp_connect_flag = false; // //设置TCP请求状态 // connect_request_flag = true; // // DataUtil.synrTcp(nettyClient,mes_gw); // } // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } //启动心跳包程序 public static java.util.Timer heartBeatTimer; public static java.util.Timer heartBeatIconTimer; public static boolean iconREDFlag = true; public static void startHeartBeatTimer() { if(heartBeatTimer!=null) { heartBeatTimer.cancel(); } //心跳显示图标 if(heartBeatIconTimer!=null) { heartBeatIconTimer.cancel(); } heartBeatIconTimer = new Timer(); heartBeatIconTimer.schedule(new TimerTask() { public void run() { if(iconREDFlag) { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); iconREDFlag = false; }else { heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"))); iconREDFlag = true; } heart_beat_menu.setText(DateLocalUtils.getCurrentTime()); heart_beat_menu.repaint(); } }, 100,mes_heart_icon_cycle*1000); } //设置tcp连接状态显示 public static void setTcpStatus() { status_menu.setText("已连接MES服务器"); heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"))); heart_beat_menu.repaint(); } public static void initWarehouseData(){ resetScanA(); } public static void resetScanA() { work_status = 0; check_quality_result = false; MesClient.finish_ok_bt.setEnabled(false); MesClient.finish_ng_bt.setEnabled(false); // product_sn.setText("000020015308-0100101425022500085"); product_sn.setText(""); MesClient.fxlabel.setVisible(false); MesClient.f_scan_data_bt_1.setEnabled(true); MesClient.status_menu.setText("请扫工件码"); updateMaterailData(); shiftUserCheck(); // 重置漏点按钮状态 closeWeldButton(); closeRepairButton(); btnNormal.setBackground(null); btnRepair.setBackground(null); isRepair = 0; // 重置时自动跳转回第一个标签页 (索引0) if (tabbedPane != null) { tabbedPane.setSelectedIndex(0); } } public static int userLoginHours;//用户登录所处小时 //换班用户信息检查 private static void shiftUserCheck() { LocalDateTime now = LocalDateTime.now(); // 判断时间范围 if (userLoginHours >= 8 && userLoginHours < 20) { int hour = now.getHour(); if (hour >= 20 || hour < 8) { logoff(); } } else { int hour = now.getHour(); if (hour >= 8 && hour < 20) { logoff(); } } } public static void resetScanB() { } //获取用户20位 public static void getUser() { user20 = user_menu.getText().toString(); String space_tmp1 = ""; if(user20.length()<20) { for(int i=0;i<20-user20.length();i++) { space_tmp1 = space_tmp1 + " "; } } user20 = user20 + space_tmp1; } //获取barcode内容36位 public static String getBarcode(String barcodeTmp) { String barcodeRet = barcodeTmp; if(barcodeTmp.equalsIgnoreCase("")) { return ""; }else { if(barcodeTmp.length()<36) { String space = ""; for(int i=0;i<36-barcodeTmp.length();i++) { space = space + " "; } barcodeRet = barcodeTmp + space; } } return barcodeRet; } public static void scanBarcode() { if(work_status == 1){ JOptionPane.showMessageDialog(mesClientFrame,"工作中,勿扫码","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } String scanBarcodeTitle = ""; switch(scan_type) { case 1: product_sn.setText(""); scanBarcodeTitle = "请扫工件码"; break; } //弹窗扫工件码 String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { //工位号 String gw = ""; switch(scan_type) { case 1: product_sn.setText(scanBarcode); break; } //刷新界面 mesClientFrame.repaint(); // if(!tcp_connect_flag) { // MesClient.setMenuStatus("设备未连接Mes服务器",-1); //// JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE); // return; // } MesClient.f_scan_data_bt_1.setEnabled(false); MesClient.startRepairButton(); MesClient.setMenuStatus("请选择正常检查或返修检查", 0); }else { MesClient.setMenuStatus("请扫工件码,请重试",-1); // JOptionPane.showMessageDialog(mesClientFrame,"请扫工件码","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } private static void startInspection(int repairMode) { String barcode = product_sn.getText(); if (barcode == null || barcode.trim().isEmpty()) { setMenuStatus("请先扫工件码", -1); return; } getUser(); JSONObject retObj = DataUtil.pcCheck(barcode, user20, repairMode); if (retObj == null || retObj.get("result") == null) { setMenuStatus("请求失败,请重试", -1); return; } if (!retObj.get("result").toString().equalsIgnoreCase("true")) { setMenuStatus(retObj.getString("message"), -1); return; } isRepair = repairMode; btnNormal.setBackground(repairMode == 0 ? Color.GREEN : null); btnRepair.setBackground(repairMode == 1 ? Color.GREEN : null); fxlabel.setVisible(repairMode == 1); check_quality_result = true; work_status = 1; finish_ok_bt.setEnabled(true); finish_ng_bt.setEnabled(true); startWeldButton(); closeRepairButton(); setMenuStatus("该工件可以加工", 0); } public static void logoff() { welcomeWin.setVisible(true); mesClientFrame.setVisible(false); // nettyClient = null; // if(heartBeatTimer!=null) { // heartBeatTimer.cancel(); // } // if(heartBeatIconTimer!=null) { // heartBeatIconTimer.cancel(); // } // tcp_connect_flag = false; // connect_request_flag = false; } public MesClient() { setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png"))); setTitle("MES系统客户端:"+mes_gw + "- " + mes_gw_des); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0, 0, 1024, 768); JMenuBar menuBar = new JMenuBar(); menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 26)); setJMenuBar(menuBar); // 左侧:工位号和名称 JLabel gwLabel = new JLabel("工位:" + mes_gw + " - " + mes_gw_des); gwLabel.setFont(new Font("微软雅黑", Font.BOLD, 22)); gwLabel.setForeground(new Color(0, 102, 204)); gwLabel.setBorder(new EmptyBorder(0, 10, 0, 20)); menuBar.add(gwLabel); // 添加弹簧组件,将后续组件推到右侧 menuBar.add(Box.createHorizontalGlue()); // 右侧:设置菜单 JMenu settingMenu = new JMenu("设置"); //settingMenu.setVisible(false); settingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png"))); settingMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); menuBar.add(settingMenu); JMenuItem resetTcpMenu = new JMenuItem("\u91CD\u8FDEMES"); resetTcpMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); resetTcpMenu.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mouseClicked(e); //重连mes // nettyClient.future.channel().close(); } }); settingMenu.add(resetTcpMenu); JMenuItem resetTcpMenu_1 = new JMenuItem("刷新工件"); resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20)); resetTcpMenu_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mouseClicked(e); resetScanA(); } }); settingMenu.add(resetTcpMenu_1); // 右侧:用户菜单 JMenu fileMenu = new JMenu("用户"); fileMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png"))); fileMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); menuBar.add(fileMenu); JMenuItem exitMenuItem = new JMenuItem("退出"); exitMenuItem.setIcon(new ImageIcon(MesClient.class.getResource("/bg/logoff.png"))); exitMenuItem.setFont(new Font("微软雅黑", Font.PLAIN, 22)); fileMenu.add(exitMenuItem); exitMenuItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) {//只能检测到mousePressed事件 super.mouseClicked(e); //dispose(); // logoff(); // 调用带密码验证的退出功能 confirmLogout(); } }); resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/reset_logo.png"))); resetTcpMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 20)); resetTcpMenu_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mouseClicked(e); resetScanA(); } }); settingMenu.add(resetTcpMenu_1); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JToolBar toolBar = new JToolBar(); contentPane.add(toolBar, BorderLayout.NORTH); JLabel equipment_statu_label = new JLabel("状态:"); equipment_statu_label.setHorizontalAlignment(SwingConstants.CENTER); equipment_statu_label.setForeground(Color.BLACK); equipment_statu_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); equipment_statu_label.setBackground(Color.LIGHT_GRAY); toolBar.add(equipment_statu_label); status_menu = new JButton("设备未连接MES服务器"); status_menu.setText("已连接MES服务器"); status_menu.setForeground(Color.GREEN); status_menu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); status_menu.setForeground(Color.GREEN); status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); status_menu.setBackground(Color.BLACK); toolBar.add(status_menu); JLabel space_1 = new JLabel(" "); toolBar.add(space_1); JLabel heart_beat_status_label = new JLabel("心跳:"); heart_beat_status_label.setHorizontalAlignment(SwingConstants.CENTER); heart_beat_status_label.setForeground(Color.BLACK); heart_beat_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); heart_beat_status_label.setBackground(Color.LIGHT_GRAY); toolBar.add(heart_beat_status_label); heart_beat_menu = new JButton("2024-02-20 23:20:10"); heart_beat_menu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"))); heart_beat_menu.setForeground(Color.GREEN); heart_beat_menu.setFont(new Font("微软雅黑", Font.PLAIN, 20)); heart_beat_menu.setBackground(Color.BLACK); toolBar.add(heart_beat_menu); JLabel space_2 = new JLabel(" "); toolBar.add(space_2); JLabel user_status_label = new JLabel("登录用户:"); user_status_label.setHorizontalAlignment(SwingConstants.CENTER); user_status_label.setForeground(Color.BLACK); user_status_label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); user_status_label.setBackground(Color.LIGHT_GRAY); toolBar.add(user_status_label); user_menu = new JButton("JinJuShi"); user_menu.setForeground(Color.GREEN); user_menu.setFont(new Font("微软雅黑", Font.PLAIN, 22)); user_menu.setBackground(Color.BLACK); toolBar.add(user_menu); JLabel space_3 = new JLabel(" "); toolBar.add(space_3); JLabel space_4 = new JLabel(" "); toolBar.add(space_4); tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setMinimumSize(new Dimension(400, 50)); tabbedPane.setFont(new Font("宋体", Font.BOLD, 15)); contentPane.add(tabbedPane); // ==================== 工作面板 1 (Index 0) ==================== JPanel indexPanelA = new JPanel(); indexScrollPaneA = new JScrollPane(indexPanelA); indexPanelA.setLayout(new BorderLayout(10, 10)); JPanel topContainer = new JPanel(); topContainer.setLayout(new BoxLayout(topContainer, BoxLayout.Y_AXIS)); // 1. 设置加工模式区域 (原本的第二行,现在先加,排在最上面) JPanel modeRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 10)); // 增加了垂直间距 modeRow.setBorder(new EmptyBorder(10, 0, 0, 0)); // 顶部留一点边距,避免顶格 btnNormal = new JButton("正常检查"); btnNormal.setFont(new Font("微软雅黑", Font.BOLD, 24)); btnNormal.setPreferredSize(new Dimension(220, 60)); btnNormal.setEnabled(false); btnNormal.setBackground(Color.GREEN); //默认为绿色 btnRepair = new JButton("返修检查"); btnRepair.setFont(new Font("微软雅黑", Font.BOLD, 24)); btnRepair.setPreferredSize(new Dimension(220, 60)); btnRepair.setEnabled(false); btnNormal.addActionListener(e -> { startInspection(0); }); btnRepair.addActionListener(e -> { startInspection(1); }); modeRow.add(btnNormal); modeRow.add(btnRepair); // 2. 设置扫码区域 (原本的第一行,现在后加,排在下面) JPanel scanRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20)); product_sn = new JTextField(30); product_sn.setHorizontalAlignment(SwingConstants.CENTER); product_sn.setEditable(false); product_sn.setFont(new Font("微软雅黑", Font.PLAIN, 28)); product_sn.setPreferredSize(new Dimension(600, 70)); scanRow.add(product_sn); f_scan_data_bt_1 = new JButton("扫码"); f_scan_data_bt_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scan_type = 1; scanBarcode(); } }); f_scan_data_bt_1.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png"))); f_scan_data_bt_1.setFont(new Font("微软雅黑", Font.PLAIN, 32)); f_scan_data_bt_1.setPreferredSize(new Dimension(198, 70)); scanRow.add(f_scan_data_bt_1); topContainer.add(modeRow); // 按钮出现在最上方 topContainer.add(scanRow); // 扫码框出现在下方 indexPanelA.add(topContainer, BorderLayout.NORTH); // 中间面板:漏点按钮和返修标签 JPanel centerPanel1 = new JPanel(); centerPanel1.setLayout(new BorderLayout(10, 10)); centerPanel1.setBorder(new EmptyBorder(10, 20, 10, 20)); // 3. 漏点按钮 1-58 (数组下标 0-57) JPanel weldPanel1 = new JPanel(); weldPanel1.setLayout(new GridLayout(8, 8, 10, 10)); // 8行8列 weldPanel1.setOpaque(false); addWeldButtonsToPanel(weldPanel1, 0, 58); // 调用辅助方法 centerPanel1.add(weldPanel1, BorderLayout.CENTER); // 4. 返修标签 fxlabel = new JLabel("该工件为返修件,请仔细检查"); fxlabel.setFont(new Font("微软雅黑", Font.PLAIN, 38)); fxlabel.setForeground(Color.RED); fxlabel.setHorizontalAlignment(SwingConstants.CENTER); fxlabel.setPreferredSize(new Dimension(810, 70)); fxlabel.setVisible(false); centerPanel1.add(fxlabel, BorderLayout.SOUTH); indexPanelA.add(centerPanel1, BorderLayout.CENTER); // 底部面板:下一步按钮 JPanel bottomPanel1 = new JPanel(); bottomPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); bottomPanel1.setBorder(new EmptyBorder(10, 10, 20, 10)); // 5. 下一步按钮 (跳转到面板2 - Tab Index 1) nextBtn1 = new JButton("下一步"); nextBtn1.setFont(new Font("微软雅黑", Font.PLAIN, 24)); nextBtn1.setPreferredSize(new Dimension(200, 60)); nextBtn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tabbedPane.setSelectedIndex(1); // 跳转到第二个标签页 } }); bottomPanel1.add(nextBtn1); indexPanelA.add(bottomPanel1, BorderLayout.SOUTH); // 添加面板1到Tab tabbedPane.addTab("工作面板1-58", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null); // ==================== 工作面板 2 (Index 1) ==================== JPanel indexPanelA2 = new JPanel(); JScrollPane indexScrollPaneA2 = new JScrollPane(indexPanelA2); indexPanelA2.setLayout(new BorderLayout(10, 10)); // 1. 漏点按钮 59-116 (数组下标 58-115) JPanel weldPanel2 = new JPanel(); weldPanel2.setLayout(new GridLayout(8, 8, 10, 10)); weldPanel2.setOpaque(false); weldPanel2.setBorder(new EmptyBorder(20, 20, 20, 20)); addWeldButtonsToPanel(weldPanel2, 58, 116); indexPanelA2.add(weldPanel2, BorderLayout.CENTER); // 2. 底部按钮面板 JPanel bottomPanel2 = new JPanel(); bottomPanel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); bottomPanel2.setBorder(new EmptyBorder(10, 10, 20, 10)); // 上一步按钮 (跳转到面板1 - Tab Index 0) prevBtn2 = new JButton("上一步"); prevBtn2.setFont(new Font("微软雅黑", Font.PLAIN, 24)); prevBtn2.setPreferredSize(new Dimension(200, 60)); prevBtn2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tabbedPane.setSelectedIndex(0); } }); bottomPanel2.add(prevBtn2); // OK 按钮 finish_ok_bt = new JButton("OK"); finish_ok_bt.setEnabled(false); finish_ok_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ok_bg.png"))); finish_ok_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32)); finish_ok_bt.setPreferredSize(new Dimension(240, 80)); finish_ok_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(work_status == 1 && check_quality_result){ String sn = product_sn.getText(); if(sn == null || sn.trim().isEmpty()){ MesClient.setMenuStatus("工件码为空,请重试",-1); return; } if(!getWeldTrueIndexes().isEmpty()){ MesClient.setMenuStatus("提交漏点,请点NG",-1); return; } getUser(); String result = "OK"; JSONObject retObj = DataUtil.pcResult(sn,user20,"",result,isRepair); if (retObj != null && retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.resetScanA(); MesClient.setMenuStatus("结果上传成功,请扫下一件", 0); MesClient.scan_type = 1; MesClient.scanBarcode(); } else { MesClient.setMenuStatus("结果上传失败,请重试", -1); return; } } } }); bottomPanel2.add(finish_ok_bt); // NG 按钮 finish_ng_bt = new JButton("NG"); finish_ng_bt.setEnabled(false); finish_ng_bt.setIcon(new ImageIcon(MesClient.class.getResource("/bg/ng_bg.png"))); finish_ng_bt.setFont(new Font("微软雅黑", Font.PLAIN, 32)); finish_ng_bt.setPreferredSize(new Dimension(240, 80)); finish_ng_bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(work_status == 1 && check_quality_result){ String sn = product_sn.getText(); if(sn == null || sn.trim().isEmpty()){ MesClient.setMenuStatus("工件码为空,请重试",-1); return; } if(getWeldTrueIndexes().isEmpty()){ MesClient.setMenuStatus("请选择漏点,进行NG提交",-1); return; } getUser(); String result = "NG"; JSONObject retObj = DataUtil.pcResult(sn,user20,getWeldTrueIndexes(),result,isRepair); if (retObj != null && retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.resetScanA(); MesClient.setMenuStatus("结果上传成功,请扫下一件", 0); MesClient.scan_type = 1; MesClient.scanBarcode(); } else { MesClient.setMenuStatus("结果上传失败,请重试", -1); return; } } } }); bottomPanel2.add(finish_ng_bt); indexPanelA2.add(bottomPanel2, BorderLayout.SOUTH); // 添加面板2到Tab tabbedPane.addTab("工作面板59-116", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA2, null); tabbedPane.setEnabledAt(0, true); tabbedPane.setEnabledAt(1, true); // searchScrollPane = new JScrollPane((Component) null); indexPanelC = new JPanel(); searchScrollPaneDj = new JScrollPane(indexPanelC); indexPanelC.setLayout(null); // tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null); // ========== 工作记录 Tab(改造:当前工位Tab + 所有工位Tab) ========== // 旧代码已注释: // indexPanelB = new JPanel(); // searchScrollPane = new JScrollPane(indexPanelB); // indexPanelB.setLayout(null); // tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null); // 创建工作记录的子 TabbedPane JTabbedPane workRecordTabbedPane = new JTabbedPane(JTabbedPane.TOP); workRecordTabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // OP80 工位:当前工位 + 所有工位 WorkRecordPanel[] workRecordPanels = new WorkRecordPanel[2]; // 当前工位Tab(使用配置文件中的 mes.gw) workRecordPanels[0] = new WorkRecordPanel(mes_gw, mes_line_sn); workRecordTabbedPane.addTab(mes_gw, null, workRecordPanels[0], "查询" + mes_gw + "的工作记录"); // 所有工位Tab(查询整条产线所有工位) workRecordPanels[1] = new WorkRecordPanel(null, mes_line_sn); workRecordTabbedPane.addTab("所有工位", null, workRecordPanels[1], "查询整条产线所有工位的工作记录"); // 切换子Tab时自动刷新数据 workRecordTabbedPane.addChangeListener(e -> { int selectedIdx = workRecordTabbedPane.getSelectedIndex(); if (selectedIdx >= 0 && selectedIdx < workRecordPanels.length) { workRecordPanels[selectedIdx].refreshData(); } }); // 将子 TabbedPane 添加到主 TabbedPane tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), workRecordTabbedPane, null); tabbedPane.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); int selectedIndex = tabbedPane.getSelectedIndex(); System.out.println("selectedIndex:"+selectedIndex); if(selectedIndex == 1){ } } }); ImagePanel hfPanel = new ImagePanel("/bg/zl.png"); // 设置背景色(可选,比如设置为白色或灰色背景) hfPanel.setBackground(Color.WHITE); // 添加到 TabbedPane // 第二个参数是图标,这里复用了 menu_data_preprocess.png,您也可以换成其他图标 tabbedPane.addTab("查看漏点", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), hfPanel, "查看漏点图片"); } public static void setMenuStatus(String msg,int error){ if(error == 0){ MesClient.status_menu.setForeground(Color.GREEN); }else{ MesClient.status_menu.setForeground(Color.RED); } MesClient.status_menu.setText(msg); } public static void getMaterailData(){ try{ JSONObject retObj = DataUtil.getBindMaterail(); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { java.util.List arrs = retObj.getList("data",BindMaterialResp.class); int i = 0; rowData = new Object[arrs.size()][7]; for (BindMaterialResp bindMaterialResp:arrs){ rowData[i][0] = bindMaterialResp.getMaterialTitle(); rowData[i][1] = bindMaterialResp.getBatchSn(); rowData[i][2] = bindMaterialResp.getLastTimes(); rowData[i][3] = ""; rowData[i][4] = bindMaterialResp.getCraft(); rowData[i][5] = bindMaterialResp.getMaterialId(); rowData[i][6] = bindMaterialResp.getType(); i++; } bindBatchPanel(); } }catch (Exception e){ log.info(e.getMessage()); } } public static void updateMaterailData(){ try{ JSONObject retObj = DataUtil.getBindMaterail(); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { List arrs = retObj.getList("data",BindMaterialResp.class); int i = 0; for (BindMaterialResp bindMaterialResp:arrs){ rowData[i][0] = bindMaterialResp.getMaterialTitle(); rowData[i][1] = bindMaterialResp.getBatchSn(); rowData[i][2] = bindMaterialResp.getLastTimes(); rowData[i][3] = ""; rowData[i][4] = bindMaterialResp.getCraft(); rowData[i][5] = bindMaterialResp.getMaterialId(); rowData[i][6] = bindMaterialResp.getType(); i++; } MesClient.table.repaint(); } }catch (Exception e){ log.info(e.getMessage()); } } // 绑定物料批次码 public static void scanBatchSn(BindMaterialResp bindMaterialResp) { //弹窗扫工件码 String scanBarcodeTitle = "请扫物料:"+bindMaterialResp.getMaterialTitle(); String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle); if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) { JSONObject retObj = DataUtil.saveBindMaterail(scanBarcode,bindMaterialResp.getCraft(),bindMaterialResp.getMaterialId(),bindMaterialResp.getType()); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { MesClient.setMenuStatus("扫物料:"+bindMaterialResp.getMaterialTitle()+"成功",0); updateMaterailData(); }else{ if(retObj.get("result")==null){ MesClient.setMenuStatus("请求失败,请重试",-1); }else{ if(retObj.get("result").toString().equalsIgnoreCase("false")){ MesClient.setMenuStatus(retObj.getString("message"),-1); } } } } } public static void bindBatchPanel(){ JPanel indexPanelBB = new JPanel(); JPanel panel = new JPanel(); panel.setBounds(0, 0, 990, 550); // panel.setBounds(81, 50, 810, 479); indexPanelBB.add(panel); panel.setLayout(new GridLayout(0, 1, 0, 0)); table = new JTable(rowData, columnNames){ public boolean isCellEditable(int row, int column) { if(column == 3){ return true; } return false; } }; table.setRowHeight(40); table.setEnabled(true); table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButton()); table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButton()); JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); JScrollPane indexScrollPaneB = new JScrollPane(indexPanelBB); indexPanelBB.setLayout(null); tabbedPane.addTab("绑定物料", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexScrollPaneB, null); } // 开启发送结果定时任务 public static void startUpdateQualityTimer(){ // 创建异步线程池 ExecutorService executorService = Executors.newSingleThreadExecutor(); // 提交任务 executorService.submit(() -> { log.info("异步任务执行中..."); // 模拟耗时操作 try { while (true){ try { // 确保连接已经打开 if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) { JdbcUtils.openConnection(); } Statement statement = JdbcUtils.conn.createStatement(); String selectSql = "SELECT id, bw FROM submit_record\n" + "WHERE state = 0 ORDER BY id DESC LIMIT 10"; String updateSql = "UPDATE submit_record \n" + "SET state = '1'\n" + "WHERE id = "; ResultSet rs = statement.executeQuery(selectSql); while(rs.next()){ int id = rs.getInt("id"); String bw = rs.getString("bw"); // 发送请求 String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/updateQualityByTiming"; log.info("质量:"+bw); String s = HttpUtils.sendPostRequestJson(url, bw ); log.info("结果:"+s); Boolean result = JSONObject.parseObject(s).getBoolean("result"); if(result) { // 更改状态为 1 statement.executeUpdate(updateSql + id); } } rs.close(); statement.close(); } catch (Exception e) { log.info(e.getMessage()); } try { Thread.sleep(2000); }catch (Exception e){ log.info(e.getMessage()); } } } catch (Exception e) { log.info(e.getMessage()); } log.info("异步任务执行完毕"); }); } // 检测漏点是否全部点击完成 public static void checkWeldCompletion() { boolean allClicked = true; for (boolean state : weldClickStates) { if (!state) { allClicked = false; break; } } } //开启漏点按钮 public static void startWeldButton(){ for (int i = 0; i < weldButtons.length; i++) { weldButtons[i].setEnabled(true); weldClickStates[i] = false; // 重置状态 } if(nextBtn1 != null) nextBtn1.setEnabled(true); if(prevBtn2 != null) prevBtn2.setEnabled(true); } // 关闭漏点按钮和下一步,上一步按钮 public static void closeWeldButton(){ for (int i = 0; i < weldButtons.length; i++) { weldClickStates[i] = false; // 重置状态 weldButtons[i].setBackground(null); // 重置颜色 weldButtons[i].setEnabled(false); } // 关闭下一步,上一步按钮 if(nextBtn1 != null) nextBtn1.setEnabled(false); if(prevBtn2 != null) prevBtn2.setEnabled(false); } //获取按钮下标 public static String getWeldTrueIndexes() { StringBuilder sb = new StringBuilder(); boolean first = true; for (int i = 0; i < weldClickStates.length; i++) { // 直接判断布尔值 if (weldClickStates[i]) { if (!first) { sb.append(","); } sb.append(i+1); first = false; } } return sb.toString(); } // 自定义面板,用于同比例缩放显示图片 public class ImagePanel extends JPanel { private Image image; public ImagePanel(String imagePath) { // 加载图片资源 try { ImageIcon icon = new ImageIcon(MesClient.class.getResource(imagePath)); this.image = icon.getImage(); } catch (Exception e) { log.error("加载图片失败: " + imagePath); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { // 获取面板的宽高 int panelWidth = getWidth(); int panelHeight = getHeight(); // 获取图片的原始宽高 int imageWidth = image.getWidth(this); int imageHeight = image.getHeight(this); if (imageWidth > 0 && imageHeight > 0) { // 计算缩放比例,保持长宽比 double widthRatio = (double) panelWidth / imageWidth; double heightRatio = (double) panelHeight / imageHeight; double ratio = Math.min(widthRatio, heightRatio); // 计算新的绘制宽高 int newWidth = (int) (imageWidth * ratio); int newHeight = (int) (imageHeight * ratio); // 计算居中位置 int x = (panelWidth - newWidth) / 2; int y = (panelHeight - newHeight) / 2; // 绘制缩放后的图片 g.drawImage(image, x, y, newWidth, newHeight, this); } } } } /** * 辅助方法:批量添加漏点按钮到指定面板 * @param panel 目标面板 * @param start 按钮起始索引(包含) * @param end 按钮结束索引(不包含) */ private void addWeldButtonsToPanel(JPanel panel, int start, int end) { for (int i = start; i < end; i++) { // 防止数组越界 (安全检查) if (i >= weldButtons.length) break; int index = i; // 供内部类使用 weldButtons[i] = new JButton("漏点" + (i + 1)); weldButtons[i].setFont(new Font("微软雅黑", Font.PLAIN, 13)); weldButtons[i].setEnabled(false); // 初始时禁用 // 添加点击事件 weldButtons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton btn = (JButton) e.getSource(); // 判断是否选中 if (weldClickStates[index]) { // 取消选中 btn.setBackground(null); weldClickStates[index] = false; log.info("已取消按钮: 漏点" + (index + 1)); } else { // 选中 btn.setBackground(Color.GREEN); weldClickStates[index] = true; log.info("已选中按钮: 漏点" + (index + 1)); } } }); panel.add(weldButtons[i]); } } /** * 密码验证后退出系统 */ private static void confirmLogout() { // 弹出密码输入对话框 JPasswordField pwdField = new JPasswordField(); Object[] message = { "请输入退出密码:", pwdField }; pwdField.setFocusable(true); // 关键:添加层级监听器,监听密码框是否被加入对话框窗口 pwdField.addHierarchyListener(new HierarchyListener() { @Override public void hierarchyChanged(HierarchyEvent evt) { // 检测:窗口已显示 + 组件可显示 + 焦点未被获取 if ((evt.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && pwdField.isShowing()) { // 强制抢焦点(优先级最高) SwingUtilities.invokeLater(() -> { pwdField.requestFocus(); pwdField.requestFocusInWindow(); pwdField.grabFocus(); pwdField.setCaretPosition(0); // 可选:禁用焦点遍历(防止焦点跳回按钮) pwdField.setFocusTraversalPolicyProvider(true); }); } } }); int option = JOptionPane.showConfirmDialog( mesClientFrame, message, "退出确认", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ); // 如果用户点击了确定按钮 if (option == JOptionPane.OK_OPTION) { String inputPassword = new String(pwdField.getPassword()); // 验证密码 if (validatePassword(inputPassword)) { // 密码正确,退出系统 // 执行清理操作 cleanupBeforeExit(); // 退出应用程序 System.exit(0); } else { // 密码错误,显示错误提示 JOptionPane.showMessageDialog( mesClientFrame, "密码错误,退出操作被拒绝", "错误", JOptionPane.ERROR_MESSAGE ); } } } /** * 验证用户输入的密码 * @param inputPassword 用户输入的密码 * @return 验证结果 */ private static boolean validatePassword(String inputPassword) { // 固定退出密码为503833 return "503833".equals(inputPassword); } /** * 退出前的清理操作 */ private static void cleanupBeforeExit() { // 关闭TCP连接 if(nettyClient != null) { // nettyClient.close(); // 根据NettyClient的关闭方法调整 } // 停止定时器 if(MesClient.heartBeatTimer != null) { MesClient.heartBeatTimer.cancel(); } if(MesClient.heartBeatIconTimer != null) { MesClient.heartBeatIconTimer.cancel(); } // 关闭数据库连接 try { if(JdbcUtils.conn != null && !JdbcUtils.conn.isClosed()) { JdbcUtils.conn.close(); } } catch (Exception e) { log.error("关闭数据库连接失败", e); } } //开启加工返修按钮 public static void startRepairButton() { MesClient.btnNormal.setEnabled(true); MesClient.btnRepair.setEnabled(true); } //重置加工返修按钮 public static void closeRepairButton() { MesClient.btnNormal.setEnabled(false); MesClient.btnRepair.setEnabled(false); } }