Results 1 to 7 of 7
- 03-19-2011, 09:57 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 14
- Rep Power
- 0
adding label to the anel of other class
hai...
plz help...i wanna add a label of one class to the panel of other class... this label will be relocated always using thread.sleep to show its movement... so im keeping that in separate file..but the panel needs no change... how to accomplish this?... anybdy plz help..
thanxs..
-
in the class of the label:
in the other class:Java Code:private JLabel jlabel1; public JLabel myLabel = jlabel1;
Java Code:mainClass.myLabel.setText("hello");Last edited by ozzyman; 03-20-2011 at 01:02 AM.
- 03-21-2011, 02:24 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 14
- Rep Power
- 0
thank u...
- 03-23-2011, 03:36 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 14
- Rep Power
- 0
/* hai, this is a class file where the paint method will draw the boundary and the label is also included in a particular location using the launch function.....
below is the function which is calling a thread where that thread must change the location of the label in a sequence and show its movement using sleep....
i dnt no wats the problem with dis....der is no movement of that label... y is it so?..
plz help..
*/
public void launch() {
Icon icon = new ImageIcon("D://img.gif");
label=new JLabel();
label.setIcon(icon);
panel.add(label);
label.setBounds(180, 40, 15, 41);
move s=new move();
}
// move class
import javax.swing.*;
public class move implements Runnable {
Thread th;
public move()
{
th.start();
}
public void run()
{
try
{
field f=new field();
f.player1.setBounds(280, 40, 15, 41);
th.sleep(1000);
f.player1.setBounds(290, 30, 15, 41);
} catch (Exception e) {
System.out.println(e);
}
}
thank u...
- 03-23-2011, 03:42 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 14
- Rep Power
- 0
im sorry....iv changed that label name....
below is my code.. sorry for the inconvenience.....plz check dis code and help me...
Java Code:/* hai, this is a class file where the paint method will draw the boundary and the label is also included in a particular location using the launch function..... below is the function which is calling a thread where that thread must change the location of the label in a sequence and show its movement using sleep.... i dnt no wats the problem with dis....der is no movement of that label... y is it so?.. plz help.. */ //boundary class... public JLabel label; public void launch() { Icon icon = new ImageIcon("D://img.gif"); label=new JLabel(); label.setIcon(icon); panel.add(label); label.setBounds(180, 40, 15, 41); move s=new move(); }
Java Code:// move class import javax.swing.*; public class move implements Runnable { Thread th; public move() { th.start(); } public void run() { try { boundary b=new boundary(); b.label.setBounds(280, 40, 15, 41); th.sleep(1000); b.label.setBounds(290, 30, 15, 41); } catch (Exception e) { System.out.println(e); } } ....
thanxLast edited by Fubarable; 03-23-2011 at 03:55 AM. Reason: Moderator Edit: code tags added
-
You don't really want outside classes to fiddle directly with the fields of another class such as in your code where outside classes directly fiddle with the JLabels' bounds properties. What if later you represent your player in a different way and the setBounds makes no sense? Rather, I suggest that you create a separate Player class, one that knows its position, that has methods to get the position:
Java Code:public Point getPosition() { // your code here }
and a method to set the position
and then inside of the Player class you change the position of your JLabel with its image, but you hide this detail from outside classes.Java Code:public void moveToPosition(Point p) { // your code here }
- 03-25-2011, 08:12 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
adding a actionListener but not using inner class
By hariza in forum AWT / SwingReplies: 2Last Post: 10-08-2010, 07:24 AM -
Adding 2 Implements to the class
By jboy in forum New To JavaReplies: 2Last Post: 10-23-2009, 05:19 AM -
can we change label from action class?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-28-2009, 02:12 PM -
Problems adding an external class
By jan2321 in forum EclipseReplies: 2Last Post: 11-06-2008, 10:34 AM -
Adding a new class to a jar file
By Raul Menendez in forum New To JavaReplies: 2Last Post: 08-21-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks