springboot mybatis-plus swing实现报警监听

发布时间:2024年01月17日

通过声音控制报警器,实现声光报警,使用beautyeye_lnf.jar美化界面如下
在这里插入图片描述
在这里插入图片描述

@EnableTransactionManagement(proxyTargetClass = true)
@SpringBootApplication
@EnableScheduling
public class AlarmWarnApplication {

    public static void main(String[] args) {
        try {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencyAppleLike;
            UIManager.put("RootPane.setupButtonVisible", false);
        } catch(Exception e) {
            //TODO exception
        }
        new SpringApplicationBuilder(AlarmWarnApplication.class).headless(false).run(args);
        //显示界面
        ViewStart.run();
    }

}
public class ViewStart {
    public static void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SpringContextUtils.getBean(SwingArea.class).setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
@Component("SwingArea")
@Scope("prototype") //创建多例
@SuppressWarnings("all")
public class SwingArea extends JFrame {
    private ImageIcon imageIcon;
    private URL url;
    private JLabel imageLabel;
    private JLabel label;
    private JButton openBtn;
    private Timer timer;
    private AudioPlay audioPlay;

    @Autowired
    private DevicealarmMapper devicealarmMapper;

    public SwingArea() {
    	//报警要加载的音乐
        InputStream inputStream = getClass().getResourceAsStream("/music/music.wav");
        System.out.println(inputStream);
        audioPlay = new AudioPlay(inputStream);
        setTitle("报警监听程序");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 处理关闭事件,例如显示确认对话框
                int option = JOptionPane.showConfirmDialog(SwingArea.this, "确定要关闭报警监听吗?", "提示", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    // 用户确认关闭,执行关闭操作
                    dispose();
                }
            }
        });
        setResizable(false);
        setLayout(null);
        //尺寸
        setSize(800, 500);
		//背景图片
        ((JPanel)this.getContentPane()).setOpaque(false);
        url = this.getClass().getResource("/music/green.png");
        imageIcon = new ImageIcon(url); //添加图片
        imageLabel = new  JLabel(imageIcon);
        imageLabel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
        getLayeredPane().add(imageLabel, new Integer(Integer.MIN_VALUE));


        label = new JLabel("无报警");
        label.setFont(new Font(null, Font.BOLD, 30));
        label.setForeground(new Color(91, 182, 91));
        label.setBounds(120, -60, 500, 200);
        label.setHorizontalAlignment(JLabel.CENTER);
        add(label);

        openBtn = new JButton("消音");
        openBtn.setBounds(295,320,144,60);
        openBtn.setBackground(new Color(255,255,255));
        openBtn.setFont(new Font("宋体", Font.BOLD,28));
        openBtn.setForeground(Color.red);//字体颜色
        openBtn.setRolloverEnabled(true);
        //更改鼠标移入按钮背景色一直不起作用
//        openBtn.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseEntered(MouseEvent e) {
//                // 鼠标进入时设置悬浮颜色
//                openBtn.setBackground(new Color(255,219,213 ));
//            }
//            @Override
//            public void mouseExited(MouseEvent e) {
//                // 鼠标离开时设置背景颜色
//                openBtn.setBackground(new Color(255,255,255));
//            }
//        });
        openBtn.setVisible(false);
        add(openBtn);

        setVisible(true);
        QueryWrapper<Devicealarm> queryWrapper = new QueryWrapper<>();
        //未消音
        queryWrapper.eq("mute", 0);
        //未处理
        queryWrapper.eq("isprocessing", 0);

        // 定时器,对报警监听
        timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                java.util.List<Devicealarm> devicealarms = devicealarmMapper.selectList(queryWrapper);
                if(devicealarms.size() > 0){
                    //动态更改背景图重点,要先进行remove
                    getLayeredPane().remove(imageLabel);
                    label.setBounds(120, -30, 500, 200);
                    label.setFont(new Font(null, Font.BOLD, 28));
                    url = this.getClass().getResource("/music/alarm.gif");
                    imageIcon = new ImageIcon(url); //添加图片
                    imageLabel = new  JLabel(imageIcon);
                    imageLabel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
                    getLayeredPane().add(imageLabel, new Integer(Integer.MIN_VALUE));

                    openBtn.setVisible(true);
                    setVisible(true);
                    setExtendedState(JFrame.NORMAL);
                    toFront();
                    java.util.List<String> messageArr = new ArrayList<>();
                    java.util.List<String> idArr = new ArrayList<>();
                    for(int i = 0; i < devicealarms.size(); i++){
                        messageArr.add(devicealarms.get(i).getName() + "报警,浓度" + devicealarms.get(i).getValue() + "%LEL");
                        idArr.add(devicealarms.get(i).getId());
                    }
                    String message = "<html>" + String.join("<br/>", messageArr) + "</html>";
                    label.setText(message);
                    label.setForeground(Color.RED);
                    audioPlay.start();
                    openBtn.addActionListener(it -> {
                    	//动态更改背景图重点,要先进行remove
                        getLayeredPane().remove(imageLabel);
                        url = this.getClass().getResource("/music/green.png");
                        imageIcon = new ImageIcon(url); //添加图片
                        imageLabel = new  JLabel(imageIcon);
                        imageLabel.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
                        getLayeredPane().add(imageLabel, new Integer(Integer.MIN_VALUE));
                        getLayeredPane().repaint();
                        openBtn.setVisible(false);

                        Devicealarm devicealarm = new Devicealarm();
                        devicealarm.setMute(1);
                        devicealarmMapper.update(devicealarm, new QueryWrapper<Devicealarm>().in("id", idArr));
                        audioPlay.pause();
                        label.setForeground(new Color(91, 182, 91));
                        label.setBounds(120, -60, 500, 200);
                        label.setText("<html>无报警</html>");
                    });
                }
            }
        });
        // 启动定时器
        timer.start();
    }
}

其他工具类

@Component
public class SpringContextUtils implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name 标识名
     * @param clazz 类型对象
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
@Component
public class SpringContextUtils implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name 标识名
     * @param clazz 类型对象
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
文章来源:https://blog.csdn.net/baidu_29609779/article/details/135652853
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。