package org.example; import org.example.cmd.COMThread; import org.example.opc.GlobalVariable; import org.example.opc.OPCComm; import org.example.opc.unit.DateTool; import org.example.server.ServerInterface; import org.example.server.impl.ServerInterfaceImpl; import javax.swing.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MainTest { private static OPCComm opc = OPCComm.getInstance(); static COMThread t = new COMThread(); public static void main(String[] args) throws Exception { String ecpStr = "";//异常记录标记 try{ GuiModel swingFile = new GuiModel(); opc.init();//初始化OPC GlobalVariable.textArea.setText(DateTool.getLocalTimeForDate()+": "+"系统开始运行!"); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); cachedThreadPool.execute(new TaskThreadPool()); t.start(); }catch (Exception e) { ecpStr ="出现异常1:" + e.getMessage(); }finally { GlobalVariable.textArea.append(ecpStr+"\r\n"); } } } class GuiModel{ String ecpStr = "";//异常记录标记 public GuiModel() { try{ JPanel panel = new JPanel(); JLabel label = new JLabel("系统日志"); panel.add(label); GlobalVariable.textArea = new JTextArea(30,70); JScrollPane scrollPane = new JScrollPane(); //设置水平和垂直滚动条自动出现 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); //设置水平和垂直滚动条总是出现 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setViewportView(GlobalVariable.textArea); //panel.add(GlobalVariable.textArea); JFrame jFrame = new JFrame("噪音仪设备(江宸德玛)"); jFrame.setContentPane(panel); jFrame.setSize(800,600); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jFrame.add(scrollPane); jFrame.setVisible(true); }catch (Exception e) { ecpStr ="出现异常1:" + e.getMessage(); }finally { GlobalVariable.textArea.append(ecpStr+"\r\n"); } } } class TaskThreadPool implements Runnable { private ServerInterface server; public void run() { try { while (true) { server = new ServerInterfaceImpl(); server.monitor(); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); } } }