请熟悉java图形用户界面的进来帮个忙 (倒计时提醒器)急啊!!

Edwin603 2009-07-09 05:13:27
下面是源码
但是这个不够理想
我想要个 比如像辩论赛那种 上来一个人 给他一个规定时间发言 在界面上输入时间后按开始,界面上有显示时间的倒计时
的地方 并且能在时间还有30秒的时候 用弹出对话框的方式提醒 比如30秒时出来一个对话框提醒还有30秒 然后20秒的时候
出来 10秒的时候出来 !
下面是一个 但是只能在时间结束的时候弹出来 不能显示时间再倒记时(就是倒记时读秒的过程,比如像秒表那样显示小时,分,秒 那种表现形式),
能帮我的朋友们 可以运行下我下面的程序 里面有个托盘图标 大家可以随便加个图片的位置
运行下就明白我的意思了
希望能在我程序的基础上改下我程序 使它能实现我上面写的功能! 或者如果有朋友有更好的程序能发来给我,我会十分感激的!
比较着急 因为周末要用的 希望各位高手来帮个忙!
我的邮箱waegflp@126.com 或者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();
}

}
...全文
317 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cangzhubai 2009-07-09
  • 打赏
  • 举报
回复
路过学习
huosidun0302 2009-07-09
  • 打赏
  • 举报
回复
Edwin603 2009-07-09
  • 打赏
  • 举报
回复
各路高手们 我是今天下午才临时的看了看 图形化界面 看的我快晕过去了
请哪位朋友 帮我修改下 或者 发我个改好的 不胜感激啊!
立刻给分!!
pengliaoye 2009-07-09
  • 打赏
  • 举报
回复
gaoshou ,,我顶!!!
zl3450341 2009-07-09
  • 打赏
  • 举报
回复
帮顶 ~
ddyouyue 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 reeves101 的回复:]
你那代码太复杂了吧??
我考虑用线程实现

我贴一下我写的90分钟倒计时的方法,你可以参考一下,在适当的地方加入一些判断,就能达到你想要的功能了.

Java codeclass EndTime extends Thread {
public void run() {
String time[] = { "90", "0" };//这里输入需要倒计时的时间
int m = Integer.valueOf(time[0]);
int s = Integer.valueOf(time[1]);
while (…
[/Quote]
线程控制是好,但是不能用sleep(),来控制时间,这个是不准的,试试在线程中获得系统时间来计算已过去的时间
zuoguodang 2009-07-09
  • 打赏
  • 举报
回复
另起一个线程
reeves101 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Edwin603 的回复:]
还有如何实现 在我的窗口里 有个能显示倒计时 的地方
[/Quote]

用BUTTON,JLABLE都可以,想怎么弄就怎么弄....呵呵
reeves101 2009-07-09
  • 打赏
  • 举报
回复
你那代码太复杂了吧??
我考虑用线程实现

我贴一下我写的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();
}
}
}
}


Edwin603 2009-07-09
  • 打赏
  • 举报
回复
还有如何实现 在我的窗口里 有个能显示倒计时 的地方
Edwin603 2009-07-09
  • 打赏
  • 举报
回复
请楼上的朋友帮我修改下 我是现学的 图形化界面 以前从没注意过这章的东西
代码也是找例子摸索修改的 实在是搞不了 timer 那里我也尝试修改 但是总是搞不了
希望帮我下 不了解这个Timer类的意思 搞了一下午头都大了
laorer 2009-07-09
  • 打赏
  • 举报
回复
你需要在每10钟后,新建一个 timer
Edwin603 2009-07-09
  • 打赏
  • 举报
回复
请各位高手们帮忙拉 代码有点长 眼睛会有点晕 能帮做出来的 我立刻追加分
不胜感激!!!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧