当前位置:编程学习 > C#/ASP.NET >>

求一个算法

我最近在学c# 在编程的时候遇到个问题 想请大大们帮我解决下

问题描述:
    编写一个windows应用程序,输入一个时间,格式“XX:XX:XX”,然后到了该时间,响应一个事件,比如弹出一个对话框,提示该吃饭了。

我使用了timer来计时,声明了委托和事件,但是还是出问题,希望哪位大大能帮我写个算法解决下。 --------------------编程问答-------------------- PS:我刚学到委托和事件,所以希望大大们用这个来实现。 --------------------编程问答-------------------- 你问题出在哪呢? --------------------编程问答-------------------- 完全可以用Timer来实现 这个没难度吧 --------------------编程问答-------------------- 贴一下代码…… --------------------编程问答-------------------- 完全没难度的,得说出你的问题是什么; --------------------编程问答-------------------- 这个是我以前写的一个定时关机的代码,你看下,看能不能给你帮助吧
package shutdown2;

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;

public class Shutdown {
public static void main(String[] args) {
new Windows();
}
}

class Windows extends JFrame implements ActionListener {

int h = Toolkit.getDefaultToolkit().getScreenSize().height;
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
JPanel jPanel1,jPanel1_1,jPanel1_2,jPanel1_3;
JLabel jLabel1,jLabel2,jLabel3,jLabel4;
JTextField jTextField1,jTextField2,jTextField3;
JButton jButton1,jButton2,jButton3,jButton4,jButton5;
Timer t;

public Windows() {
jPanel1 = new JPanel(new BorderLayout());
jPanel1_1 = new JPanel(new FlowLayout());
jPanel1_2 = new JPanel(new FlowLayout());
jPanel1_3 = new JPanel(new FlowLayout());

jLabel1 = new JLabel("设置小时");
jLabel2 = new JLabel("设置分钟");
jLabel3 = new JLabel("设置秒钟");
t = new Timer(1000,this);
t.start();
jLabel4 = new JLabel("当前时间:"+Calendar.getInstance().getTime().toLocaleString());

jTextField1 = new JTextField(8);
jTextField2 = new JTextField(8);
jTextField3 = new JTextField(8);

jButton1 = new JButton("关机");jButton1.addActionListener(this);
jButton2 = new JButton("重启");jButton2.addActionListener(this);
jButton3 = new JButton("注销");jButton3.addActionListener(this);
jButton4 = new JButton("清空");jButton4.addActionListener(this);
jButton5 = new JButton("放弃");jButton5.addActionListener(this);

jPanel1_1.add(jLabel1);
jPanel1_1.add(jTextField1);
jPanel1_1.add(jButton1);
jPanel1_1.add(jLabel2);
jPanel1_1.add(jTextField2);
jPanel1_1.add(jButton2);
jPanel1_1.add(jLabel3);
jPanel1_1.add(jTextField3);
jPanel1_1.add(jButton3);
jPanel1_2.add(jButton4);
jPanel1_2.add(jButton5);
jPanel1_3.add(jLabel4);

jPanel1.add(jPanel1_3,BorderLayout.NORTH);
jPanel1.add(jPanel1_1,BorderLayout.CENTER);
jPanel1.add(jPanel1_2,BorderLayout.SOUTH);

add(jPanel1);
this.setBounds((w-400)/2, (h-200)/2, 250, 210);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
this.jLabel4.setText("当前时间:" + Calendar.getInstance().getTime().toLocaleString());
MyTask myTask = new MyTask();
MyCalendar m = new MyCalendar();
if (e.getSource() == jButton1) {
myTask.string = "shutdown.exe -s -t 10";
// myTask.string = "C:/Program Files/Windows Media Player/wmplayer.exe E:/音乐/SHE/中国话.mp3";
m.MyCalendars(Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()),myTask);
}
if (e.getSource() == jButton2) {
myTask.string = "shutdown.exe -r -t 10";
// myTask.string = "C:/Program Files/Windows Media Player/wmplayer.exe E:/音乐/SHE/S.H.E-只为爱上你.mp3";
m.MyCalendars(Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()),myTask);
}
if (e.getSource() == jButton3) {
myTask.string = "shutdown.exe -l";
// myTask.string = "C:/Program Files/Windows Media Player/wmplayer.exe E:/音乐/SHE/S.H.E、周定纬-两个人的荒岛.mp3";
m.MyCalendars(Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()),myTask);
}
if (e.getSource() == jButton4) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
if (e.getSource() == jButton5) {
myTask.string = "shutdown.exe -a";
m.MyCalendars(Integer.parseInt(jTextField1.getText()),Integer.parseInt(jTextField2.getText()),Integer.parseInt(jTextField3.getText()),myTask);

}
}
} --------------------编程问答-------------------- 其实我也不大懂 --------------------编程问答-------------------- 我的代码
private void myTimer_Tick(Object myObject, EventArgs myEventArgs)
        {
            string now;
            string current = "";
            current = DateTime.Now.ToString();
            int space = displayTime.Text.IndexOf(" ");
            now = current.Substring(space, current.Length - space - 1);
            string time = comboBoxHour.Text + ":" + comboBoxMin.Text + ":" + comboBoxSec.Text;
            if (now.Equals(time))
            {
                MessageBox.Show("该吃饭了!");
            }
        }

        private void timeBoot_Click(object sender, EventArgs e)
        {
            TimerEnable = true;
            Timer myTimer = new Timer();
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Interval = 1000;
            myTimer.Start();
            MessageBox.Show("定时器已经启动。");
        }
--------------------编程问答-------------------- 重贴
private void myTimer_Tick(Object myObject, EventArgs myEventArgs)
        {
            string now;
            string current = "";
            current = DateTime.Now.ToString();
            int space = current.IndexOf(" ");
            now = current.Substring(space, current.Length - space - 1);
            string time = comboBoxHour.Text + ":" + comboBoxMin.Text + ":" + comboBoxSec.Text;
            if (now.Equals(time))
            {
                MessageBox.Show("该吃饭了!");
            }
        }

        private void timeBoot_Click(object sender, EventArgs e)
        {
            TimerEnable = true;
            Timer myTimer = new Timer();
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Interval = 1000;
            myTimer.Start();
            MessageBox.Show("定时器已经启动。");
        }
--------------------编程问答-------------------- 有什么问题? --------------------编程问答-------------------- 不能实现定时啊 这个程序要怎么改呢??? --------------------编程问答--------------------

System.Timers.Timer timer = new System.Timers.Timer();
   timer.Interval = 1000;//定时器每隔一秒执行一次
   timer.Enabled = true;
   timer.Elapsed += delegate
           {
//每次执行内容 可以是调用方法

            string now;
            string current = "";
            current = DateTime.Now.ToString();
            int space = current.IndexOf(" ");
            now = current.Substring(space, current.Length - space - 1);
            string time = comboBoxHour.Text + ":" + comboBoxMin.Text + ":" + comboBoxSec.Text;
               if (now.Equals(time))
                    {
                        MessageBox.Show("该吃饭了!");
                     }

            }
                                          

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,