Jlabel showing both old and new numbers

前端 未结 2 1180
攒了一身酷
攒了一身酷 2020-12-22 07:52

My Jlabel shows not only present (tNow - tStart) % 10000, but all the past ones, how do I fix that?

import javax.swing.; import java.awt.;

public cl

相关标签:
2条回答
  • 2020-12-22 08:27

    You are creating a new JLabel and adding it to the frame everytime add_time is called and the timing conditions are met. This means you end up with the JLabel's appearing [Removed:next to each other, as if the new time value was appended. e.g. JLabel1+JLabel2+JLabel3] on top of the old values.

    You should declare the label variable once and then update its text value, rather than using new JLabel in add_time.

    0 讨论(0)
  • 2020-12-22 08:43
    frame.add(label);
    

    Don't keep adding components to the frame. Get rid of that statement.

    Instead you need to update the existing label on the frame:

    So you need to make a couple of changes:

    1. Create the JLabel and add it to the frame in the main() method.

    2. Then the addTime(...) method needs to be changed to pass the label to the method intead of the frame. Then you can just use the setText() method of the label to update the label.

    Actually this is poorly designed code. You should not be using static methods for all your code. Here is an example that shows how to better structure your code and how to use a Timer to update the text on a label:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;
    
    public class TimerTime extends JPanel implements ActionListener
    {
        private JLabel timeLabel;
        private int count = 0;
    
        public TimerTime()
        {
            timeLabel = new JLabel( new Date().toString() );
            add( timeLabel );
    
            Timer timer = new Timer(1000, this);
            timer.setInitialDelay(1);
            timer.start();
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //System.out.println(e.getSource());
            timeLabel.setText( new Date().toString() );
    //      timeLabel.setText( String.valueOf(System.currentTimeMillis() ) );
            count++;
    
            if (count == 10)
            {
                Timer timer = (Timer)e.getSource();
                timer.stop();
            }
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("TimerTime");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new TimerTime() );
            frame.setLocationByPlatform( true );
            frame.pack();
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    I suggest you update to code to use something like that instead of your current code.

    0 讨论(0)
提交回复
热议问题