简体   繁体   中英

Keeping constant track of time in java

So I want to make a program that constantly keeps track of time while other functions of the code can run. For my code specifically, I have 4 JButtons and 4 JLabels. Once I click a JButton, I want the corresponding JLabel to update its text every couple of seconds (let's say 5 seconds). But while the code running, I want to be able to select a different button at any given time to update its corresponding JLabel and stop the one before. I've tried a few things, but every time I run into two problems:

  1. The JLabel doesn't update every 5 seconds.
  2. I can't click a different JButton while the another JButton is clicked.

They both have to do with the fact that my code goes through an infinite loop once a JButton is clicked, but I'm not sure how I can go around doing this.

I'm new to coding so my knowledge of the terminology is quite limited. Any help will be appreciated.

Here's the code for what happens when the button is clicked. (I changed the code a few times here and there to try to fix on my own, but couldn't so currently it just gives me a stackoverflow error.) :

package maingame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;

public class StatButtonListener implements ActionListener
{
private JButton _button;
private ArrayList<JLabel> _jlList;
private static String _clicked;
public StatButtonListener(JButton button, ArrayList<JLabel> jlList)
{
    _button = button;
    _jlList = jlList; 
}

@Override
public void actionPerformed(ActionEvent arg0)
{
    _clicked = _button.getText(); 
    Long value = null;
    JLabel valueLabel = null;
    // to stop an infinite loop
    for(int i=0; i < 8; i++)
    {
        if(_button.getText().equals(_jlList.get(i).getText()))
        {
            valueLabel = _jlList.get(i+1);
            value = Long.parseLong(valueLabel.getText());
        }
    }
    double startTime = System.currentTimeMillis()/1000;
    updateStat(value, valueLabel, startTime);


}

public void updateStat(Long value, JLabel valueLabel, double startTime)
{

    if(!(value == null || valueLabel == null))
    {   
        if(_clicked.equals(_button.getText()))
        {
            double endTime=System.currentTimeMillis()/1000;
            if((endTime-startTime) >= 1)
            {
                startTime = System.currentTimeMillis()/1000;
                value = value + 3;
                valueLabel.setText(value.toString());
            }
            updateStat(value, valueLabel, startTime);
        }
    }
}

}

You can do this using the aforementioned Swing-Timers:

http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

This invokes actionPerformed() once its done. You can then update whatever you want to update and restart() it.


Or you can do this using a new Thread:

http://de.wikibooks.org/wiki/Java_Standard:_Threads

This runs completely on its own and does not block the main program - and might need a handle of the objects you want to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM