java 计时器标记位_Java标签计时器和保存

这篇博客讨论了如何在Java Swing中创建一个从45秒倒计时的计时器,并在达到0或用户点击按钮时保存数据。作者提到了两个JFrame类,StartJFrame和TestingJFrame,后者包含计时器和数据收集功能。文章通过一个名为TimerTest01的示例程序展示了如何设置计时器,并提供了代码片段。然而,对于数据收集和保存的具体细节,作者表示需要进一步明确。
摘要由CSDN通过智能技术生成

让我解释一下我想做什么.

我有两个类扩展JFrame,StartJFrame和TestingJFrame.在main方法中,我启动了一个StartJFrame.它有一个按钮,开始.当按下它时,我将其设置为隐藏该帧并启动TestingJFrame.现在我在TestingJFrame中没有任何东西.

在那个屏幕上,我希望在右下角有一个标签,它是一个计时器,从45秒开始计数到0.我还需要每隔10秒运行一些代码,并收集一些数据.在TestingJFrame中将有两个按钮,是和否.当按下其中一个按钮时,它应该停止计时器并保存信息.

数据基本上只是双倍.我只会在每次运行程序时收集一次数据.我有一个UserData类,它包含有关测试主题的一些信息,以及一个双精度列表,它每10秒添加一次.我有一个大致的想法如何在java中保存数据.

我的问题是,我应该如何设置计时器,以便它从45秒开始倒计时,当它达到0或用户按下是或否按钮时它会调用一个函数来保存数据?我想我可以处理保存数据部分.

对不起,如果这很容易,我是Java的新手(来自c#),而Swing让我感到困惑.

解决方法:

第一部分(显示倒计时和停止计时器)相对容易……

public class TimerTest01 {

public static void main(String[] args) {

new TimerTest01();

}

public TimerTest01() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception ex) {

}

JFrame frame = new JFrame("Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

private JLabel label;

private Timer timer;

private long startTime;

public TestPane() {

setLayout(new GridLayout(0, 1));

label = new JLabel("...");

label.setHorizontalAlignment(JLabel.CENTER);

final JButton btn = new JButton("Start");

btn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (timer.isRunning()) {

timer.stop();

btn.setText("Start");

} else {

startTime = System.currentTimeMillis();

timer.restart();

btn.setText("Stop");

}

repaint();

}

});

add(label);

add(btn);

timer = new Timer(250, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

long endTime = (startTime + 45000);

long timeLeft = endTime - System.currentTimeMillis();

if (timeLeft < 0) {

timer.stop();

label.setText("Expired");

btn.setText("Start");

} else {

label.setText(Long.toString(timeLeft / 1000));

}

repaint();

}

});

}

}

}

你问的第二部分是含糊不清,可靠地为你提供答案.数据来自哪里?怎么收?

标签:java,swing,timer,jframe

来源: https://codeday.me/bug/20190928/1825640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值