Tag: timer

如何使定时器倒计时与进度条一起?

如何使进度条慢慢降低时间限制? class GamePanel extends JPanel implements MouseListener, ActionListener { private JButton quit; private JButton q; private Font loadFont; public GamePanel() { setBackground(Color.blue); // sets background color this.setLayout(null); quit = new JButton(“Quit”); quit.addActionListener(this); quit.setBounds(550, 700, 100, 30); this.add(quit); q = new JButton(“Questions”); q.addActionListener(this); q.setBounds(100, 100, 120, 30); this.add(q); loadFont = new Font(“Serif”, Font.PLAIN, 30); } public […]

如何在特定时间安排任务?

我有一个java调度程序的问题,我的实际需要是我必须在特定时间启动我的进程,我会在某个时间停止,我可以在特定时间启动我的进程但我不能在某个时间停止我的进程,如何指定进程在调度程序中运行多长时间(这里我不会放入),任何人都有建议。 import java.util.Timer; import java.util.TimerTask; import java.text.SimpleDateFormat; import java.util.*; public class Timer { public static void main(String[] args) throws Exception { Date timeToRun = new Date(System.currentTimeMillis()); System.out.println(timeToRun); Timer timer1 = new Timer(); timer1.schedule(new TimerTask() { public void run() { //here i call another method } } }, timeToRun);//her i specify my start time } }

Java调度程序完全独立于系统时间的变化

使用Java Timer,然后切换到ScheduledExecutorService,但我的问题没有修复。 在系统时间更改之前安排的任务(通过ntpd)不会在指定的延迟时执行。 没有任何日志记录相同:(。 在64位linux上使用我的目标中的jre 1.6.0_26 64位。 更新: ScheduledExecutorService在Windows上运行正常。 问题仅出在运行64位JVM的基于64位Linux的系统上。 它在运行32位JVM的64位linux上运行良好……很奇怪。 在任何博客上都没有找到任何相同的参考。 IBM的JAVA SDK也存在同样的问题(ibm-java-sdk-7.0-0.0-x86_64-archive.bin)。 我已经提交了针对JDK 7139684的缺陷,它被接受但已被关闭并标记为6900441的副本 。 请投票给它,如果你觉得它的价值得到修复…我不知道为什么它已经修复了几年以上 以下是我用来测试此问题的示例代码: package test; import java.io.IOException; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @author yogesh * */ public class TimerCheck implements Runnable { ScheduledExecutorService worker; public TimerCheck(ScheduledExecutorService worker) { super(); this.worker = worker; this.worker.schedule(this, 1, […]

Java标签计时器和保存

让我解释一下我想做什么。 我有两个类扩展JFrame , StartJFrame和TestingJFrame 。 在main方法中,我启动了一个StartJFrame 。 它有一个按钮,开始。 当按下它时,我将其设置为隐藏该帧并启动TestingJFrame 。 现在我在T estingJFrame没有任何东西。 在那个屏幕中,我希望在右下角有一个标签,它是一个计时器,从45秒开始计数到0。我还需要每隔10秒运行一些代码,并收集一些数据。 在TestingJFrame中将有两个按钮,是和否。当按下其中一个按钮时,它应该停止计时器并保存信息。 数据基本上只是双倍。 每次运行程序时,我只会收集一次数据。 我有一个UserData类,它包含有关测试主题的一些信息,以及一个双精度列表,它每10秒添加一次。 我有一个大致的想法如何在java中保存数据。 我的问题是,我应该如何设置计时器,使其从45秒开始倒计时,当它达到0或用户按下是或否按钮时,它会调用一个函数来保存数据? 我想我可以处理保存数据部分。 对不起,如果这很容易,我是Java的新手(来自c#),而Swing让我感到困惑。

Java Swing:延迟后更改文本

基本上,我有这个游戏,一旦猜到正确的答案,它开始一个新的游戏用新词。 我想显示Correct! 但三秒后,将其更改为空字符串。 我怎么做? 我的尝试: if (anagram.isCorrect(userInput.getText())) { anagram = new Anagram(); answer.setText(“CORRECT!”); word.setText(anagram.getRandomScrambledWord()); this.repaint(); try { Thread.currentThread().sleep(3000); } catch (Exception e) { } answer.setText(“”); } else { answer.setForeground(Color.pink); answer.setText(“INCORRECT!”); } 编辑: 我的解决方案 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (anagram.isCorrect(userInput.getText())) { answer.setText(“CORRECT!”); ActionListener taskPerformer = new ActionListener() { […]

添加计时器并显示标签文本

我有一个JLabel。 最初我已经为它设置了一些文字。 JLabel j = new JLabel(); // set width height and position j.setText(“Hello”); 我只希望文本Hello显示5秒钟。 然后我想要显示文本再见。 我怎么能这样做 我的工作; 但我知道这是错的,因为它一次只执行1个if-else块。 我想我需要一个计时器或一个计数器。 让这个工作。 帮帮我 ? long time = System.currentTimeMillis(); if ( time < (time+5000)) { // adding 5 sec to the time j.setText("Hello"); } else { j.setText("Bye"); }

在调用方法和JOptionPane之后允许代码继续执行所需的定时器或其他想法

我需要一种方法来允许我的程序在调用此方法后继续运行代码。 目前,它等待半小时,获取信息,将其存储到对象WeatherCard,并显示它,然后重复。 但它挂在JOptionPane上。 我需要一种方法来使程序保持在JOptionPane下面或在大约10秒后关闭窗格。 我目前还不确定如何使用我的代码 public void printWeatherCard(WeatherCard w, JFrame controlFrame) throws MalformedURLException, IOException{ /* Displays a dialog box containing the temperature and location */ BufferedImage img = ImageIO.read(new URL(w.imgSrc)); ImageIcon icon = new ImageIcon(img); JOptionPane.showMessageDialog(controlFrame, “It is currently ” + w.currentTemp + ” \u00B0 F in ” + w.location.city + “, ” + w.location.state […]

使用Swing Timer更新标签

我在使用这段代码时遇到了一些麻烦。 我正在用一个随机数启动一个计时器,我希望每秒都用倒计时更新一个JLabel。 但是我还没弄明白怎么做,因为定时器触发的唯一监听器就在它的末尾(我知道)。 这是代码: int i = getTimer(maxWait); te1 = new Timer(i, this); label.setText(i+””); te1.start(); … public int getTimer(int max){ Random generator = new Random(); int i = generator.nextInt(max); return i*1000; } … public void actionPerformed(ActionEvent ev){ if(ev.getSource() == te1){ label.setText(i+””); te1.stop(); } }

如何使用Timer类调用方法,执行某些操作,重置计时器,重复?

我是一名Java初学者,并且已经为这个问题提供了各种解决方案,并且让我自己有点打结。 我已经尝试过使用Threads,然后发现了这个Timer类,并且到目前为止还没有成功。 如果您可以使用main方法发布可执行代码,那么我可以看到它正常工作并从那里开始玩,那将是很棒的。 启动计划 调用doSomething() 生成随机数并设置那么长的Timer。 当Timer关闭时,再次调用doSomething() 。 可能使用这个: http : //docs.oracle.com/javase/6/docs/api/java/util/Timer.html