[英]update time on button click
I want to have it so when i click the button the time will update. 我想要它,所以当我单击按钮时,时间将更新。 Can i have a hint or help into how i would accomplish this, if what i am wanting is at all possible. 如果我想要的是一切可能的话,我能提示或帮助我如何实现这一目标。
I have been struggling to do this for the last couple of days so any help would be greatly appreciated. 最近几天,我一直在努力做到这一点,因此,我们将不胜感激。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static javax.swing.BoxLayout.X_AXIS;
import static javax.swing.BoxLayout.Y_AXIS;
public class CountdownNBA2K16 extends JFrame implements ActionListener{
JLabel displayDate ;
JLabel displayDateS ;
JLabel displayDateM ;
JLabel displayDateH ;
JLabel displayDateD ;
JPanel Mili = new JPanel();
JPanel Sec = new JPanel();
JPanel minute = new JPanel();
JPanel hours = new JPanel();
JPanel day = new JPanel();
JPanel cd = new JPanel();
JPanel combine = new JPanel();
JButton button = new JButton("update time");
Calendar releaseDate;
Calendar todayDate;
long diff;
long diffSec;
long days;
long secDays;
long secs;
long min;
long hour;
int SECONDS_IN_A_DAY = 24 * 60 * 60;
String ms;
String s;
String m;
String h;
String d;
public CountdownNBA2K16() {
super("COUNTDOWN TO NBA2K16");//sets title of app
setLayout(new FlowLayout());
button.addActionListener(this);
releaseDate = Calendar.getInstance();
releaseDate.set(Calendar.DAY_OF_MONTH,25);
releaseDate.set(Calendar.MONTH,8);
releaseDate.set(Calendar.YEAR,2015);
todayDate = Calendar.getInstance();
diff = releaseDate.getTimeInMillis() - todayDate.getTimeInMillis();
diffSec = diff/1000;
days = diffSec / SECONDS_IN_A_DAY;
secDays = diffSec % SECONDS_IN_A_DAY;
secs = secDays % 60;
min = (secDays%60) %60;
hour = (secDays/3600);
s = String.valueOf(secs);
displayDateS = new JLabel("seconds:"+s + "");
m = String.valueOf(min);
displayDateM = new JLabel("minutes:"+m +"");
h = String.valueOf(hour);
displayDateH = new JLabel("hours:"+h+"");
d = String.valueOf(days);
displayDateD = new JLabel("days:"+d+"");
Sec.add(displayDateS);
minute.add(displayDateM);
hours.add(displayDateH);
day.add(displayDateD);
cd.add(day);
cd.add(hours);
cd.add(minute);
cd.add(Sec);
cd.setLayout( new BoxLayout(cd ,X_AXIS));
combine.add(button);
combine.add(cd);
combine.setLayout( new BoxLayout(combine ,Y_AXIS));
add(combine);
pack();
setSize(300,300);
setResizable(false);//doesnt let user resize the gui
setLocationRelativeTo(null);//sets location relative to nothing
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//makes the applictaion close when the gui is closed
}
@Override
public void actionPerformed(ActionEvent e) {
displayDateS.getText();
displayDateM.getText();
displayDateH.getText();
displayDateD.getText();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CountdownNBA2K16().setVisible(true);
}
});
}
}
Start by using something like Java 8's Time API or Joda-Time to calculate the duration. 首先使用Java 8的Time API或Joda-Time之类的东西来计算持续时间。
Separate the logic for the duration calculation to a separate method, this way you can call it from both the constructor and the actionPerformed
method. 将持续时间计算的逻辑分为一个单独的方法,这样您就可以从构造函数和actionPerformed
方法中调用它。
Something like... 就像是...
protected void updateDuration() {
System.out.println(LocalDateTime.now());
Duration d = Duration.between(releaseDate.atStartOfDay(), LocalDateTime.now());
System.out.println(d.toMillis());
long days = d.toDays();
d = d.minusDays(days);
long hours = d.toHours();
d = d.minusHours(hours);
long mins = d.toMinutes();
d = d.minusMinutes(mins);
long seconds = d.toMillis() / 1000;
displayDateS.setText("Seconds: " + seconds);
displayDateM.setText("Minutes: " + mins);
displayDateH.setText("Hours: " + hours);
displayDateD.setText("Days: " + days);
}
for example (just make sure you've created the labels before you call it) 例如(只需在调用之前确保已创建标签)
You might also like to have a look at How to use Swing Timers to update the output automatically. 您可能还想看看如何使用Swing计时器自动更新输出。
Analogy just. 打个比方。 Because still I am unable to post comment that is why I answered you. 因为我仍然无法发表评论,所以我才回答你。 In the actionPerformed method just set the text of the labels to what you want. 在actionPerformed方法中,只需将标签的文本设置为所需的文本即可。
public class CountdownNBA2K16 extends JFrame implements ActionListener {
JLabel displayTime;
JButton button = new JButton("Update time");
public CountdownNBA2K16() {
super("Time");
setLayout(new FlowLayout());
button.addActionListener(this);
displayTime = new JLabel("Just to see the updates ..." + System.currentTimeMillis());
setSize(300, 300);
add(displayTime);
add(button);
pack();
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
displayTime.setText("Just to see the updates ..." + System.currentTimeMillis());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CountdownNBA2K16().setVisible(true);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.