Tag: 计时器

Java – 使用大量RAM的GUI时钟?

我为Java中的桌面小部件制作了一个小时钟(该小部件还包括许多其他function)。 我检查了任务管理器中的应用程序RAM使用情况,看看时钟是否使用了700多MB的RAM。 我禁用了时钟,RAM使用率下降到大约60 MB。 这是时钟代码: final int timeRun = 0; new Thread() { public void run() { while(timeRun == 0) { Calendar cal = new GregorianCalendar(); int hour = cal.get(Calendar.HOUR); int min = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int AM_PM = cal.get(Calendar.AM_PM); String day_night = “”; if (AM_PM == 1){ day_night = “PM”; }else{ day_night […]

Java Swing Timer倒计时

我必须做一个倒计时程序,也显示十分之一秒; 例如,从10.0秒倒计时,它应显示9.9秒,9.8秒,… 0.0秒 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { timer.start(); timer2.start(); } Double timeLeft=5000; //5 seconds Timer timer=new Timer(1,countDown); Timer timer2=new Timer(1000,countDown2); ActionListener countDown=new ActionListener() { public void actionPerformed(ActionEvent e) { timeLeft–; SimpleDateFormat df=new SimpleDateFormat(“mm:ss:S”); jLabel1.setText(df.format(timeLeft)); if(timeLeft<=0) { timer.stop(); } } }; 结果是,用5秒钟完成5秒钟。 我将上面的代码与另一个Timer进行了比较 int timeLeft2=5; ActionListener countDown2=new ActionListener() { public void actionPerformed(ActionEvent e) { timeLeft2–; […]

java.util.Timer:它已被弃用了吗?

我读了一篇关于这个答案的评论以及关于不推荐使用java.util.Timer调度(抱歉,没有引用)的许多其他问题。 我真的希望不要因为我使用它作为在Java中安排事物的轻松方式(并且它工作得很好)。 但如果它被弃用了,我会去别处看看。 但是,快速查看1.6的API文档并没有说明它被弃用的任何内容。 在Sun的弃用列表中甚至没有提到它。 是否正式弃用*如果是这样,我应该使用什么呢? *另一方面, 如果它没有被弃用 , 那么人们是否可以停止对这种无辜且精彩实施的集合类进行诋毁?

Java计时器

我正在尝试使用计时器来安排应用程序中的重复事件。 但是,我希望能够实时调整事件触发的时间段(根据用户输入)。 例如: public class HelperTimer extends TimerTask { private Timer timer; //Default of 15 second between updates private int secondsToDelay = 15; public void setPeriod(int seconds) { this.secondsToDelay = seconds; long delay = 1000; // 1 second long period = 1000*secondsToDelay; // seconds if (timer != null) { timer.cancel(); } System.out.println(timer); timer = new […]

如何在Java Swing应用程序中添加简单的延迟?

我想知道如何在Java中的Swing应用程序中添加时间延迟,我使用了Thread.sleep(time) ,并且我也使用了SwingWorker但它不起作用。 这是我的代码的一部分: switch (state) { case ‘A’: if (charAux == ‘A’) { state = ‘B’; //Here’s where I’d like to add a time delay jLabel13.setForeground(Color.red); break; } else { //Here’s where I’d like to add a time delay jLabel12.setForeground(Color.red); break; } } 我希望你在使用SwingWorker时可以帮助我或解决我的疑虑。

Java Animate JLabel

所以我创建了一个基本的应用程序,我希望在屏幕底部有一个JLabel,从左下角开始,动画样式,在设定的时间内移动到右下角,在中心移动静态图像。 为此,我使用BorderLayout创建了一个带JPanel的JFrame。 有一个JLabel,其中ImageIcon添加到BorderLayout.CENTER,而一个JPanel添加到BorderLayout.SOUTH。 我的代码虽然草率而且非常漂亮,但是: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.BorderFactory; public class GameWindow extends JPanel{ private static JLabel mainWindow, arrowLabel, arrowBox; protected static JFrame frame; protected static JPanel arrows; public static int x = 600; public GameWindow(){ mainWindow = new JLabel(“Center”); […]