Results 1 to 6 of 6
  1. #1
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Smile help me with JFrame and JLabel

    hello!
    I have been trying, with some friends help, for a couple of days now to make a "countdown" program. The main point of this program was to see a time running down in a window. I have tried to make my own window with JFrame and successed. (I have done a new label and everything) But now the problem is how to get the label updated so I can see the time "running" down. I have tested with "setText" from the main platform JLabel but I have not succesed. Maybe the foult is that I use a Calendar platform from java.util to count how much time it's left.
    I realy need help with this problem.
    Ty... Michal:)

  2. #2
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    here is my program:


    package minapplikation;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    public class countdown {

    public static void main(String [] arg)
    throws Exception{
    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
    if (ids.length == 0)
    System.exit(0);

    SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    JLabel emptyLabel = new JLabel();

    int a = calendar.get(Calendar.DAY_OF_YEAR);
    int dagarkvar = 0;
    if(a>50)
    dagarkvar = 402-a;
    else
    dagarkvar = 46-a;

    int b = calendar.get(Calendar.HOUR) + 9;

    int c = calendar.get(calendar.MINUTE);

    int d = calendar.get(Calendar.SECOND);

    int timmarkvar = 24 - b;
    int minkvar = 60 - c;
    int sekkvar = 60 - d;

    JFrame frame = new JFrame("LOST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    for(int i=0; i<=3; i++){
    emptyLabel.setText(" tid kvar till LOST: " + dagarkvar + " dagar, " + timmarkvar +
    " timmar, " + minkvar + " minuter, " + sekkvar + " sekunder ");

    SwingUtilities.updateComponentTreeUI(emptyLabel);
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }

    }
    }
    Last edited by michcio; 11-19-2007 at 11:14 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class CountdownRx implements Runnable {
        JLabel emptyLabel;
        SimpleTimeZone pdt;
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public void run() {
            while(true) {
                try {
                    Thread.sleep(500);
                } catch(InterruptedException e) {
                    break;
                }
                updateLabel();
            }
        }
    
        private void updateLabel() {
            Calendar calendar = new GregorianCalendar(pdt);
            Date trialTime = new Date();
    
            int a = calendar.get(Calendar.DAY_OF_YEAR);
            int dagarkvar = 0;
            if(a>50)
                dagarkvar = 402-a;
            else
                dagarkvar = 46-a;
    
            int b = calendar.get(Calendar.HOUR) + 9;
    
            int c = calendar.get(calendar.MINUTE);
    
            int d = calendar.get(Calendar.SECOND);
    
            int timmarkvar = 24 - b;
            int minkvar = 60 - c;
            int sekkvar = 60 - d;
    
            emptyLabel.setText(" tid kvar till LOST: " + dagarkvar +
                               " dagar, "   + timmarkvar +
                               " timmar, "  + minkvar +
                               " minuter, " + sekkvar + " sekunder ");
        }
    
        private JLabel getLabel() {
            String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
            if (ids.length == 0)
                System.exit(0);
            pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
            emptyLabel = new JLabel();
            return emptyLabel;
        }
    
        public static void main(String[] arg) throws Exception{ 
            CountdownRx test = new CountdownRx();
            JFrame frame = new JFrame("LOST");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(test.getLabel(), BorderLayout.CENTER);
            frame.setSize(400,125);
            frame.setVisible(true);
            test.start();
        }
    }

  4. #4
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    hum I have tried to compile the prog. that you have send. It compiled without errors but when I'm trying to run it one appears:

    java.lang.NoClassDefFoundError: minapplikation/CountdownRx
    Exception in thread "main"
    Java Result: 1

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    minapplikation/CountdownRx
    I didn't put a package statement in the class I posted.

  6. #6
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    oh how stupid of me I missed it... and it works now ty very very much for help D

Similar Threads

  1. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 05:16 AM
  2. Problems with JLabel 2
    By geork in forum New To Java
    Replies: 2
    Last Post: 02-03-2008, 09:40 PM
  3. How to use JLabel with html
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 05:43 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 03:19 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
  •