package com.xindao.ocr.swingui.swing; import com.xindao.ocr.swingui.service.OcrService; import com.xindao.ocr.swingui.swing.jpanel.ContractNumberProcessPanel; import com.xindao.ocr.swingui.swing.jpanel.MultipleAreaProcessPanel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import javax.annotation.PostConstruct; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; @org.springframework.stereotype.Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 设置为原型作用域,每次获取创建新实例 public class FileProcessorApp extends JFrame { @Autowired private OcrService ocrService; private static final Font DEFAULT_FONT; // 颜色定义 private static final Color BACKGROUND_COLOR = new Color(245, 245, 247); private static final Color TEXT_COLOR = new Color(51, 51, 51); private static final Color PRIMARY_COLOR = new Color(66, 133, 244); private static final Color TEXT_LIGHT = new Color(102, 102, 102); static { // 字体设置 if (isFontAvailable("Microsoft YaHei")) { DEFAULT_FONT = new Font("Microsoft YaHei", Font.PLAIN, 12); } else if (isFontAvailable("SimHei")) { DEFAULT_FONT = new Font("SimHei", Font.PLAIN, 12); } else if (isFontAvailable("WenQuanYi Micro Hei")) { DEFAULT_FONT = new Font("WenQuanYi Micro Hei", Font.PLAIN, 12); } else { DEFAULT_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 12); } } private static boolean isFontAvailable(String fontName) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = ge.getAvailableFontFamilyNames(); for (String name : fontNames) { if (name.equals(fontName)) { return true; } } return false; } public FileProcessorApp() { setTitle("OCR图像处理工具"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(850, 700); setLocationRelativeTo(null); setResizable(true); // 设置全局字体和背景 setFont(DEFAULT_FONT); getContentPane().setBackground(BACKGROUND_COLOR); } /** * 使用@PostConstruct确保依赖注入完成后再初始化UI * 避免构造函数中直接使用@Autowired的字段导致null值 */ @PostConstruct public void initialize() { initUI(); setVisible(true); } private void initUI() { // 主面板使用边界布局 JPanel mainPanel = new JPanel(new BorderLayout(15, 15)); mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15)); mainPanel.setBackground(BACKGROUND_COLOR); // 创建标签页面板 JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setFont(DEFAULT_FONT); JPanel mainTab = new ContractNumberProcessPanel(BACKGROUND_COLOR, PRIMARY_COLOR, TEXT_COLOR,TEXT_LIGHT,DEFAULT_FONT,this,ocrService).initPanel(); JPanel extensionPanel = new MultipleAreaProcessPanel(this,ocrService,BACKGROUND_COLOR, PRIMARY_COLOR, TEXT_COLOR, DEFAULT_FONT).initPanel(); // 添加标签页到标签面板 tabbedPane.addTab("文件处理", null, mainTab, ""); tabbedPane.addTab("多区域识别", null, extensionPanel, ""); // 添加标签面板到主面板 mainPanel.add(tabbedPane, BorderLayout.CENTER); // 设置内容面板 setContentPane(mainPanel); } }