Tag: timer

问题:创建一个非常准确的Swing Timer

为了使SwingTimer准确,我喜欢@Tony Docherty On CR建议的逻辑和示例。 这是链接 。 为了突出显示给定的单词,一次又一次,总会有几微秒的延迟。 如果我有话要强调说:“你好怎么样”,每个单词的值分别是(延迟):200,300,400 ms,那么计时器的实际时间总是更多。 说而不是200毫秒,它需要216毫秒。 像这样,如果我有很多话……最后,额外的延迟是显而易见的。 我必须强调每个字母说:’h”””””0’每个应该得到200 /长度(即5)=约40毫秒。 设置每个字母后的延迟。 我的逻辑是,在开始这个过程之前,先把当前时间说成startTime 。 另外,计算totalDelay ,即totalDelay + = delay / .length()。 现在检查条件:( startTime+totalDelay-System.currentTime )如果这是-ve,这意味着时间消耗更多,所以跳过这封信。 检查直到有一个正延迟。这意味着我要添加时间直到现在,并用它开始时的过程所花费的时间差来检查它。 这可能导致跳过以突出显示字母。 但有些事情是错的。 什么,我很难搞清楚。 这可能是循环事情的一些问题。 我已经看到它进入循环(检查时间是否为-ve)只是两次。 但事实并非如此。 而且我也不确定是否会设置下一次延迟。 有任何想法吗? 这是一个SSCCE: import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextPane; import javax.swing.SwingUtilities; import javax.swing.Timer; import […]

使用java.util.timer与Quartz进行调度的优缺点?

我必须编写一个执行一系列任务的应用程序: 任务每天0200时运行一次。 任务将在凌晨4点运行一次 从0003小时开始以15分钟的间隔运行任务 从0005时开始以15分钟的间隔运行任务 使用普通java.util.timer Vs有什么优缺点。 石英这个? 我还应该考虑其他替代方案吗?

JFrame组件之间的触发延迟

我想展示如何使用JFrame直观地进行合并排序。 我想做的是让后续的JLabel可见一段时间延迟。 我尝试了很多方法,但所有这些都出现在同一时刻,没有中间延迟。 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // jLabel1.setVisible(false); jLabel2.setVisible(false); jLabel3.setVisible(false); jLabel4.setVisible(false); jLabel5.setVisible(false); jLabel6.setVisible(false); jLabel7.setVisible(false); final Timer t=new Timer((4000), null); final int delay=2000; final ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { jLabel1.setVisible(true); t.getDelay(); jLabel2.setVisible(true); t.setDelay(3000); jLabel3.setVisible(true); t.setDelay(2000); jLabel4.setVisible(true); t.setDelay(2000); jLabel5.setVisible(true); t.setDelay(2000); jLabel6.setVisible(true); t.setDelay(2000); […]

Java swing计时器仅工作一次,然后keyEvents快速连续触发 – 按住键

所以我把它设置为KeyEvents和计时器的测试。 第一次按下右箭头键时,事件将等待5秒,就像定时器设置为,然后打印KeyPressed 。 然而,在第一次打印之后, KeyPressed将快速连续打印,就像我按下键时收集的KeyEvents的长队列一样。我不希望所有按住右箭头键的额外按键导致。 我想按住右箭头键,每5秒钟只接收一次println 。 任何帮助是极大的赞赏。 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class GameBoard extends JPanel { public Ninja ninja; public GameBoard() { addKeyListener(new TAdapter()); setFocusable(true); setBackground(Color.BLACK); setDoubleBuffered(true); ninja = new Ninja(); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(ninja.getImage(), 20,20,null); } […]

@ Singleton,@ Start,@ PostConstruct不适用于EJB3.1和Glassfishv3.0.1

我有这个设置的问题,我甚至无法查看日志。 这是我的@Singleton @Startup EJB: @Singleton @Startup @DependsOn(“SchedulerEJB”) public class SchedulerStartUp { private static Logger log = Logger.getLogger(SchedulerStartUp.class); @EJB SchedulerEJB schedEJB; @PostConstruct public void atStartup() { log.info(“startUp”) System.out.println(“startUp”); schedEJB.cancelTimer(“EBlastScheduler”); schedEJB.createTimer(“*/1”, “*”, “*”); } } SchedulerEJB : @Stateless public class SchedulerEJB { @Resource TimerService timerService; public cancelTimer(String timerInfo){/*…*/} public createTimer(String sec, String min, String hour) {/*…*/} @Timeout […]

如何在Java中延迟MouseOver?

我有一个简短的问题,我希望有人可以帮助我。 请查看以下代码段: public void mouseEntered(MouseEvent e){ //wait 2 seconds. //if no other mouseEntered-event occurs, execute the following line //otherwise restart, counting the 2 seconds. foo(); } 有人可以帮我解决这个问题吗? 我想实现像ToolTip这样的行为:你用鼠标进入一个区域。 如果您的鼠标停留在该位置,请执行某些操作。

如何在不使用JProgressBar的情况下制作进度条?

我正在考虑将进度条放入我正在制作的游戏中,从5到20之间的随机数减少到0。这是我的代码到目前为止: 包软件; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.Timer; public class CountDownProgressBar extends JPanel { RandomValues randomNum; int timeValue; Timer timer; int length, newLength; public CountDownProgressBar() { timer = new Timer(24, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = timeValue; i > 0; –i) { […]

Java中的javascript setTimeout相当于什么?

我需要实现一个函数,在单击按钮60秒后运行。 请帮助,我使用了Timer类,但我认为这不是最好的方法。

如何使用Swing Timer延迟进度条的加载

我需要找到一种方法来使用带有进度条的Swing Timer。 我尝试使用Thread.sleep(),但是当我使用它时它崩溃了应用程序。 有什么方法可以使用Swing Timer而不是Sleep()? public void piiEros(int dist) { Pii pii = new Pii(); pii.setVisible(true); for(int pc = 0;100 > pc; pc++) { try { Thread.sleep(dist/100); } catch (InterruptedException ex) { Logger.getLogger(Trav.class.getName()).log(Level.SEVERE, null, ex); } pii.pg.setValue(pc); } pii.dispose(); o.Eros(); } 注意:Pii是一个带有Progress Bar的课程。 Dist是它加载的速度。 Trav是该方法所在的类.Pc代表%,在条上显示了多少。 o.Eros打开另一个GUI。

在Swing应用程序内部的javax.swing.Timer与java.util.Timer

最好在swing应用程序中使用javax.swing.Timer而不是使用java.util.Timer ? 例如: Timer timer = new Timer(1000, e -> label.setText(new Date().toString())); timer.setCoalesce(true); timer.setRepeats(true); timer.setInitialDelay(0); timer.start(); 要么 new java.util.Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { label.setText(new Date().toString()); } }, 0, 1000); 这两个有什么区别吗?