zoukankan      html  css  js  c++  java
  • 时钟

    界面如下:

    截图功能:

    更改背景:

    帮助->关于软件:

    package com.b510.main;
    
    import com.b510.ui.main.MyAlarm;
    
    public class AlarmMain {
    
        public static void main(String[] args) {
            new MyAlarm("Alarm tiantian v-1.0");
        }
    }
    package com.b510.menu;
    
    public class AlarmBackground {
    
        /**
         * 默认的背景图片
         */
        private static String BACKGROUND_DEFAULT="src/image/background.png";
        /**
         * 水果系列
         */
        private static String BACKGROUND_FRUIT="src/image/background1.png";
        /**
         * 自行车系列
         */
        private static String BACKGROUND_BICK="src/image/background2.png";
        /**
         * 背景图片的list
         */
        private static String[] backgroundList={BACKGROUND_DEFAULT,BACKGROUND_FRUIT,BACKGROUND_BICK};
        /**
         * 获取背景图片的list的长度
         * @return 返回背景图片的list的长度
         */
        public static int getBackgroundListSize(){
            return backgroundList.length;
        }
        public static String getBackground(int bg){
            return backgroundList[bg];
        }
    }
    package com.b510.menu;
    
    /**
     *
     * @author tiantian
     * 菜单-文件
     */
    public class AlarmFile {
    
        /**
         * 退出程序
         */
        public static void exit(){
            System.exit(0);
        }
    }
    package com.b510.menu;
    
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    import com.b510.ui.main.MyAlarm;
    
    public class AlarmHelp extends JFrame {
    
        private static final long serialVersionUID = 5248482602468160509L;
    
        public AlarmHelp(String title) {
            super(title);
            initComponents();
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    AlarmHelp.this.setVisible(false);
            }});
        }
    
    
        private JButton closeButton = new JButton();
     
        javax.swing.JLabel appTitleLabel = new javax.swing.JLabel();
    
        javax.swing.JLabel versionLabel = new javax.swing.JLabel();
     
        javax.swing.JLabel appVersionLabel = new javax.swing.JLabel();
     
        javax.swing.JLabel homepageLabel = new javax.swing.JLabel();
        /**
         * Homepage
         */
        javax.swing.JLabel appHomepageLabel = new javax.swing.JLabel();
     
        javax.swing.JLabel appDescLabel = new javax.swing.JLabel();
     
        javax.swing.JLabel imageLabel = new javax.swing.JLabel();
    
    
        private void initComponents() {
    
            this.setVisible(true);
            // setResizable(false);
            this.setLocation(MyAlarm.pointX+60, MyAlarm.pointY+190);
            closeButton.addMouseListener(new MouseAdapter() {
                 public void mouseEntered(java.awt.event.MouseEvent evt) {
                       closeButton.setIcon(new ImageIcon("src/image/biao2.png"));
                    }
                 public void mouseExited(MouseEvent evt){
                     closeButton.setIcon(new ImageIcon("src/image/biao.png"));
                 }
            });
    
            appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(
                    appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD,
                    appTitleLabel.getFont().getSize() + 4));
            appTitleLabel.setText("闹钟啦♪(^∇^*)"); // NOI18N
            appTitleLabel.setName("appTitleLabel"); // NOI18N
    
            versionLabel.setFont(versionLabel.getFont().deriveFont(
                    versionLabel.getFont().getStyle() | java.awt.Font.BOLD));
            versionLabel.setText("版本"); // NOI18N
            versionLabel.setName("versionLabel"); // NOI18N
    
            appVersionLabel.setText("1.0"); // NOI18N
            appVersionLabel.setName("appVersionLabel"); // NOI18N
    
            homepageLabel.setFont(homepageLabel.getFont().deriveFont(
                    homepageLabel.getFont().getStyle() | java.awt.Font.BOLD));
            homepageLabel.setText("制作人:"); // NOI18N
            homepageLabel.setName("homepageLabel"); // NOI18N
    
            appHomepageLabel.setText("田甜"); // NOI18N
            appHomepageLabel.setName("appHomepageLabel"); // NOI18N
    
            appDescLabel.setText("闹钟详情"); // NOI18N
            appDescLabel.setName("appDescLabel"); // NOI18N
    
            imageLabel
                    .setIcon(new ImageIcon("src/image/about.png")); // NOI18N
            imageLabel.setName("imageLabel"); // NOI18N
    
            closeButton
                    .setIcon(new ImageIcon("src/image/biao.png"));
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                    getContentPane());
            getContentPane().setLayout(layout);
            layout
                    .setHorizontalGroup(layout
                            .createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                    layout
                                            .createSequentialGroup()
                                            .addComponent(imageLabel)
                                            .addGap(18, 18, 18)
                                            .addGroup(
                                                    layout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addGroup(
                                                                    javax.swing.GroupLayout.Alignment.LEADING,
                                                                    layout
                                                                            .createSequentialGroup()
                                                                            .addGroup(
                                                                                    layout
                                                                                            .createParallelGroup(
                                                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                                                            .addComponent(
                                                                                                    versionLabel)
                                                                                            .addComponent(
                                                                                                    homepageLabel))
                                                                            .addPreferredGap(
                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                            .addGroup(
                                                                                    layout
                                                                                            .createParallelGroup(
                                                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                                                            .addComponent(
                                                                                                    appVersionLabel)
                                                                                            .addComponent(
                                                                                                    appHomepageLabel)))
                                                            .addComponent(
                                                                    appTitleLabel,
                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addComponent(
                                                                    appDescLabel,
                                                                    javax.swing.GroupLayout.Alignment.LEADING,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                    266,
                                                                    Short.MAX_VALUE)
                                                            .addComponent(
                                                                    closeButton))
                                            .addContainerGap()));
            layout
                    .setVerticalGroup(layout
                            .createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(imageLabel,
                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    Short.MAX_VALUE)
                            .addGroup(
                                    layout
                                            .createSequentialGroup()
                                            .addContainerGap()
                                            .addComponent(appTitleLabel)
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(
                                                    appDescLabel,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(
                                                    layout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(
                                                                    versionLabel)
                                                            .addComponent(
                                                                    appVersionLabel))
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(
                                                    layout
                                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(
                                                    layout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(
                                                                    homepageLabel)
                                                            .addComponent(
                                                                    appHomepageLabel))
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                    19, Short.MAX_VALUE)
                                            .addComponent(closeButton)
                                            .addContainerGap()));
            pack();
        }
    }
    package com.b510.menu;
    
    import mai.capture.DesktopCapture;
    
    /**
     * 菜单-工具
     */
    public class AlarmTools {
    
        /** 定义截图功能 */
        @SuppressWarnings("unused")
        private static DesktopCapture deskTopCapture;
        /**
         * 截图
         */
        public static void screenshot(){
            deskTopCapture=new DesktopCapture();
        }
    }
    package com.b510.ui.down;
    
    /**
     * 主界面区-设置闹钟时间
     */
    public class AlarmSetTime {
    
    }
    package com.b510.ui.main;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.IOException;
    import java.util.Calendar;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.SourceDataLine;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    import com.b510.menu.AlarmBackground;
    import com.b510.menu.AlarmFile;
    import com.b510.menu.AlarmHelp;
    import com.b510.menu.AlarmTools;
    
    
    public class MyAlarm extends JFrame implements ActionListener {
    
        /**
         *+1
         */
        private javax.swing.JButton add1;
        /**
         * +30
         */
        private javax.swing.JButton add30;
        /**
         * +5
         */
        private javax.swing.JButton add5;
        /**
         * 时
         */
        private javax.swing.JComboBox dorp_down_hours;
        /**
         * 分
         */
        private javax.swing.JComboBox dorp_down_minute;
        /**
         * 秒
         */
        private javax.swing.JComboBox drop_down_second;
        /**
         * 试听
         */
        private javax.swing.JButton listening_test;
        /**
         * 试听-停止
         */
        private javax.swing.JButton listening_test_stop;
        /**
         * 主Panel
         */
        private javax.swing.JPanel mainPanel;
        /**
         * 菜单bar
         */
        private javax.swing.JMenuBar menuBar;
        /**
         * -1
         */
        private javax.swing.JButton minus1;
        /**
         * -30
         */
        private javax.swing.JButton minus30;
        /**
         * -5
         */
        private javax.swing.JButton minus5;
        /**
         * 当前时间
         */
        private javax.swing.JLabel now;
        /**
         * 铃声
         */
        private javax.swing.JComboBox ring_setup;
        /**
         * 截屏
         */
        private javax.swing.JMenuItem screenshot;
        /**
         * 更改背景图片
         */
        private javax.swing.JMenuItem changeBackground;
        /**
         * 停止
         */
        private javax.swing.JButton stop;
        /**
         * 工具
         */
        private javax.swing.JMenu tools;
        /**
         * 帮助
         */
        private javax.swing.JMenu help;
        /**
         * 关于
         */
        private javax.swing.JMenuItem about;
        /**
         * 退出
         */
        private javax.swing.JMenuItem exit;
        /**
         * 上传铃声
         */
        private javax.swing.JMenuItem uploadRing;
        /**
         * 文件
         */
        private javax.swing.JMenu file;
        /**
         * 结果,即剩余时间
         */
        private JLabel result;
        /**
         * 分割线
         */
        private javax.swing.JSeparator line;
        /**
         * 变量-->小时
         */
        private String h;
        /**
         * 变量-->分钟
         */
        private String m;
        /**
         * 变量-->秒
         */
        private String s;
        /**
         * 线程的一个标志
         */
        private boolean running = true;
        /**
         * 定义图盘图盘标志
         */
        public boolean iconed = false;
        /**
         * 背景图片选择标志
         */
        private int background = 0;
        /**
         * 获取result的秒数
         */
        private int secondOfResult;
        /**
         * 更改背景图片的标志
         */
        private boolean flagOfBackground = false;
        /**
         * MyAlarm的X坐标
         */
        public static int pointX = 0;
        /**
         * MyAlarm的Y坐标
         */
        public static int pointY = 0;
        /** 定义托盘 */
        MyTray myTray;
        /**
         * 版本号
         */
        private static final long serialVersionUID = -6601825053136983041L;
    
        public MyAlarm(String title) {
            this.setTitle(title);
            init();
        }
    
        /**
         * 初始化背景图片
         */
        public void initMainPanel() {
            mainPanel = new JPanel() {
                private static final long serialVersionUID = 1L;
    
                protected void paintComponent(Graphics g) {
                    ImageIcon icon = new ImageIcon(AlarmBackground
                            .getBackground(background));
                    g.drawImage(icon.getImage(), 0, 0, 545, 463, null);
                }
            };
        }
    
        /**
         * 主界面初始化
         */
        public void init() {
            initMainPanel();
            now = new javax.swing.JLabel();
            stop = new javax.swing.JButton();
            add1 = new javax.swing.JButton();
            add5 = new javax.swing.JButton();
            add30 = new javax.swing.JButton();
            minus1 = new javax.swing.JButton();
            minus5 = new javax.swing.JButton();
            minus30 = new javax.swing.JButton();
            listening_test = new javax.swing.JButton();
            listening_test_stop = new javax.swing.JButton();
            dorp_down_hours = new javax.swing.JComboBox();
            dorp_down_minute = new javax.swing.JComboBox();
            drop_down_second = new javax.swing.JComboBox();
            ring_setup = new javax.swing.JComboBox();
            menuBar = new javax.swing.JMenuBar();
            file = new javax.swing.JMenu();
            uploadRing = new javax.swing.JMenuItem();
            exit = new javax.swing.JMenuItem();
            tools = new javax.swing.JMenu();
            screenshot = new javax.swing.JMenuItem();
            changeBackground = new javax.swing.JMenuItem();
            help = new javax.swing.JMenu();
            about = new javax.swing.JMenuItem();
            line = new javax.swing.JSeparator();
            result = new javax.swing.JLabel();
    
            mainPanel.setName("mainPanel"); // NOI18N
    
            result.setForeground(Color.RED);
            result.setName("result");
            result.setVisible(false);
    
            now.setFont(now.getFont().deriveFont(
                    now.getFont().getStyle() | java.awt.Font.BOLD,
                    now.getFont().getSize() + 70));
            now.setName("now"); // NOI18N
            // 时间监听器,得到系统时间和设置好时间后,得到剩余时间
            timeListener();
    
            stop.setText("stop"); // NOI18N
            stop.setName("stop"); // NOI18N
            // 初始化的时候是不可见的
            stop.setVisible(false);
            stop.addActionListener(this);
    
            add1.setText("+1"); // NOI18N
            add1.setName("add1"); // NOI18N
            add1.addActionListener(this);
    
            add5.setText("+5"); // NOI18N
            add5.setName("add5"); // NOI18N
            add5.addActionListener(this);
    
            add30.setText("+30"); // NOI18N
            add30.setName("add30"); // NOI18N
            add30.addActionListener(this);
    
            minus1.setText("-1"); // NOI18N
            minus1.setName("minus1"); // NOI18N
            minus1.addActionListener(this);
    
            minus5.setText("-5"); // NOI18N
            minus5.setName("minus5"); // NOI18N
            minus5.addActionListener(this);
    
            minus30.setText("-30"); // NOI18N
            minus30.setName("minus30"); // NOI18N
            minus30.addActionListener(this);
    
            listening_test.setText("试听"); // NOI18N
            listening_test.setName("listening_test"); // NOI18N
            listening_test.addActionListener(this);
    
            listening_test_stop.setText("停止"); // NOI18N
            listening_test_stop.setName("listening_test_stop"); // NOI18N
            listening_test_stop.addActionListener(this);
    
            dorp_down_hours.setModel(new javax.swing.DefaultComboBoxModel(
                    new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                            "07", "08", "09", "10", "11", "12", "13", "14", "15",
                            "16", "17", "18", "19", "20", "21", "22", "23" }));
            dorp_down_hours.setName("dorp_down_hours"); // NOI18N
            dorp_down_hours.addActionListener(this);
    
            dorp_down_minute.setModel(new javax.swing.DefaultComboBoxModel(
                    new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                            "07", "08", "09", "10", "11", "12", "13", "14", "15",
                            "16", "17", "18", "19", "20", "21", "22", "23", "24",
                            "25", "26", "27", "28", "29", "30", "31", "32", "33",
                            "34", "35", "36", "37", "38", "39", "40", "41", "42",
                            "43", "44", "45", "46", "47", "48", "49", "50", "51",
                            "52", "53", "54", "55", "56", "57", "58", "59" }));
            dorp_down_minute.setName("dorp_down_minute"); // NOI18N
            dorp_down_minute.addActionListener(this);
    
            drop_down_second.setModel(new javax.swing.DefaultComboBoxModel(
                    new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                            "07", "08", "09", "10", "11", "12", "13", "14", "15",
                            "16", "17", "18", "19", "20", "21", "22", "23", "24",
                            "25", "26", "27", "28", "29", "30", "31", "32", "33",
                            "34", "35", "36", "37", "38", "39", "40", "41", "42",
                            "43", "44", "45", "46", "47", "48", "49", "50", "51",
                            "52", "53", "54", "55", "56", "57", "58", "59" }));
            drop_down_second.setName("drop_down_second"); // NOI18N
            drop_down_second.addActionListener(this);
    
            ring_setup.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
                    "铃声一", "铃声二", "铃声三", "铃声四", "铃声五", "铃声六", "铃声七" }));
            ring_setup.setName("ring_setup"); // NOI18N
            // 主要的布局
            omponentLayout();
    
            menuBar.setName("menuBar"); // NOI18N
    
            file.setText("文件"); // NOI18N
            file.setName("file"); // NOI18N
    
            uploadRing.setText("上传铃声"); // NOI18N
            uploadRing.setName("uploadRing"); // NOI18N
            uploadRing.addActionListener(this);
            file.add(uploadRing);
    
            file.add(line);
    
            exit.setText("退出");
            exit.setName("exit"); // NOI18N
            exit.addActionListener(this);
            file.add(exit);
    
            menuBar.add(file);
    
            tools.setText("工具"); // NOI18N
            tools.setName("tools"); // NOI18N
    
            screenshot.setText("截图工具"); // NOI18N
            screenshot.setName("screenshot"); // NOI18N
            screenshot.addActionListener(this);
            tools.add(screenshot);
    
            changeBackground.setText("更改背景");
            changeBackground.setName("changeBackground");
            changeBackground.addActionListener(this);
            tools.add(changeBackground);
    
            menuBar.add(tools);
    
            help.setText("帮助"); // NOI18N
            help.setName("help"); // NOI18N
    
            about.setText("关于软件");
            about.setName("about"); // NOI18N
            about.addActionListener(this);
            help.add(about);
    
            menuBar.add(help);
    
            this.add(mainPanel);
            setJMenuBar(menuBar);
    
            this.setVisible(true);
            this.setSize(550, 516);
            // this.pack();
            this.setLocationRelativeTo(null);
            this.setResizable(false);
            // this.setLocation(470, 250);
            // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // 当点击"-"最小化按钮时,系统会最小化到托盘
            addWindowListener(new WindowAdapter() {
                public void windowIconified(WindowEvent e) {
                    iconed = true;
                    setVisible(false);
                }
    
                public void windowClosing(WindowEvent e) {
                    // 当点击"X"关闭窗口按钮时,会询问用户是否要最小化到托盘
                    // 是,表示最小化到托盘,否,表示退出
                    int option = JOptionPane.showConfirmDialog(MyAlarm.this,
                            "是否最小化到托盘?", "提示:", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION) {
                        iconed = true;
                        MyAlarm.this.setVisible(false);
                    } else {
                        AlarmFile.exit();
                    }
                }
            });
            // 初始化自定义托盘
            myTray = new MyTray(MyAlarm.this);
    
        }
    
        /**
         * 组件的布局,不要轻易动啊
         */
        private void omponentLayout() {
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
                    mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout
                    .setHorizontalGroup(mainPanelLayout
                            .createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                    mainPanelLayout
                                            .createSequentialGroup()
                                            .addContainerGap(170, Short.MAX_VALUE)
                                            .addGroup(
                                                    mainPanelLayout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addGroup(
                                                                    mainPanelLayout
                                                                            .createSequentialGroup()
                                                                            .addComponent(
                                                                                    result)
                                                                            .addContainerGap())
                                                            .addGroup(
                                                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                                                    mainPanelLayout
                                                                            .createParallelGroup(
                                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                                            .addGroup(
                                                                                    mainPanelLayout
                                                                                            .createSequentialGroup()
                                                                                            .addComponent(
                                                                                                    now,
                                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                    483,
                                                                                                    Short.MAX_VALUE)
                                                                                            .addContainerGap())
                                                                            .addGroup(
                                                                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                    mainPanelLayout
                                                                                            .createSequentialGroup()
                                                                                            .addGroup(
                                                                                                    mainPanelLayout
                                                                                                            .createParallelGroup(
                                                                                                                    javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                            .addGroup(
                                                                                                                    mainPanelLayout
                                                                                                                            .createSequentialGroup()
                                                                                                                            .addComponent(
                                                                                                                                    dorp_down_hours,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                    74,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    dorp_down_minute,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                    65,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    drop_down_second,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                    62,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    stop))
                                                                                                            .addGroup(
                                                                                                                    mainPanelLayout
                                                                                                                            .createSequentialGroup()
                                                                                                                            .addComponent(
                                                                                                                                    add1)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    add5)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    add30)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    minus1)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    minus5)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    minus30))
                                                                                                            .addGroup(
                                                                                                                    mainPanelLayout
                                                                                                                            .createSequentialGroup()
                                                                                                                            .addComponent(
                                                                                                                                    ring_setup,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    listening_test)
                                                                                                                            .addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                            .addComponent(
                                                                                                                                    listening_test_stop)))
                                                                                            .addGap(
                                                                                                    73,
                                                                                                    73,
                                                                                                    73))))));
            mainPanelLayout
                    .setVerticalGroup(mainPanelLayout
                            .createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                    mainPanelLayout
                                            .createSequentialGroup()
                                            .addGap(120, 120, 120)
                                            .addComponent(result)
                                            .addGap(24, 24, 24)
                                            .addComponent(
                                                    now,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                    Short.MAX_VALUE)
                                            .addGap(36, 36, 36)
                                            .addGroup(
                                                    mainPanelLayout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(
                                                                    dorp_down_hours,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(
                                                                    dorp_down_minute,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(
                                                                    drop_down_second,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(stop))
                                            .addPreferredGap(
                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(
                                                    mainPanelLayout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(add1)
                                                            .addComponent(add5)
                                                            .addComponent(add30)
                                                            .addComponent(minus1)
                                                            .addComponent(minus5)
                                                            .addComponent(minus30))
                                            .addGap(13, 13, 13)
                                            .addGroup(
                                                    mainPanelLayout
                                                            .createParallelGroup(
                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(
                                                                    ring_setup,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(
                                                                    listening_test)
                                                            .addComponent(
                                                                    listening_test_stop))
                                            .addGap(58, 58, 58)));
        }
    
        /**
         * 时间监听器,得到系统时间和设置好时间后,得到剩余时间
         */
        public void timeListener() {
            new Thread(new Runnable() {// 设置一个线程
                        public void run() {
                            while (true) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                initMainPanel();
                                now.setText(now());// 得到系统时间
                                result.setText(surplus_time());// 得到剩余时间
                            }
                        }
                    }).start();
        }
    
        /**
         * 播放声音的监听器
         */
        public void myListener() {
            new Thread(new Runnable() {// 设置一个线程
                        public void run() {
                            while (true) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                executeSound();// 播放声音
                            }
                        }
                    }).start();
        }
    
        /**
         * 获取返回结果
         * 
         * @return result值
         */
        public String surplus_time() {
            String r = null;
            // 获取系统时,分,秒
            int h = getHour();
            int m = getMunite();
            int s = getSecond();
            // 获取设置的响铃时间
            int dh = 0;
            int dm = 0;
            int ds = 0;
            if (dorp_down_hours.getSelectedIndex() != 0) {
                dh = dorp_down_hours.getSelectedIndex() - 1;
            }
            if (dorp_down_minute.getSelectedIndex() != 0) {
                dm = dorp_down_minute.getSelectedIndex() - 1;
            }
            if (drop_down_second.getSelectedIndex() != 0) {
                ds = drop_down_second.getSelectedIndex() - 1;
            }
            int hour = dh - h;
            int min = dm - m;
            int sec = ds - s;
            if (hour == 0) {
                if (min == 0) {
                    if (sec == 0) {
                        r = "时间到了哦!";
                    }
                    if (sec < 0) {
                        hour += 23;
                        min += 59;
                        sec += 59;
                    }
                }
                if (min < 0) {
                    hour += 23;
                    if (sec < 0) {
                        min -= 1;
                        sec += 59;
                    }
                    min += 60;
                }
                if (min >= 0) {
                    if (sec < 0 || sec == 0) {
                        min -= 1;
                        sec += 59;
                    }
                    if (sec > 0) {
                        // sec=sec;
                    }
                }
            }
            if (hour < 0) {
                if (min <= 0) {
                    if (sec <= 0) {
                        hour -= 1;
                        min += 59;
                        sec += 59;
                    }
                }
                if (min > 0) {
                    if (sec <= 0) {
                        min -= 1;
                        sec += 59;
                    }
                }
                hour += 24;
            }
            if (hour > 0) {
                if (min == 0) {
                    if (sec <= 0) {
                        hour -= 1;
                        min += 59;
                        sec += 59;
                    }
                }
                if (min < 0) {
                    if (sec < 0) {
                        min -= 1;
                        sec += 59;
                    }
                    min += 60;
                    hour -= 1;
                }
                if (min > 0) {
                    if (sec < 0 || sec == 0) {
                        min -= 1;
                        sec += 59;
                    }
                }
            }
    
            if (sec == 30 && min == 0 && hour == 0) {
                setSecondOfResult(sec);
            }
            r = new String("剩:" + hour + "时" + min + "分" + sec + "秒");
            if (hour == 0 && min == 0 && sec < 0) {
                r = "时间到了哦!";
            }
            // result.setText(r);
            return r;
        }
    
        public int getSecondOfResult() {
            return secondOfResult;
        }
    
        public void setSecondOfResult(int sec) {
            this.secondOfResult = sec;
        }
    
        public boolean getFlagOfBackground() {
            return flagOfBackground;
        }
    
        public void setFlagOfBackground(boolean flag) {
            this.flagOfBackground = flag;
        }
    
        /**
         * 时间到了的时候就播放声音
         */
        public void executeSound() {
            // 获取系统时,分,秒
            int h = getHour();
            int m = getMunite();
            int s = getSecond();
            // 获取设置的响铃时间
            int dh = 0;
            int dm = 0;
            int ds = 0;
            if (dorp_down_hours.getSelectedIndex() != 0) {
                dh = dorp_down_hours.getSelectedIndex() - 1;
            }
            if (dorp_down_minute.getSelectedIndex() != 0) {
                dm = dorp_down_minute.getSelectedIndex() - 1;
            }
            if (drop_down_second.getSelectedIndex() != 0) {
                ds = drop_down_second.getSelectedIndex() - 1;
            }
            int hour = dh - h;
            int min = dm - m;
            int sec = ds - s;
            if (hour == 0 && min == 0 && sec == 0) {
                // 主窗体设置为可见
                setVisible(true);
                // 设置窗口前端显示
                setExtendedState(JFrame.NORMAL);
                setAlwaysOnTop(true);
                // 播放声音
                new Thread(new AlarmSound(ring_setup.getSelectedIndex())).start();
                // 震动
                shack();
            }
        }
    
        /**
         * 得到系统时间当前时间,并返回
         * 
         * @return 返回系统当前时间
         */
        public String now() {
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int min = calendar.get(Calendar.MINUTE);
            int sec = calendar.get(Calendar.SECOND);
            // 小于10的时候,在前面加0
            String h = hour < 10 ? "0" : "";
            String m = min < 10 ? "0" : "";
            String s = sec < 10 ? "0" : "";
            String current = new String(h + hour + ":" + m + min + ":" + s + sec);
            return current;
        }
    
        /**
         * 布局下面的按钮和下拉选项的监听器
         */
        @SuppressWarnings("deprecation")
        @Override
        public void actionPerformed(ActionEvent e) {
    
            /**
             * 获取dorp_down_hours,dorp_down_minute,drop_down_second的值
             */
            if (e.getSource() == dorp_down_hours) {
                valueJudgment();
            }
            if (e.getSource() == dorp_down_minute) {
                valueJudgment();
            }
            if (e.getSource() == drop_down_second) {
                valueJudgment();
            }
            if (e.getSource() == stop) {
                stopActionPerformed();
            }
            if (e.getSource() == add1) {
                add1Action();
                myListener();
            }
            if (e.getSource() == add5) {
                add5Action();
                myListener();
            }
            if (e.getSource() == add30) {
                add30Action();
                myListener();
            }
            if (e.getSource() == minus1) {
                minus1Action();
                myListener();
            }
            if (e.getSource() == minus5) {
                minus5Action();
                myListener();
            }
            if (e.getSource() == minus30) {
                minus30Action();
                myListener();
            }
            AlarmSound alarmSound = new AlarmSound(ring_setup.getSelectedIndex());
            Thread alarmThread = new Thread(alarmSound);
            if (e.getSource() == listening_test) {
                // 试听
                if (!alarmThread.isAlive()) {
                    alarmThread.start();
                }
                if (running) {
                    alarmThread.resume();
                }
                listening_test.setEnabled(false);
            }
            if (e.getSource() == listening_test_stop) {
                // 停止试听
                listening_test.setEnabled(true);
                if (running) {
                    alarmThread.suspend();
                }
                running = !running;
            }
            if (e.getSource() == uploadRing) {
                // 上传文件
            }
            if (e.getSource() == exit) {
                // 退出程序
                AlarmFile.exit();
            }
            if (e.getSource() == screenshot) {
                // 主窗体隐藏
                iconed = true;
                MyAlarm.this.setVisible(false);
                // 开始截图
                AlarmTools.screenshot();
            }
            if (e.getSource() == changeBackground) {
                changeBG();
            }
            if (e.getSource() == about) {
                pointX = getMyAlarmX();
                pointY = getMyAlarmY();
                new AlarmHelp("关于程序");
            }
        }
    
        /**
         * 更改背景图片</br> 当点击工具-- >更改背景,这里就可以处理更改背景图片
         */
        private void changeBG() {
            setFlagOfBackground(true);
            if (background < AlarmBackground.getBackgroundListSize()) {
                iconed = true;
                setVisible(false);
                if (background != AlarmBackground.getBackgroundListSize() - 1) {
                    ++background;
                } else {
                    background = 0;
                }
                initMainPanel();
            }
        }
    
        /**
         * 点击-30按钮的时候执行的动作
         */
        private void minus30Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() - 30 < 0) {
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() + 30);// 60-30
                if (dorp_down_hours.getSelectedIndex() == 0) {
                    dorp_down_hours.setSelectedIndex(24);// 设置为23
                } else {
                    // 小时数-1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() - 1);
                }
            } else {
                // 分钟数-30
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() - 30);
            }
            valueJudgment();
        }
    
        /**
         * 点击-5按钮的时候执行的动作
         */
        private void minus5Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() - 5 < 0) {
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() + 55);// 60-5
                if (dorp_down_hours.getSelectedIndex() == 0) {
                    dorp_down_hours.setSelectedIndex(24);// 设置为23
                } else {
                    // 小时数-1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() - 1);
                }
            } else {
                // 分钟数-5
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() - 5);
            }
            valueJudgment();
        }
    
        /**
         * 点击-1按钮的时候执行的动作
         */
        private void minus1Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() - 1 == 0) {
                dorp_down_minute.setSelectedIndex(60);// 设置为59
                if (dorp_down_hours.getSelectedIndex() == 0) {
                    dorp_down_hours.setSelectedIndex(24);// 设置为23
                } else {
                    // 小时数-1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() - 1);
                }
            } else {
                // 分钟数-1
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() - 1);
            }
            valueJudgment();
        }
    
        /**
         * 判断dorp_down_hours,dorp_down_minute,drop_down_second当前是否为0,即:"关闭"
         */
        private void isHMSZero() {
            // 如果小时数还没有设置,那么就设置为当前小时数
            if (dorp_down_hours.getSelectedIndex() == 0) {
                dorp_down_hours.setSelectedIndex(getHour() + 1);
            }
            // 如果分钟数还没有设置,那么就设置为当前分钟数
            if (dorp_down_minute.getSelectedIndex() == 0) {
                dorp_down_minute.setSelectedIndex(getMunite() + 1);
            }
            // 如果秒钟还没有设置,那么就设置为当前秒钟
            if (drop_down_second.getSelectedIndex() == 0) {
                drop_down_second.setSelectedIndex(getSecond());
            }
        }
    
        /**
         * 点击+30按钮的时候执行的动作
         */
        private void add30Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() + 30 > 60) {
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() - 30);// +30-60
                if (dorp_down_hours.getSelectedIndex() > 23) {
                    dorp_down_hours.setSelectedIndex(1);// 设置为00
                } else {
                    // 小时数+1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() + 1);
                }
            } else {
                // 分钟数+30
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() + 30);
            }
            valueJudgment();
        }
    
        /**
         * 点击+5按钮的时候执行的动作
         */
        private void add5Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() + 5 > 60) {
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() - 55);// +5-60
                if (dorp_down_hours.getSelectedIndex() > 23) {
                    dorp_down_hours.setSelectedIndex(1);// 设置为00
                } else {
                    // 小时数+1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() + 1);
                }
            } else {
                // 分钟数+5
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() + 5);
            }
            valueJudgment();
        }
    
        /**
         * 点击+1按钮的时候执行的动作
         */
        private void add1Action() {
            isHMSZero();
            if (dorp_down_minute.getSelectedIndex() + 1 > 60) {
                dorp_down_minute.setSelectedIndex(1);// 设置为00
                if (dorp_down_hours.getSelectedIndex() > 23) {
                    dorp_down_hours.setSelectedIndex(1);// 设置为00
                } else {
                    // 小时数+1
                    dorp_down_hours.setSelectedIndex(dorp_down_hours
                            .getSelectedIndex() + 1);
                }
            } else {
                // 分钟数+1
                dorp_down_minute.setSelectedIndex(dorp_down_minute
                        .getSelectedIndex() + 1);
            }
            valueJudgment();
        }
    
        /**
         * 给h,m,s三个变量赋值,并判断他们的值
         */
        private void valueJudgment() {
            h = dorp_down_hours.getSelectedItem().toString();
            m = dorp_down_minute.getSelectedItem().toString();
            s = drop_down_second.getSelectedItem().toString();
            hsmCheck();
        }
    
        /**
         * 检查时,分,秒的值,如果都不是"关闭",那么</br>
         * 
         * <li>stop按钮要显示出来</li><br/>
         * <li>result要显示出来剩余时间</li><br/>
         * <li>ring_setup要设置为不可用</li> <li>listening_test按钮为不可用</li> <li>
         * listening_test_stop按钮为不可用</li>
         */
        private void hsmCheck() {
            if (h != "关闭" && m != "关闭" && s != "关闭") {
                stop.setVisible(true);
                result.setVisible(true);
                ring_setup.setEnabled(false);
                listening_test.setEnabled(false);
                listening_test_stop.setEnabled(false);
            }
        }
    
        /**
         * stop按钮的动作: <li>
         * 把dorp_down_hours,dorp_down_minute,drop_down_second的值设置为"关闭"</li> <li>
         * 隐藏result</li> <li>ring_setup设置为可用</li> <li>listening_test按钮为可用</li> <li>
         * listening_test_stop按钮为可用</li> <li>stop按钮设置为不可见</li><li>停止声音播放</li>
         */
        private void stopActionPerformed() {
            dorp_down_hours.setSelectedIndex(0);
            dorp_down_minute.setSelectedIndex(0);
            drop_down_second.setSelectedIndex(0);
            result.setVisible(false);
            ring_setup.setEnabled(true);
            listening_test.setEnabled(true);
            listening_test_stop.setEnabled(true);
            // 这里要停止声音
            stop.setVisible(false);
        }
    
        /**
         * 获取当前小时数
         * 
         * @return 返回当前小时数
         */
        private int getHour() {
            return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        }
    
        /**
         * 获取当前分钟数
         * 
         * @return 返回当前分钟数
         */
        private int getMunite() {
            return Calendar.getInstance().get(Calendar.MINUTE);
        }
    
        /**
         * 获取当前秒钟数
         * 
         * @return 返回当前秒钟数
         */
        private int getSecond() {
            return Calendar.getInstance().get(Calendar.SECOND);
        }
    
        /**
         * 实现震动功能
         */
        public void shack() {
            Point p = MyAlarm.this.getLocation();
            int x = (int) p.getX();
            int y = (int) p.getY();
            for (int i = 0; i < 5000; i++) {
                MyAlarm.this.setLocation(x - 5, y);
                MyAlarm.this.setLocation(x, y + 5);
                MyAlarm.this.setLocation(x + 5, y);
                MyAlarm.this.setLocation(x, y - 5);
            }
            MyAlarm.this.setLocation(x, y);
        }
    
        /**
         * 获取MyAlarm的X坐标
         * 
         * @return 返回MyAlarm的X坐标
         */
        public int getMyAlarmX() {
            return (int) getLocation().getX();
        }
    
        /**
         * 获取MyAlarm的Y坐标
         * 
         * @return 返回MyAlarm的Y坐标
         */
        public int getMyAlarmY() {
            return (int) MyAlarm.this.getLocation().getY();
        }
    
        /**
         * 闹铃类*/
        class AlarmSound implements Runnable {
            private String temAlarm;
            private String alarm0Path = "src/image/sound/alarm0.wav";//
            private String alarm1Path = "src/image/sound/alarm1.wav";// 
            private String alarm2Path = "src/image/sound/alarm2.wav";// 
            private String alarm3Path = "src/image/sound/alarm3.wav";// 
            private String alarm4Path = "src/image/sound/alarm4.wav";// 
            private String alarm5Path = "src/image/sound/alarm5.wav";// 
            private String alarm6Path = "src/image/sound/alarm6.wav";// 
            private String alarm7Path = "src/image/sound/alarm7.wav";// 
            private String alarm8Path = "src/image/sound/alarm8.wav";// 
    
            public AlarmSound(int a) {
                switch (a) {
                case 0:
                    temAlarm = alarm0Path;
                    break;
                case 1:
                    temAlarm = alarm1Path;
                    break;
                case 2:
                    temAlarm = alarm2Path;
                    break;
                case 3:
                    temAlarm = alarm3Path;
                    break;
                case 4:
                    temAlarm = alarm4Path;
                    break;
                case 5:
                    temAlarm = alarm5Path;
                    break;
                case 6:
                    temAlarm = alarm6Path;
                    break;
                case 7:
                    temAlarm = alarm7Path;
                    break;
                case 8:
                    temAlarm = alarm8Path;
                    break;
                }
            }
    
            // 读取声音文件,并且播放出来
            public void run() {
                File soundFile = new File(temAlarm);
                AudioInputStream audioInputStream = null;
                try {
                    audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return;
                }
                AudioFormat format = audioInputStream.getFormat();
                SourceDataLine auline = null;
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                try {
                    auline = (SourceDataLine) AudioSystem.getLine(info);
                    auline.open(format);
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
                auline.start();
                int nBytesRead = 0;
                byte[] abData = new byte[512];
                try {
                    while (nBytesRead != -1) {
                        nBytesRead = audioInputStream
                                .read(abData, 0, abData.length);
                        if (nBytesRead >= 0)
                            auline.write(abData, 0, nBytesRead);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                } finally {
                    auline.drain();
                    auline.close();
                }
    
            }
        }
    }
    package com.b510.ui.main;
    
    import java.awt.AWTException;
    import java.awt.Image;
    import java.awt.MenuItem;
    import java.awt.PopupMenu;
    import java.awt.SystemTray;
    import java.awt.TrayIcon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    import com.b510.menu.AlarmFile;
    import com.b510.menu.AlarmTools;
    
    public class MyTray implements ActionListener, MouseListener {
        private Image icon;// 图标
        private TrayIcon trayIcon;
        private SystemTray systemTray;// 系统托盘
    
        private MyAlarm myAlarm; // 托盘所属主窗体
        private PopupMenu pop = new PopupMenu(); // 弹出菜单
        // 菜单选项
        /** 截图 */
        private MenuItem screenshot = new MenuItem("ScreenShot");
        /** 还原 */
        private MenuItem open = new MenuItem("Restore");
        /** 退出*/
        private MenuItem exit =new MenuItem("Exit");
        public MyTray(MyAlarm myAlarm) {
            this.myAlarm = myAlarm;
            // 得到托盘的图标
            icon = new ImageIcon(this.getClass().getClassLoader().getResource(
                    "image/mytray.png")).getImage();
    
            if (SystemTray.isSupported()) {
                systemTray = SystemTray.getSystemTray();
                // 设置鼠标经过图标时,显示的内容
                trayIcon = new TrayIcon(icon, "open Alarm", pop);
                pop.add(screenshot);
                pop.add(open);
                pop.add(exit);
                // 添加系统托盘
                try {
                    systemTray.add(trayIcon);
                } catch (AWTException e1) {
                    e1.printStackTrace();
                    trayIcon.addMouseListener(this);
                }
            }
            displayInfoListener();
            trayIcon.addMouseListener(this);
            exit.addActionListener(this);
            open.addActionListener(this);
            screenshot.addActionListener(this);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==exit){
                //退出系统
                AlarmFile.exit();
            }else if (e.getSource() == open) {
                // 单点击菜单中的"还原"选项时,还原窗口
                //displayInfo();
                //trayIcon.displayMessage("温馨提示", "hello,world", TrayIcon.MessageType.INFO);
                myAlarm.iconed = false;
                friendListSet(true);
                
            } else if (e.getSource() == screenshot) {
                // 但点击“截图”选项时,进行截图
                AlarmTools.screenshot();
            } 
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            // 但鼠标点击一次的时候,进行弹出窗口
            if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
                if (!myAlarm.isVisible()) {
                    friendListSet(true);
                } else {
                    friendListSet(false);
                }
            }
            // 但鼠标点击两次的时候,进行弹出窗口
            // 如果窗口有显示,则隐藏窗口,否则显示窗口
            if (e.getClickCount() == 2 && e.getButton() != MouseEvent.BUTTON3) {
                if (!myAlarm.isVisible()) {
                    friendListSet(true);
                } else {
                    friendListSet(false);
                }
            }
        }
    
        /**
         * 设置friendList的可见性
         */
        private void friendListSet(boolean flag) {
            myAlarm.setVisible(true);
            myAlarm.setExtendedState(JFrame.NORMAL);
        }
        
        
        public void displayInfoListener() {
            new Thread(new Runnable() {// 设置一个线程
                        public void run() {
                            while (true) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                if(myAlarm.getSecondOfResult()==30){
                                     trayIcon.displayMessage("温馨提示","距闹钟设置时间还剩:
    00 时 00 分 " +myAlarm.getSecondOfResult() + " 秒",TrayIcon.MessageType.INFO);
                                     myAlarm.setSecondOfResult(29);
                                }
                                if(myAlarm.getFlagOfBackground()){
                                    trayIcon.displayMessage("温馨提示","您已经更改了背景,点击托盘图标
    可以看到效果,右键托盘图标
    可以进行功能选择。",TrayIcon.MessageType.INFO);
                                    myAlarm.setFlagOfBackground(false);
                                }
                            }
                        }
                    }).start();
        }
    
        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    }
    package com.b510.ui.up;
    
    import java.util.Calendar;
    
    /**
     * 主界面区-显示现在的时间
     */
    public class AlarmShowNow {
    
        /**
         * 获得系统的当前时间,并返回
         * @return 返回系统当前时间
         */
        public static String now() {
            Calendar cal = Calendar.getInstance();
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            int min = cal.get(Calendar.MINUTE);
            int sec = cal.get(Calendar.SECOND);
            String current = new String(hour+ ":" + min + ":" + sec);
            return current;
        }
       public static void main(String args[]){
           System.out.println(now());
       }
    }

    package mai.capture;
    
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    public class BackGroundPanel extends ImagePanel{
    
        private static final long serialVersionUID = 1L;
        int width , height ;
        
        public BackGroundPanel(){
            super();
        }
        
        public BackGroundPanel(String imgUrl){
            super(imgUrl);
        }
        
        public BackGroundPanel(Image img){
            super(img);
        }
        
        public void refreshImg(Image img){
            this.img = img;
            width = getWidth();
            height = getHeight();
        }
        
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
            g2.setColor(Color.BLACK);
            g2.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
        }
    }
    package mai.capture;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.JTextArea;
    import javax.swing.JWindow;
    
    public class CaptureView extends JWindow implements MouseListener, KeyListener,
            MouseMotionListener {
     
        private static final long serialVersionUID = 1L;
        private BufferedImage desktopImg;
        private boolean captured = false, draging = false, toolPanelAtRight = true;
        private int x = 0, y = 0, x1 = 0, y1 = 0, x2 = 1, y2 = 1; // 坐标原点坐标,选区左上角和右下角坐标
        private int point_x, point_y; // 鼠标点坐标
        private Color point_color; // 鼠标点颜色
        private DesktopCapture window; // 所属截图工具主窗体
        private ImagePanel toolPanel; // 提示整框
        private final int TOOLPANEL_WIDTH = 200, TOOLPANEL_HEIGHT = 300,
                HALF_PICK_IMG = 40;
        private JTextArea infoArea; // 提示区
        private ToolImagePanel pickImgPanel; // 放大镜
    
        CaptureView(DesktopCapture window, BufferedImage img) {
            super(window);
            this.window = window;
            this.desktopImg = img;
            setSize(Toolkit.getDefaultToolkit().getScreenSize());
            init();
            setVisible(true);
            setAlwaysOnTop(true);
            this.requestFocus();
        }
    
        void init() {
            this.setContentPane(new BackGroundPanel(desktopImg));
            setLayout(null);
            toolPanel = new ImagePanel("image/weixin_bg.jpg");
            toolPanel.setLayout(new BorderLayout());
            pickImgPanel = new ToolImagePanel();
            infoArea = new JTextArea();
            infoArea.setOpaque(false);
            infoArea.setEditable(false);
            infoArea.setForeground(Color.WHITE);
            infoArea.setFont(new Font("楷体", Font.PLAIN, 11));
            infoArea.setText("");
            toolPanel.add(pickImgPanel, BorderLayout.CENTER);
            toolPanel.add(infoArea, BorderLayout.SOUTH);
            toolPanel.setLocation(getWidth() - TOOLPANEL_WIDTH, 0);
            toolPanel.setSize(TOOLPANEL_WIDTH, TOOLPANEL_HEIGHT);
            this.getLayeredPane().add(toolPanel, 300);
            addKeyListener(this);
            addMouseListener(this);
            addMouseMotionListener(this);
        }
    
        // 桌面屏幕更新
        public void refreshBackGround(BufferedImage img) {
            this.desktopImg = img;
            this.setContentPane(new BackGroundPanel(desktopImg));
            setVisible(true);
            setAlwaysOnTop(true);
            this.requestFocus();
        }
    
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);
            if (captured == true) {
                if (draging) {
                    // 截图辅助十字线
                    g.drawLine(point_x, 0, point_x, getHeight());
                    g.drawLine(0, point_y, getWidth(), point_y);
                }
                confirmArea(); // 确定截图选区的左上角坐标(x1,y1)和右下角坐标(x2,y2)
                if (x1 < x2 && y1 < y2)
                    g.drawImage(desktopImg.getSubimage(x1, y1, Math.abs(x2 - x1),
                            Math.abs(y2 - y1)), x1, y1, null);
                g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
            } else {
                g.drawLine(point_x, 0, point_x, getHeight());
                g.drawLine(0, point_y, getWidth(), point_y);
            }
            repaintToolPanel();
        }
    
        // 确定提示框位置并重画
        public void repaintToolPanel() {
    
            if (toolPanelAtRight == true) {
                if (point_x > (getWidth() - TOOLPANEL_WIDTH - 100)
                        && point_y < (TOOLPANEL_HEIGHT + 100)) {
                    toolPanel.setLocation(0, 0);
                    toolPanelAtRight = false;
                }
            } else {
                if (point_x < (TOOLPANEL_WIDTH + 100)
                        && point_y < (TOOLPANEL_HEIGHT + 100)) {
                    toolPanel.setLocation(getWidth() - TOOLPANEL_WIDTH, 0);
                    toolPanelAtRight = true;
                }
            }
            // 计算point的状态
            point_color = new Color(desktopImg.getRGB(point_x, point_y));
    
            refreshInfoText(); // 更新提示框内容
    
        }
    
        // 提示框状态信息的刷新
        public void refreshInfoText() {
    
            // 文本信息
            String text = new String(
                    "操作提示: By MaiLingFeng  =^-^=
    1.单击托盘图标->进入截图状态
    2.双击右键------>退出截图状态
    3.双击左键------>保存截图
    4.单击右键---->重新选择截图区域");
            String infoString;
            int captureWidth, captureHeight;
            if (captured == true) {
                captureWidth = x2 - x;
                captureHeight = y2 - y;
            } else {
                captureWidth = 0;
                captureHeight = 0;
            }
            infoString = "X,Y : " + point_x + "," + point_y + "    W*H : "
                    + captureWidth + "*" + captureHeight + "
    当前RBG:("
                    + point_color.getRed() + "," + point_color.getGreen() + ","
                    + point_color.getBlue() + ")
    " + text;
            infoArea.setText(infoString);
    
            // 放大镜信息
            int pick_x1, pick_y1, pick_x2, pick_y2, pickImg_x, pickImg_y;
    
            if (point_x - HALF_PICK_IMG < 0) { // 获得放大图片的捡取左上角和右下角坐标,以及在放大镜中的左上角位置坐标
                pick_x1 = 0;
                pick_x2 = point_x + HALF_PICK_IMG;
                pickImg_x = HALF_PICK_IMG - point_x;
            } else if (point_x + HALF_PICK_IMG > this.getWidth()) {
                pick_x1 = point_x - HALF_PICK_IMG;
                pick_x2 = this.getWidth();
                pickImg_x = 0;
            } else {
                pick_x1 = point_x - HALF_PICK_IMG;
                pick_x2 = point_x + HALF_PICK_IMG;
                pickImg_x = 0;
            }
    
            if (point_y - HALF_PICK_IMG < 0) {
                pick_y1 = 0;
                pick_y2 = point_y + HALF_PICK_IMG;
                pickImg_y = HALF_PICK_IMG - point_y;
            } else if (point_y + HALF_PICK_IMG > this.getHeight()) {
                pick_y1 = point_y - HALF_PICK_IMG;
                pick_y2 = this.getHeight();
                pickImg_y = 0;
            } else {
                pick_y1 = point_y - HALF_PICK_IMG;
                pick_y2 = point_y + HALF_PICK_IMG;
                pickImg_y = 0;
            }
    
            BufferedImage pickImg = new BufferedImage(HALF_PICK_IMG * 2,
                    HALF_PICK_IMG * 2, BufferedImage.TYPE_INT_RGB);
            Graphics pickGraphics = pickImg.getGraphics();
            pickGraphics.drawImage(desktopImg.getSubimage(pick_x1, pick_y1, pick_x2
                    - pick_x1, pick_y2 - pick_y1), pickImg_x, pickImg_y,
                    Color.black, null);
            pickImgPanel.refreshImg(pickImg.getScaledInstance(TOOLPANEL_WIDTH,
                    TOOLPANEL_WIDTH, BufferedImage.SCALE_AREA_AVERAGING));
            toolPanel.validate();
        }
    
        // 确定区域的左上点和右下角坐标
        public void confirmArea() {
            int temp;
            // 以x,y为截图选区左上角坐标初值,计算左上角x1,y1和右下角x2,y2的坐标
            x1 = x;
            y1 = y;
            if (x2 < x1) {// 2,3
                if (y2 < y1) { // 2
                    temp = x1;
                    x1 = x2;
                    x2 = temp;
                    temp = y1;
                    y1 = y2;
                    y2 = temp;
                } else { // 4
                    temp = x1;
                    x1 = x2;
                    x2 = temp;
                }
            } else { // 1,4
                if (y2 < y1) { // 1
                    temp = y1;
                    y1 = y2;
                    y2 = temp;
                }
            }
        }
    
        public void exit() {
    
            x = 0;
            y = 0;
            x1 = 0;
            y1 = 0;
            x2 = 1;
            y2 = 1;
            point_x = 0;
            point_y = 0;
            captured = false;
            draging = false;
            toolPanel.setLocation(getWidth() - TOOLPANEL_WIDTH, 0);
            toolPanelAtRight = true;
            this.setVisible(false);
            // this.dispose();
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            // 双击左键,确定选择图像选区,保存截图
            if (e.getButton() == MouseEvent.BUTTON1) {
                if (e.getClickCount() == 2) {
                    this.setVisible(false);
                    window.toFront();
                    window.saveCapture(x1, y1, x2, y2);
                    if (!window.iconed)
                        window.setVisible(false);
                        //window.setVisible(true);
                    exit();
                }
            } else if (e.getClickCount() == 2) {
                if (!window.iconed) {
                    // window.setVisible(true);
                    window.setVisible(false);
                    window.toFront();
                }
                exit();
            }
            if (window.isActive()) {
                System.exit(0);
            }
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            // 左键在未选区完毕时单击,确定选区起点
            if (e.getButton() == MouseEvent.BUTTON1) {
                if (captured == false) {
                    point_x = e.getX();
                    point_y = e.getY();
                    x = point_x; // 选区起点坐标
                    y = point_y;
                    draging = true;
                    captured = true;
                }
            }
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            // 左键释放,确定截图选区
            // 右键释放,重新选区
            if (e.getButton() == MouseEvent.BUTTON1) {
                if (draging == true) {
                    point_x = e.getX();
                    point_y = e.getY();
                    x2 = point_x;
                    y2 = point_y;
                    repaint();
                    draging = false;
                }
            } else {
                draging = false;
                captured = false;
                point_x = e.getX();
                point_y = e.getY();
                repaint();
            }
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub
            // 拖拉截图时画矩形
            if (draging == true) {
                point_x = e.getX();
                point_y = e.getY();
                x2 = point_x;
                y2 = point_y;
                repaint();
            }
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub
            // 未决定截图时,画交叉线
            if (!captured) {
                point_x = e.getX();
                point_y = e.getY();
                repaint();
            }
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
    
            if (e.getKeyCode() == KeyEvent.VK_CANCEL) {
                this.setVisible(false);
                this.dispose();
            }
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
            // System.out.println("aaa");
            if (e.getKeyCode() == KeyEvent.VK_A) {
                this.setVisible(false);
                this.dispose();
            }
    
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                this.setVisible(false);
                this.dispose();
            }
        }
    
    }
    package mai.capture;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class DesktopCapture extends JFrame implements ActionListener {
    
        /**
         * 1.改变双击托盘截图为单击托盘截图 2.修复了托盘后无反应BUG 3.将放大镜提示框移至左上角和右上角
         */
        private static final long serialVersionUID = 1L;
        JButton confirm;
        BufferedImage desktopImg;
        CaptureView view;
        boolean iconed = false;
    
        public DesktopCapture() {
            //super("sunflower-截图工具");
            capture();
            //setVisible(false);
            /*
            init();
            pack();
            setSize(350, 230);
            setLocation(500, 300);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setResizable(false);
            setVisible(true);
            */
        }
    
        void init() {
            setContentPane(new ImagePanel("image/weixin_bg.jpg"));
            Container con = getContentPane();
            con.setLayout(new BorderLayout());
            confirm = new JButton("截图");
            confirm.addActionListener(this);
            JPanel jp = new JPanel();
            jp.setLayout(new FlowLayout());
            jp.setOpaque(false);
            jp.add(new ImageLabel("image/blackCat.png"));
            jp.add(confirm);
            jp.add(new ImageLabel("image/whiteCat.png"));
            JTextArea jta = new JTextArea();
            jta.setOpaque(false);
            jta.setEditable(false);
            jta.setForeground(Color.WHITE);
            jta.setFont(new Font("楷体", Font.PLAIN, 11));
            jta
                    .setText("操作提示:--> 1.单击托盘图标-->进入截图状态
    	2.双击右键------->退出截图状态
    	3.双击左键------->保存截图
    	4.单击右键---->重新选择截图区域   ^_^");
            con.add(jp, BorderLayout.CENTER);
            con.add(jta, BorderLayout.SOUTH);
    
        }
    
        // 获得屏幕图片
        public void captureDesktop() throws Exception {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle rect = new Rectangle(d);
            desktopImg = new BufferedImage((int) d.getWidth(), (int) d.getHeight(),
                    BufferedImage.TYPE_4BYTE_ABGR);
            GraphicsEnvironment environment = GraphicsEnvironment
                    .getLocalGraphicsEnvironment();
            GraphicsDevice device = environment.getDefaultScreenDevice();
            Robot robot = new Robot(device);
            desktopImg = robot.createScreenCapture(rect);
        }
    
        // 截图
        public void capture() {
            this.setVisible(false);
            try {
                Thread.sleep(200);
                captureDesktop();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            if (view == null) {
                view = new CaptureView(this, desktopImg);
            } else {
                view.refreshBackGround(desktopImg);
            }
        }
    
        // 保存截图
        public void saveCapture(int x1, int y1, int x2, int y2) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("*.gif", "gif"));
            chooser.setFileFilter(new FileNameExtensionFilter("*.bmp", "bmp"));
            chooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
            chooser.setSelectedFile(new File(chooser.getCurrentDirectory(), "截图"));
            int state = chooser.showSaveDialog(this);
            if (state == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                FileFilter filter = chooser.getFileFilter();
                String descrition = filter.getDescription();
                String endWith = descrition.substring(2);
                captureToFile(desktopImg.getSubimage(x1, y1, Math.abs(x2 - x1),
                        Math.abs(y2 - y1)), endWith, new File(file
                        .getAbsoluteFile()
                        + "." + endWith));
            }
        }
    
        // 截图输出到文件
        public void captureToFile(BufferedImage img, String endWith, File file) {
            if (!file.exists()) {
                file.mkdir();
            }
            try {
                ImageIO.write(img, endWith, file);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                JOptionPane.showMessageDialog(null, "截图保存出错...");
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                DesktopCapture desk = new DesktopCapture();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getActionCommand().equals("截图")) {
                capture();
            }
        }
    
    }
    package mai.capture;
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    
    public class ImageLabel extends JLabel{
    
        private static final long serialVersionUID = 1L;
    
        /**
         * @param args
         */
        
        
        public ImageLabel(String imgURL){
            ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource(imgURL));
            setSize(icon.getImage().getWidth(null),icon.getImage().getHeight(null));
            setIcon(icon);
            setIconTextGap(0);
            setBorder(null);
            setText(null);
            setOpaque(false);
            
        }
        
        public ImageLabel(ImageIcon icon){
            setSize(icon.getImage().getWidth(null),icon.getImage().getHeight(null));
            setIcon(icon);
            setIconTextGap(0);
            setBorder(null);
            setText(null);
            setOpaque(false);
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
        }
    
    }
    package mai.capture;
    
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    
    public class ImagePanel extends JPanel{
    
    
        private static final long serialVersionUID = 1L;
        /**
         * @param args
         */
        protected Image img;
        
        public ImagePanel(){
            super();
        }
        
        public ImagePanel(String imgUrl){
            super();
    //        img = Toolkit.getDefaultToolkit().createImage(imgUrl);
            img = new ImageIcon(this.getClass().getClassLoader().getResource(imgUrl)).getImage();
            Dimension size = new Dimension(img.getWidth(null),img.getHeight(null));
            
            setSize(size);
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setOpaque(false);
            setLayout(null);
            
        }
        
        public ImagePanel(Image img){
            super();
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null),img.getHeight(null));
            setSize(size);
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setOpaque(false);
            setLayout(null);
        }
        
        public void refreshImg(Image img){
            this.img = img;
        }
        
        public void paintComponent(Graphics g ){
            super.paintComponent(g);
            g.drawImage(img,0,0,null);
        }
        
    
    }
    package mai.capture;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    public class ToolImagePanel extends ImagePanel{
    
        private static final long serialVersionUID = 1L;
        int width , height ;
        int QUANTITY = 1;    //实际为奇数,向上取奇
        
        public ToolImagePanel(){
            super();
        }
        
        public ToolImagePanel(String imgUrl){
            super(imgUrl);
                    
        }
        
        public ToolImagePanel(Image img){
            super(img);
        }
        
        public void refreshImg(Image img){
            this.img = img;
            width = getWidth();
            height = getHeight();
        }
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawLine(width/2 , 0, width/2 , height);
            g.drawLine(0, height/2  , width, height/2);
            for(int i=1; i < QUANTITY/2 + 1 ; i++){
                g.drawLine(width/2 - i, 0, width/2 - i, height);
                g.drawLine(0, height/2 - i , width, height/2 - i);
            }
            for(int i=1; i < QUANTITY/2 + 1 ; i++){
                g.drawLine(width/2 + i, 0, width/2 + i, height);
                g.drawLine(0, height/2 + i , width, height/2 + i);
            }
            
        }
    }
  • 相关阅读:
    bs4解析错误之 bs4 FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.
    UI自动化之selenium元素定位不到问题的原因有哪几种?
    selenium定位:出现Message: element not interactable 元素不可交互的问题解决方案
    JS--编码规范
    JS操作数组-2
    JS-数组操作3
    JS操作数组
    用JS解决url地址中参数乱码的问题
    数组去重--ES5和ES6
    选择排序
  • 原文地址:https://www.cnblogs.com/tt-t/p/6476004.html
Copyright © 2011-2022 走看看