thread-sleep

Java Swing JButton Time Delays (Flicker)

十年热恋 提交于 2019-11-27 09:53:13
I am trying to make my JButton flicker red for this game I am creating. All the solutions on this website suggest using a thread and putting it to sleep or using a timer, however, the pause allays seems to come after the color change Here is my code: Color cb = board[Y1][X1].getBackground(); board[Y1][X1].setBackground(Color.RED); //Pause board[Y1][X1].setBackground(cb); If I put a thread and put it to sleep on line 3 and comment out line 4 the pause will come before the JButton is turned red. (Note board is just a 2D array of JButtons) There are any number reasons why this might be occurring

Problems with using Thread.Sleep for short times

独自空忆成欢 提交于 2019-11-27 07:55:00
问题 I have an app with 2 threads (now), but it seems that function Thread.Sleep() doesn't work very good. It sleeps threads but it takes much more time (for example- I want to sleep it for 5ms and it sleeps for 0,3s or more). Here is code: int vlakien = 2; Thread[] vlakna; vlakna = new Thread[vlakien]; for (int i = 0; i < vlakien; i++) { try { vlakna[i] = new Thread(new ThreadStart(utok)); vlakna[i].Start(); } } private void utok() { //some code Thread.Sleep(5); //some code } Also I tried to

how to update a jLabel every time with a while loop with a delay

淺唱寂寞╮ 提交于 2019-11-27 06:26:33
问题 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int count = jSlider1.getValue(); int delay = jSlider2.getValue(); int valueOfSlider = jSlider2.getValue(); int valueOfSlider2 = jSlider1.getValue(); while (count > 0) { count--; String count2 = ""+count; jLabel3.setText(count2); try {Thread.sleep(delay); } catch (InterruptedException ie) { } } It will eventually show the final number on the jLabel but it does not incrementally update the number. any help 回答1: Swing is

SwingWorker does not update JProgressBar without Thread.sleep() in custom dialog panel

会有一股神秘感。 提交于 2019-11-27 05:37:04
I have a SwingWorker class which loads a text file and slices it to chunks for further processing. This is the SwingWorker class: public class ConverterWorker extends SwingWorker<String, String> { private final File f; private final JLabel label; public ConverterWorker(File f, JLabel label) { this.f = f; this.label = label; } @Override protected String doInBackground() throws Exception { NMTMain.convertableData = getDataSets(f); if(!NMTMain.convertableData.isEmpty()) { return "Done"; } else { publish("Failed to load the file!"); return "Failed"; } } @Override public void done() { try { label

How to create a delay in Swing

若如初见. 提交于 2019-11-27 05:34:57
I made a blackjack game, and I want the AI player to pause between taking cards. I tried simply using Thread.sleep(x), but that makes it freeze until the AI player is done taking all of his cards. I know that Swing is not thread safe, so I looked at Timers, but I could not understand how I could use one for this. Here is my current code: while (JB.total < 21) { try { Thread.sleep(1000); } catch (InterruptedException ex) { System.out.println("Oh noes!"); } switch (getJBTable(JB.total, JB.aces > 0)) { case 0: JB.hit(); break; case 1: break done; case 2: JB.hit(); JB.bet *= 2; break done; } } BTW

Simple animation using Thread.sleep() in ActionListener

为君一笑 提交于 2019-11-26 22:10:22
问题 I'm having trouble with this code I am using to create a roulette wheel. The goal is to spin the wheel when I click the "SPIN!" button. I have done this by creating a for loop that should change the status of the wheel from true to false, which changes the orientation. This, when done fast enough, should create the illusion of movement. THE PROBLEM I AM HAVING : is that my wheel is only repainting after the whole for loop is done, despite my placement of the repaint(). So, it only spins one

Can I improve the resolution of Thread.Sleep?

我只是一个虾纸丫 提交于 2019-11-26 22:02:27
问题 Thread.Sleep() resolution varies from 1 to 15.6ms Given this console app: class Program { static void Main() { int outer = 100; int inner = 100; Stopwatch sw = new Stopwatch(); for (int j = 0; j < outer; j++) { int i; sw.Restart(); for (i = 0; i < inner; i++) Thread.Sleep(1); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } } } I expected the output to be 100 numbers close to 100. Instead, I get something like this: 99 99 99 100 99 99 99 106 106 99 99 99 100 100 99 99 99 99 101 99 99

Swing - Thread.sleep() stop JTextField.setText() working [duplicate]

你离开我真会死。 提交于 2019-11-26 19:10:06
Possible Duplicate: using sleep() for a single thread I'm having issues with JTextField.setText() when using Thread.sleep(). This is for a basic calculator I'm making. When the input in the input field is not of the correct format I want "INPUT ERROR" to appear in the output field for 5 seconds and then for it to be cleared. The setText() method did work when I just set the text once to "INPUT ERROR" and by printing out the text in between I found it does work with both that and the setText("") one after the other. The problem arises when I put the Thread.sleep() between them. Here's a SSCCE

Thread.Sleep() without freezing the UI

孤人 提交于 2019-11-26 17:57:15
First off, I am a beginner in C# and I would like to make this: class2.method_79(null, RoomItem_0, num, num2, 0, false, true, true); System.Threading.Thread.Sleep(250); class2.method_79(null, RoomItem_0, num, num4, 0, false, true, true); System.Threading.Thread.Sleep(300); class2.method_79(null, RoomItem_0, num, num6, 0, false, true, true); But this solution freezes the UI, how could I make the second event occur 250ms after the first etc without freezing the UI? The simplest way to use sleep without freezing the UI thread is to make your method asynchronous. To make your method asynchronous

How to put delay before doing an operation in WPF

别等时光非礼了梦想. 提交于 2019-11-26 16:34:36
I tried to use the below code to make a 2 second delay before navigating to the next window. But the thread is invoking first and the textblock gets displayed for a microsecond and landed into the next page. I heard a dispatcher would do that. Here is my snippet: tbkLabel.Text = "two mins delay"; Thread.Sleep(2000); Page2 _page2 = new Page2(); _page2.Show(); Phil The call to Thread.Sleep is blocking the UI thread. You need to wait asynchronously. Method 1: use a DispatcherTimer tbkLabel.Text = "two seconds delay"; var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; timer