aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes swing help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "swing help" Watch "swing help" New topic
Author

swing help

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
Hello swing experts,
I need your help.In the below given programme i have created two frames, one frame creating the other only when i hit the button labelled nitin after which i am taken to the next frame on which i am tring to do some sort of animation.IF i remove that animation code i can see the second frame if i don't i can't see the second frame.The lines which is giving me problems are written at the end of the code.Code from Line number 44 onwards are creating problems.Please throw some light on it or rectify the error in my code.I waiting for it eagerly.
1) import java.awt.*;
2) import java.awt.event.*;
3) import javax.swing.*;
4) public class nitin extends JFrame implements ActionListener
5) {
6) JButton b;
7) public nitin()
8) {
9) b=new JButton("nitin");
10) getContentPane().add(b);
11) setSize(400,400);
12) setVisible(true);
13) b.addActionListener(this);
14) }
15) public void actionPerformed(ActionEvent t)
16) {
17) if(t.getSource()==b)
18) {
19) new s();
20) }
21) }
22) public static void main(String[]args)
23) {
24) nitin n=new nitin();
25) }
26) }
27) class s extends JFrame
28) {
29) JLabel l;
30) String[]args={"nitin","sharma","r u alright"};
31) public s()
32) {
33) l=new JLabel();
34) getContentPane().add(l);
35) setSize(400,400);
36) setVisible(true);
37) try
38) {
39) Thread.sleep(30000);
40) }
41) catch(InterruptedException r)
42) {
43) }
44) for(int i=0;i<args.length;i++)//this and the lines following this are creating problem.>
{
l.setText(args[i]);
if(i==2)
{
i=0;
}
}
}
}
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
Hello
Somebody some up with something yaar.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Nitin,
You are not far enough into the Java book yet! You need to get the the thread part of the book. In order to create animation you need to create a thread that lets other threads (such as painting) do their job at the same time. Your for loop is hogging all the CPU and the painting is not happening.
To make use of threads just make your s class implement Runnable and place your loop into the run method. I also took the liberty to make your label switching more elegant with the use of an instance variable.

Enjoy,
Manfred.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
Hi
manfred u are right that i still have along way to go in java beacuse threading is still one of my wekest are's in which i scored just 71 percent marks.I am very much aware of the method which u have shown me,infact, i have tried that method few day's back and it worked very fine then i thought of using some other way just to improve my logic.Now, Have a look at the foolowing code ,code is same.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class lapore extends JFrame
{
JLabel l;
String args[]={"nitin","sharma","You are right"};
public lapore()
{
l=new JLabel();
getContentPane().add(l);
setSize(400,400);
setVisible(true);

for(int i=0;i<args.length;i++)>
{
l.setText(args[i]);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
}
if(i==2)
{
i=0;
}
}
}
public static void main(String[]args)
{
lapore l=new lapore();
}
}
Just compile and see output.I haven't used any thread's in the code.The question is that the given code run's perfectly fine when it is executed, if i put the same code on the second frame then it does not run as u have pointed in your explanation.What exactly is the thing which is not casuing problem when run on a single frame.?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: swing help
 
Similar Threads
Accessing JFrame contents
help regarding batch file
SWING GUI ADVICE,PLEASE
SWING GUI ADVICE,PLEASE
How to close a window and open another one