下面是源码
但是这个不够理想
我想要个 比如像辩论赛那种 上来一个人 给他一个规定时间发言 在界面上输入时间后按开始,界面上有显示时间的倒计时
的地方 并且能在时间还有30秒的时候 用弹出对话框的方式提醒 比如30秒时出来一个对话框提醒还有30秒 然后20秒的时候
出来 10秒的时候出来 !
下面是一个 但是只能在时间结束的时候弹出来 不能显示时间再倒记时(就是倒记时读秒的过程,比如像秒表那样显示小时,分,秒 那种表现形式),
能帮我的朋友们 可以运行下我下面的程序 里面有个托盘图标 大家可以随便加个图片的位置
运行下就明白我的意思了
希望能在我程序的基础上改下我程序 使它能实现我上面写的功能! 或者如果有朋友有更好的程序能发来给我,我会十分感激的!
比较着急 因为周末要用的 希望各位高手来帮个忙!
 或者qq 14990544
import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;public class Alerter extends JFrame implements ActionListener {
private Image image;// 托盘图标
private TrayIcon trayIcon;
private SystemTray systemTray;// 系统托盘 int alertInterval = 0;
final String[] in = { "周", "日", "时", "分", "秒" };
final int ONEDAYMILLISECONDS = 24 * 3600 * 1000;
final int[] milliSeconds = { 7 * this.ONEDAYMILLISECONDS,
this.ONEDAYMILLISECONDS, 3600 * 1000, 60 * 1000, 1000 }; Timer timer;
JLabel time;
JLabel interval;
JTextField msg;
JTextField tf;
JComboBox cb;
JButton button; Alerter() {
systemTray = SystemTray.getSystemTray();// 获得系统托盘的实例
try {
image = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("temp.jpg"));// 定义托盘图标的图片
trayIcon = new TrayIcon(image, "系统托盘");
systemTray.add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2)// 双击托盘窗口再现
setExtendedState(Frame.NORMAL);// 状态
setVisible(true);
}
}); time = new JLabel("请输入提示信息:");
interval = new JLabel("提醒周期:");
msg = new JTextField(12);
tf = new JTextField(10);
cb = new JComboBox(in);
cb.setSelectedIndex(2);
button = new JButton("确定");
button.addActionListener(this); this.setTitle("定时提醒器");
this.setBounds(200, 150, 300, 250);
this.setVisible(true);
this.setLayout(new FlowLayout());
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
dispose();// 窗口最小化时dispose该窗口
}
}); this.add(time);
this.add(msg);
this.add(interval);
this.add(tf);
this.add(cb);
this.add(button); tf.requestFocus();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
if (!tf.getText().equals("")) {
int idx = cb.getSelectedIndex();
this.alertInterval = Integer.parseInt(tf.getText())
* this.milliSeconds[idx];
if (timer == null) {
timer = new Timer(this.alertInterval, this);
} else {
timer.setDelay(this.alertInterval);
}
timer.start();
}
} else if (e.getSource() == timer) {
JOptionPane.showMessageDialog(this, msg.getText(), "叮呤呤!",
JOptionPane.DEFAULT_OPTION);
}
}
public static void main(String[] args) {

new Alerter();
}}

解决方案 »

  1.   

    你需要在每10钟后,新建一个 timer
      

  2.   

    请楼上的朋友帮我修改下 我是现学的 图形化界面 以前从没注意过这章的东西
    代码也是找例子摸索修改的 实在是搞不了 timer 那里我也尝试修改 但是总是搞不了
    希望帮我下 不了解这个Timer类的意思 搞了一下午头都大了
      

  3.   

    你那代码太复杂了吧??
    我考虑用线程实现我贴一下我写的90分钟倒计时的方法,你可以参考一下,在适当的地方加入一些判断,就能达到你想要的功能了.
    class EndTime extends Thread {
    public void run() {
    String time[] = { "90", "0" };//这里输入需要倒计时的时间
    int m = Integer.valueOf(time[0]);
    int s = Integer.valueOf(time[1]);
    while (true) {
    jl_time.setText("考试时间:" + String.valueOf(m) + " 分" + String.valueOf(s) + " 秒");//jl_time是一个JLable,用来显示倒计时
    if (s > 0 && s < 60) {
    s--;
    }
    if (s == 0 && m > 0) {
    s = 59;
    m--;
    }
    if (s == 0 && m == 0) {
    JOptionPane.showMessageDialog(null, "时间到!");
    }
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
      

  4.   


    用BUTTON,JLABLE都可以,想怎么弄就怎么弄....呵呵
      

  5.   

    线程控制是好,但是不能用sleep(),来控制时间,这个是不准的,试试在线程中获得系统时间来计算已过去的时间