Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

swing - Java Label Timer and Saving

Let me explain what I am trying to do.

I have two classes extending JFrame, the StartJFrame and TestingJFrame. In the main method, I start up a StartJFrame. It has a single button, start. When that is pressed, I have it set up to hide that frame and start up the TestingJFrame. Right now I don't have anything in the TestingJFrame.

In that screen, I want to have a label in the bottom right corner that is a timer, starting on 45 seconds and counting down to 0. I also need to have some code run every 10th of a second, and collect some data. There will be two buttons in the TestingJFrame, Yes and No. When one of them is pressed, it should stop the timer and save the information.

The data is basically just doubles. I am only going to be collecting data once per run of the program. I have a UserData class that holds some information about the test subject, and a list of doubles, it is added to every 10th of a second. I have a general idea how to save the data in java.

My question is, how should I set up the timer, so that it will count down from 45 seconds, and when it reaches 0 or the user presses the yes or no button it will call a function to save the data? I think I can handle the saving data part.

Sorry if this is really easy, I'm new to Java (from c#) and Swing has been confusing me a bit.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use javax.swing.Timer to setup your timer. You can have a look at the official tutorial too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...