java开发之闹钟的实现代码

2016-02-19 10:04 79 1 收藏

下面是个简单易学的java开发之闹钟的实现代码教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

项目结构:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

效果图一:

效果图二:

效果图三:

关于闹钟:



代码如下:

Alarm hongten v1.0

 使用说明:

     此文件夹中包括:
             src文件夹       存放图片和声音文件夹,使用程序的时候,请不要删除此文件夹
             alarm.jar    主要的功能程序jar包(要求安装jdk1.5或以上版本)
             ReadMe.txt     说明文档

 

  1.菜单功能说明

     文件——    
           上传铃声(还没实现)
           退出                     功能:退出程序
     工具——    
           截图工具              功能:进行截图
           更改背景        功能:更改背景
     帮助——    
           关于程序        功能:查看程序说明

 
  2.设置闹表

     2.1.可以在响铃时间里设置闹表时间

     2.2.还可以用快速设置,+1表示在系统时间的基础上增加一分钟,-1表示在系统时间的基础上减去一分钟                (+5,+30,-5,-30同样原理)

  3.提示铃声

     可以选择铃声类型,进行试听,试听是时候可以停止(该功能还没完善)

  4.系统托盘

     4.1.在主窗体隐藏的时候,单击左键,主窗体可以显示出来;

     4.2.在主窗体隐藏的时候,单击右键,可以进行功能选择:退出,恢复主窗体可见,截图

     4.3.在设置的闹表时间剩下30秒是时候,系统会自动提示信息(电脑右下角会弹出消息提示框)

     4.4.在设置更改背景的时候,同样会弹出提示信息(所有的提示信息会自动隐退)

下面是代码部分

==========================================================

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

/Alarm/src/com/b510/main/AlarmMain.java
代码如下:

package com.b510.main;

 import com.b510.ui.main.MyAlarm;

 /**
  *
  * @author Hongten
  */
 public class AlarmMain {

     public static void main(String[] args) {
         new MyAlarm("Alarm hongten v-1.0");
     }
 }

/Alarm/src/com/b510/menu/AlarmBackground.java
代码如下:

package com.b510.menu;

 /**
  * @author Hongten
  *
  * @time 2012-3-4  2012
  */
 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];
     }
 }

/Alarm/src/com/b510/menu/AlarmFile.java
代码如下:

package com.b510.menu;

 /**
  *
  * @author Hongten
  * 菜单-文件
  */
 public class AlarmFile {

     /**
      * 退出程序
      */
     public static void exit(){
         System.exit(0);
     }
 }

/Alarm/src/com/b510/menu/AlarmHelp.java
代码如下:

/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */
 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;

 /**
  *
  * @author Hongten 菜单-帮助
  */
 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("http://www.cnblogs.com/hongten"); // 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();
     }
 }

/Alarm/src/com/b510/menu/AlarmTools.java
代码如下:

/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */

 package com.b510.menu;

 import mai.capture.DesktopCapture;

 /**
  *
  * @author Hongten
  * 菜单-工具
  */
 public class AlarmTools {

     /** 定义截图功能 */
     @SuppressWarnings("unused")
     private static DesktopCapture deskTopCapture;
     /**
      * 截图
      */
     public static void screenshot(){
         deskTopCapture=new DesktopCapture();
     }
 }

/Alarm/src/com/b510/ui/main/MyAlarm.java
代码如下:

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;

 /**
  *
  * @author Hongten
  */
 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;
     /**
      * wenjian
      */
     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"); // NO

来源:http://www.tulaoshi.com/n/20160219/1593231.html

延伸阅读
标签: Web开发
代码如下: 1.  script language="javascript"  !--  location.replace("www.goldtimes.net");  --/script  2.  meta http-equiv="refresh" content="0;url=http://www.goldtimes.net"   3.  script language="javascript"  !--  self.location="";&nbs...
当主流计算机应用软件开始迁移到客户/服务器体系结构时,程序员们开始寻找方法以简化使用类似技术并且在结构也类似的工程的开发。这就为现代软件开发框架打好了基础。 随着基于万维网的应用程序服务器的发展及其相关的应用软件的膨胀,支持这些技术的开发框架也随着蓬勃发展。当前,在企业开发领域中出现了许多特别适合于Java J2EE平台的软件开发...
先看看代码再说: 代码如下: package com.b510.note;  import java.math.BigInteger;  import java.security.MessageDigest;  import java.security.NoSuchAlgorithmException;  /**   * MD5加密   *   * @author Hongten   *   */  public class MD5 {      pub...
标签: Web开发
//改變時的事件 代码如下: $("#testSelect").change(function(){ //事件發生 jQuery('option:selected', this).each(function(){ //印出選到多個值 alert(this.value); }); }); //印出選到的項目 代码如下: 法1:$("select#Clubs").children("[@selected]").each(function(){ alert(this.text); }); 法2:$("#selBags").va...
去掉标题栏: requestWindowFeature(Window.FEATURE_NO_TITLE); API上是这么说的: int     FEATURE_NO_TITLE     Flag for the "no title" feature, turning off the title at the top of the screen. 屏幕全屏: getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); API上是这么说的...

经验教程

735

收藏

59
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部