如何在单独的 J Frame Form 标签上显示 Java Timer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9721066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Java Timer on a separate J Frame Form label?
提问by Walid
i have a question , i have this timer code in java that when its executed it displays a count down timer on its own JFrame label, what i want to do is to display this timer on another JFrame form label without having to move the code to other classes.
我有一个问题,我在 Java 中有这个计时器代码,当它执行时,它在自己的 JFrame 标签上显示一个倒数计时器,我想要做的是在另一个 JFrame 表单标签上显示这个计时器,而不必将代码移动到其他班级。
I hope you can help me with this thanks lot guys .
我希望你能帮助我,谢谢很多人。
this is the code for the Timer class:
这是 Timer 类的代码:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TimerExample extends JFrame {
final JLabel label;
Timer countdownTimer;
int timeRemaining = 10;
public TimerExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
label = new JLabel(String.valueOf(timeRemaining), JLabel.CENTER);
getContentPane().add(label);
countdownTimer = new Timer(1000, new CountdownTimerListener());
setVisible(true);
countdownTimer.start();
}
class CountdownTimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (--timeRemaining > 0) {
label.setText(String.valueOf(timeRemaining));
} else {
label.setText("Time's up!");
countdownTimer.stop();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerExample();
}
});
}
}
thanks
谢谢
采纳答案by Rahul Borkar
Here it is,
这里是,
Following is my TestTimer class which accepts a JLabel as input
以下是我的 TestTimer 类,它接受 JLabel 作为输入
public class TestTimer {
private JLabel label;
Timer countdownTimer;
int timeRemaining = 10;
public TestTimer(JLabel passedLabel) {
countdownTimer = new Timer(1000, new CountdownTimerListener());
this.label = passedLabel;
countdownTimer.start();
}
class CountdownTimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (--timeRemaining > 0) {
label.setText(String.valueOf(timeRemaining));
} else {
label.setText("Time's up!");
countdownTimer.stop();
}
}
}
}
And here is another Main class which is actually extending a JFrame and showing a label in it,
这是另一个 Main 类,它实际上扩展了一个 JFrame 并在其中显示了一个标签,
public class TimerJFrame extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label;
public TimerJFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
label = new JLabel("10", JLabel.CENTER);
getContentPane().add(label);
new TestTimer(label);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerJFrame();
}
});
}
}
Second Code passes a created JLabel to first class and first class uses it to show timer.
第二个代码将创建的 JLabel 传递给第一类,第一类使用它来显示计时器。
回答by Rahul Borkar
You need to follow following steps,
您需要按照以下步骤操作,
- Modify TimerExample constructor to accept JLabel. And initialize JLabel of TimerExample class with passwd JLabel
- Pass JLabel from other JFrame class.
- Remove main method from this as it will not be required.
- 修改 TimerExample 构造函数以接受 JLabel。并使用 passwd JLabel 初始化 TimerExample 类的 JLabel
- 从其他 JFrame 类传递 JLabel。
- 从中删除 main 方法,因为它不需要。
Bu first step here, constructor will accept predefined JLabel from other classes and use those to display timer.
但是第一步,构造函数将接受来自其他类的预定义 JLabel 并使用它们来显示计时器。