Sleepers
can someone explain to me what sleepers are and what they are used for?
i'm trying to use one in my program and i don't think it works.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass Title
{
JFrame f1;
JPanel p1;
JLabel Story;
Container c1;
boolean end;
public Title()
{
f1 =new JFrame("HOBO");
f1.setSize(750,650);
c1 = f1.getContentPane();
p1 =new JPanel();
end =false;
Thread runner =new Thread();
while (end=false)
{
try
{
runner.sleep(2);
}
catch (InterruptedException e){}
for (int i=0; i<5; i++)
{
if (i==1)
Story =new JLabel("A Long time ago....");
if (i==2)
Story =new JLabel("there was a hobo...");
if (i==3)
Story =new JLabel("who was old...");
if (i==4)
Story =new JLabel("and homeless...");
if (i==5)
Story =new JLabel("and kicked people's butts for money");
p1.add(Story);
}
end =true;
}
c1.add(p1);
f1.show();
}
}
did i put it in the wrong place?
Thread.sleep is a static method. when you invoke it, you put the thread that it's called from to sleep, not the thread you try and invoke it on. always call it like this
Thread.sleep(1000);
rather than
Thread mythread = new Thread();
mythread.sleep(); // will cause THIS thread to sleep, not mythread
a common mistake
but there's nothing here called a "sleeper" - a unique mistake
other than that am i using it right?because i want the text to show up one at a time.
no. you can't put a thread to sleep from within another thread, the thread has to put itself to sleep, using Thread.sleep()
so do i take it out of where i have it?
> so do i take it out of where i have it?you can't work that out yourself?
well read the code, i need the text to show up individually, and i was told to us sleep
> well read the code, i need the text to show up> individually, and i was told to us sleepyou have everything you need right here in this thread, if you stop and think for a minute. I like to help people, but I don't feel like I'm helping if I just spoon-feed them
so ur saying i don't need the thread?or do i put it in the for() statement?honestly i don't know what threads are.Message was edited by: ichbinsterben
there is no for in the code. actually, looking at the code, I can't really see why you've got that thread there, it doesn't do anything. it's unclear what you're trying to do, to be honest
its there.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Title
{
JFrame f1;
JPanel p1;
JLabel Story;
Container c1;
boolean end;
public Title()
{
f1 = new JFrame("HOBO");
f1.setSize(750,650);
c1 = f1.getContentPane();
p1 = new JPanel();
end = false;
while (end=false)
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e) {}
for (int i=0; i<5; i++)
{
if (i==1)
Story = new JLabel("A Long time ago....");
if (i==2)
Story = new JLabel("there was a hobo...");
if (i==3)
Story = new JLabel("who was old...");
if (i==4)
Story = new JLabel("and homeless...");
if (i==5)
Story = new JLabel("and kicked people's butts for money");
p1.add(Story);
}
end = true;
}
c1.add(p1);
f1.show();
}
}
i need those JLabels (i'll change them to individuals eventually) show up one at a time
Message was edited by:
ichbinsterben
Message was edited by:
ichbinsterben
> honestly i don't know what threads are. http://java.sun.com/docs/books/tutorial/essential/concurrency/
for(int i=0;i<5;i++)....Message was edited by: ichbinsterben
> for(int i=0;i<5;i++)....> > Message was edited by: > ichbinsterbenlol I do apologise. I saw the while loop and that was it!before you go stampeding off using threads, you need to understand them a bit. follow hunter's link
hunter's link takes me to a "Lesson" on how a computer processes. unless that's what threads are
> hunter's link takes me to a "Lesson" on how a> computer processes. unless that's what threads areThere's more to it than just the intro paragraph. See the links on the left? See the Next link at the bottom?
This program won't work anyway because the statement "end=false" will allways return false and therefore the while loop will never run. You wanted "end == false" as the first statement assigns instead of comparing.
> This program won't work anyway because the statement
> "end=false" will allways return false and therefore
> the while loop will never run. You wanted "end ==
> false" as the first statement assigns instead of
> comparing.
good spot. or even better, just while(!end)
Also this will never work as you want it to since you only show the GUI at the end and all of the operations on the JLabel are done before the GUI is shown.
If you want the same JLabel to change, you should do story.setText(), if not, you should have different JLabel's.
The Thread.sleep(1000) should be inside the for loop if you want to pause between each execution of for loop statement, ie:
for (int i=0; i<5; i++)
{
Thread.sleep(1000);
if (i==1)
Story = new JLabel("A Long time ago....");
if (i==2)
Story = new JLabel("there was a hobo...");
if (i==3)
Story = new JLabel("who was old...");
if (i==4)
Story = new JLabel("and homeless...");
if (i==5)
Story = new JLabel("and kicked people's butts for money");
}
So if you replace Story = new JLabel with Story.setText and put the for loop after the GUI is shown you will have something closer to what your looking for I believe.
phsabo thanx it helped a lot.