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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jan 2006
    Posts
    1

    Red face 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:

    Code:
        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();
                }
            }
            
            
        }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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.
    Last edited by sjalle; 01-18-2006 at 07:31 AM.
    eschew obfuscation

Similar Threads

  1. VS.Net and Code Behind
    By Chaitanya Marvici in forum ASP.NET
    Replies: 6
    Last Post: 07-21-2003, 09:15 AM
  2. Where Will I Place the code
    By Nelson in forum VB Classic
    Replies: 0
    Last Post: 08-02-2002, 09:33 AM
  3. Replies: 0
    Last Post: 04-11-2002, 06:48 PM
  4. .NET equals Efficiency
    By Kevin in forum .NET
    Replies: 150
    Last Post: 03-04-2002, 05:40 PM
  5. Good Editorial by Russell Jones
    By Robert G in forum .NET
    Replies: 84
    Last Post: 02-08-2001, 02:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center