javax.swing.timer

Using SwingWorker and Timer to display time on a label?

自闭症网瘾萝莉.ら 提交于 2020-01-29 08:29:24
问题 I want to have a clock showing current time and refreshes every second. The code I am using is: int timeDelay = 1000; ActionListener time; time = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { timeLabel.setText(DateTimeUtil.getTime()); /*timeLabel is a JLabel to display time, getTime() is samll static methos to return formatted String of current time */ } }; SwingWorker timeWorker = new SwingWorker() { @Override protected Object doInBackground() throws

JFrame that has multiple layers

筅森魡賤 提交于 2020-01-03 21:18:05
问题 I have a window that has two layers: a static background and a foreground that contains moving objects. My idea is to draw the background just once (because it's not going to change), so I make the changing panel transparent and add it on top of the static background. Here is the code for this: public static void main(String[] args) { JPanel changingPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(100, 100,

JFrame that has multiple layers

戏子无情 提交于 2020-01-03 21:16:39
问题 I have a window that has two layers: a static background and a foreground that contains moving objects. My idea is to draw the background just once (because it's not going to change), so I make the changing panel transparent and add it on top of the static background. Here is the code for this: public static void main(String[] args) { JPanel changingPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(100, 100,

Enable to stop timer

こ雲淡風輕ζ 提交于 2019-12-25 17:44:30
问题 I have an application that I want to refresh screen in some periods.User will select a task in combo, if job is active task, a timer will be started.So only one timer exists. I am trying to stop timer when a new task is selected from combo.Here is stop timer function. It seems it does not work for some cases.But I could not catch the case. Although timer is not null and is Running, it does not stop.it works at the begining of program, after a while does not work. public void stopTimer() {

repeating animation using java.swingTimer

心已入冬 提交于 2019-12-25 07:10:20
问题 I am writing the code of Game bean machine (Galton box) using animated graphics. I am using swing.Timer to make the red ball move, by letting the Timer object take an ActionListener. Here is the output: The red ball here is moving randomly : http://imgur.com/J9Xq0t5&HIv1udp And here it reaches the bottom: http://imgur.com/J9Xq0t5&HIv1udp#1 The problem is that I want to make several balls moving not only 1 but I actually cannot, I tried to put the Timer object in a loop but it only speeded up

Use javax.swing.Timer to make countdown timer in Java [duplicate]

雨燕双飞 提交于 2019-12-20 03:21:48
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Stop timer with conditional only works first time? I'm very confused as to how to make a timer using the swing and not util timer. I am making a game where users have to answer questions in a 30 second time limit. I have a PlayFrame, where the time is shown, and a method inside PlayFrame called startTimer which contains all the timer stuff. public static void startTimer() { int elapsedSeconds = 0; javax.swing

Can't understand Java Swing Timers. How do I make a 1 time delay?

妖精的绣舞 提交于 2019-12-12 06:38:35
问题 I need a one-time pause in this program for what I'm trying to do. I display some text in a Java Swing JFrame , repaint shows it, I wait 1.5 sec, then I change the text. Basically, I started with this: statusLabel.setText(s); appFrame.repaint(); Thread.sleep(1500); statusLabel.setText(y); appFrame.repaint(); But this wasn't working. Thread.sleep() would invoke before repaint had finished, meaning s would never be shown. I read a lot of places that you're not supposed to use Thread.sleep() in

move a ball one after another

这一生的挚爱 提交于 2019-12-12 02:16:44
问题 I am writing a code of game bean machine(Galton box), its mechanism that one ball fall and move randomly until it get to one of the slots (randomly) and then another ball comes doing the same thing and so on, the problem is that all balls fall at the same time which something I do not want, I want each ball to fall when the current one finishes its travel..can anyone helps me ! Thanks.. here is the code: import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util