Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Code to place a Jlabel on a Jpanel, at given coordinates...

Hello people,

This is definately the place for me, I've very new to programming!

I have some code that draws a circle on a drawingPanel, then a timer that will move the circle down a few pixels every second. That works fine but instead of drawing a circle at x = 150 y = 30 (or whatever) coordinates, I would like to place a Jlabel at those coordinates, that would then move down the screen like the circle did.

Basically, I just want to know what code to put on my "start" button to get the Jlabel on there!

I've read through these forums and have come up with nothing that can help me, probably because I need my hand holding! I don't understand much of this yet!

Any help would be very very much appreciated!

Here's what I have so far, just so you know:

private void cmdStartActionPerformed(java.awt.event.ActionEvent evt) {
x = 150;

y = 20;

int w = 30;

int h = 30;

panDraw.draw(cboShape.getSelectedIndex(), x, y, w, h);

txtRemaining.setText("until landing");

timer = new Timer();
timer.schedule(new LowerShip(),
0, //initial delay
1*1000); //subsequent rate


}

class LowerShip extends TimerTask {
int Height = 10;

public void run() {

for ( int Height=0; Height<10; Height++ ) {



x = 150;

y = y + 30;

int w = 30;

int h = 30;

panDraw.draw(cboShape.getSelectedIndex(), x, y, w, h);


txtTimeLeft.setText(" ");
txtTimeLeft.setText(9 - Height + " seconds");

txtHeightLeft.setText(" ");
txtHeightLeft.setText(67.5 - Height * 7.5 + " metres");

try {

Thread.sleep(1000);


} catch (InterruptedException e){

}
timer.cancel();
}
}


}
[2632 byte] By [JupiterBo] at [2007-11-11 7:46:14]
# 1 Re: Code to place a Jlabel on a Jpanel, at given coordinates...
Do this:

Set your drawing panel's layoutmanager to null, create your label, add it to the panel with a null constraint.

For every tick invoke its setbounds(x,y,width,height) method and do a plain repaint() for the panel.
Set the button's visible property as appropriate.
sjalle at 2007-11-11 22:37:26 >