小闹钟,会响了……
又是很久没有写过博客了。
唉……
现在放假了,那么,就补上博客吧。
先从定时的闹钟来吧。
?
简要来说,一个闹钟,就是要实现三个要点:获取当前系统时间,并且随时改变;设定一个定时项目,到时间会启动,音乐能够播放;第三,界面的设计。现在就分别来阐述总结一下。
<一>系统时间的获取,要用到 java.text包下面的SimpleDateFormat类并实例化一个对象
String time;SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH时mm分ss秒");//这里注释一下,上面的那个格式也可以自己再去改,自己去查一次API就会知道其余时间的表示方法啦time=simpleDateFormat.format(new Date());?现在这个代码实现了获取当前的时间。但是我们要使得时间每一秒都获得一次啊,每秒都要更新,这就可以用一个线程来控制,因为线程中有sleep的方法嘛。代码如下:
Thread thread = new Thread(){ public void run(){while(true){ simpleDateFormat = new SimpleDateFormat("HH时mm分ss秒"); time=simpleDateFormat.format(new Date()); try {Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}}?这样呢,第一步就完成啦!
<二>定时任务
这个时候我们要用到?java.util.Timer?和java.util.TimerTask这两个类,并且实例化一个对象
public static void main(String[] args) { //TimerTaskDemo是继承了TimerTask的一个子类TimerTaskDemo taskDemo = new TimerTaskDemo();Timer timer = new Timer();timer.schedule( taskDemo,0,1000); //这三个参数可以自己去查一下啦,第一个是那个定时器对象,第二个是持续时间,第三个是间隔时间} //run方法就是要执行的内容啦,所以要播放音乐public void run() { //获取音乐的路径(这是我的路径哈)URL url = this.getClass().getResource("change.wav"); sound = Applet.newAudioClip(url); sound.play(); }}这样就可以计时啦。
第三步是界面设计……先配一个效果图

?现在把整个界面的代码附上:
package Albert20130918;import java.applet.Applet;import java.applet.AudioClip;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Timer;import java.util.TimerTask;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingConstants;/** * 闹钟 * @author Mengqing.L * */public class AlarmClock extends TimerTask {public SimpleDateFormat simpleDateFormat=null;public JLabel clock1 = new JLabel();private String time;private JLabel clock;private static boolean on=false;private static boolean soundOn=false;private int hourNumber;private int minuteNumber;private int secondNumber;private String path;/** * 主函数 */public static void main(String[] args){AlarmClock alarmClock = new AlarmClock();alarmClock.initUI();Timer timer = new Timer();timer.schedule(alarmClock,1000,10000);}private void initUI() {String[]countHourStrings=new String[24];int[]countHour = new int[24];String[]countMinuteStrings=new String[60];int[]countMinute = new int[60];String[]countSecondStrings=new String[60];int[]countSecond = new int[60]; JFrame jFrame = new JFrame(); jFrame.setTitle("孟卿的小闹钟"); jFrame.setSize(400,400); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(3); jFrame.setResizable(false); jFrame.setLayout(new BorderLayout());JPanel show = new JPanel();show.setPreferredSize(new Dimension(10,100)); // show.setBackground(Color.black);jFrame.add(show,BorderLayout.NORTH);JPanel control = new JPanel();control.setBackground(Color.green);control.setPreferredSize(new Dimension(10,300));jFrame.add(control,BorderLayout.SOUTH); //添加背景URL backgroundPath = this.getClass().getResource("背景.PNG");ImageIcon background = new ImageIcon(backgroundPath); JLabel back = new JLabel(background);back.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());jFrame.getLayeredPane().add(back,new Integer(Integer.MIN_VALUE)); JPanel lastJPanel = (JPanel)jFrame.getContentPane(); lastJPanel.setOpaque(false);show.setOpaque(false);control.setOpaque(false);//添加两个标签JLabel clock0 = new JLabel();clock0.setPreferredSize(new Dimension(150,70)); clock0.setHorizontalAlignment(SwingConstants.CENTER);clock0.setForeground(Color.red);clock0.setFont(new Font("隶书",Font.BOLD,22));clock1.setPreferredSize(new Dimension(150,60)); clock1.setHorizontalAlignment(SwingConstants.CENTER);clock1.setForeground(Color.red);clock1.setFont(new Font("隶书",Font.BOLD,22));show.add(clock0,BorderLayout.CENTER);show.add(clock1,BorderLayout.CENTER);//获取当前系统 时间this.simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");time=this.simpleDateFormat.format(new Date());//clock0.setText(time);iikclock0.setText("当前时间为:");Thread thread = new Thread(){public void run(){while(true){simpleDateFormat = new SimpleDateFormat("HH时mm分ss秒");time=simpleDateFormat.format(new Date());URL url = this.getClass().getResource("change.wav"); AudioClip sound = Applet.newAudioClip(url); sound.play();try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}clock1.setText(time);}}};thread.start();this.simpleDateFormat = new SimpleDateFormat("HH时mm分ss秒");time=this.simpleDateFormat.format(new Date());clock1.setText(time); //通过Calendar类获取时间Calendar calendar = new GregorianCalendar(); final int[] date = new int[7]; date[0]=calendar.get(Calendar.YEAR);date[1]=calendar.get((Calendar.MONTH))+1;date[2]=calendar.get(Calendar.DAY_OF_MONTH);date[3]=calendar.get(Calendar.HOUR_OF_DAY);date[4]=calendar.get(Calendar.MINUTE);date[5]=calendar.get(Calendar.SECOND);date[6]=calendar.get(Calendar.MILLISECOND);////控制时间的面板JPanel timeInput = new JPanel();timeInput.setPreferredSize(new Dimension(250,290));//timeInput.setBackground(Color.black);timeInput.setOpaque(false);control.add(timeInput,BorderLayout.WEST);//确定区,并且加上两个按钮JPanel sureInput = new JPanel();sureInput.setPreferredSize(new Dimension(125,290));//sureInput.setBackground(Color.blue);sureInput.setOpaque(false);control.add(sureInput,BorderLayout.EAST);//三个按钮JButton sureButton = new JButton("确定");JButton cancelButton = new JButton("取消");JButton sound = new JButton("声音");//按钮的设置sureButton.setForeground(Color.blue);sureButton.setBackground(Color.green);sureButton.setFont(new Font("隶书",Font.BOLD,30));sureButton.setOpaque(false);sureButton.setPreferredSize(new Dimension(100,80));sureInput.add(sureButton);cancelButton.setForeground(Color.blue);cancelButton.setBackground(Color.green);cancelButton.setOpaque(false);cancelButton.setFont(new Font("隶书",Font.BOLD,30));cancelButton.setPreferredSize(new Dimension(100,80));sureInput.add(cancelButton);sound.setForeground(Color.blue);sound.setBackground(Color.green);sound.setOpaque(false);sound.setFont(new Font("隶书",Font.BOLD,30));sound.setPreferredSize(new Dimension(100,80));sureInput.add(sound);//添加24小时for(int i=0;i<24;i++){countHour[i]=i;countHourStrings[i]=String.valueOf(countHour[i]);}//添加60分钟for(int j=0;j<60;j++){countMinute[j]=j;countMinuteStrings[j]=String.valueOf(countMinute[j]);}//添加60秒for(int k=0;k<60;k++){countSecond[k]=k;countSecondStrings[k]=String.valueOf(countSecond[k]);}//添加时、分、秒三个标签和三个输入框JLabel hour = new JLabel("时:");hour.setHorizontalAlignment(SwingConstants.CENTER);JComboBox hourInput = new JComboBox(countHourStrings);hour.setForeground(Color.green);hour.setBackground(Color.green);hour.setOpaque(false);hour.setFont(new Font("隶书",Font.BOLD,30));hour.setPreferredSize(new Dimension(100,80));timeInput.add(hour);hourInput.setForeground(Color.red);hourInput.setBackground(Color.green);hourInput.setOpaque(false);hourInput.setFont(new Font("隶书",Font.BOLD,25));hourInput.setPreferredSize(new Dimension(120,60));timeInput.add(hourInput);JLabel minute = new JLabel("分:");minute.setHorizontalAlignment(SwingConstants.CENTER);JComboBox minuteInput = new JComboBox(countMinuteStrings);minute.setForeground(Color.green);minute.setBackground(Color.green);minute.setOpaque(false);minute.setFont(new Font("隶书",Font.BOLD,30));minute.setPreferredSize(new Dimension(100,80));timeInput.add(minute);minuteInput.setForeground(Color.red);minuteInput.setBackground(Color.green);minuteInput.setOpaque(false);minuteInput.setFont(new Font("隶书",Font.BOLD,25));minuteInput.setPreferredSize(new Dimension(120,60));timeInput.add(minuteInput);JLabel second = new JLabel("秒:");second.setHorizontalAlignment(SwingConstants.CENTER);JComboBox secondInput = new JComboBox(countSecondStrings);second.setForeground(Color.green);second.setBackground(Color.green);second.setOpaque(false);second.setFont(new Font("隶书",Font.BOLD,30));second.setPreferredSize(new Dimension(100,80));timeInput.add(second);secondInput.setForeground(Color.red);secondInput.setBackground(Color.green);secondInput.setOpaque(false);secondInput.setFont(new Font("隶书",Font.BOLD,25));secondInput.setPreferredSize(new Dimension(120,60));timeInput.add(secondInput);//添加计算方式 String date3String = String.valueOf(date[3]); String date4String = String.valueOf(date[4]); String date5String = String.valueOf(date[5]);/ hourNumber = hourInput.getSelectedIndex(); minuteNumber = minuteInput.getSelectedIndex(); secondNumber = secondInput.getSelectedIndex();//添加监听器sureButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {on=true;System.out.println("确定");if((hourNumber==date[3])&&(minuteNumber==date[4])&&(secondNumber==date[5])){ run();}}});cancelButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {on=false;System.out.println("取消");}});sound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {soundOn=true;System.out.println("音乐开启");JFileChooser chooser = new JFileChooser();path = chooser.getName();}});jFrame.setVisible(true);//return hourNumber*3600+minuteNumber*60+secondNumber-(hourNumber*3600+minuteNumber*60+secondNumber)*100;}public void run() {if(on){System.out.println("激活");URL soundURL = this.getClass().getResource(path);AudioClip sound = Applet.newAudioClip(soundURL);if(soundOn)sound.play();on=false;}}?这个小闹钟不难,只是要把界面弄得好看一点啦。
?
就是这样了,想想还有搜索器、二叉树、哈夫曼树以及打字线程游戏没有总结,还有Unity3d的一些东东,我保证会补上的!敬请期待!
?
?
加油~