在Netbeans JFrame窗体中怎么设计倒计时
答案:2 悬赏:10 手机版
解决时间 2021-11-18 03:40
- 提问者网友:了了无期
- 2021-11-17 06:08
在Netbeans JFrame窗体中怎么设计倒计时
最佳答案
- 五星知识达人网友:痴妹与他
- 2021-11-17 07:23
给你个,在窗体上显示时间的例子。你可以直接运行起来。楼主肯定会的。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TimerFrame extends JFrame{
private static final long serialVersionUID = 3974977196233834289L;
private JLabel label=new JLabel();
private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Timer timer=new Timer();
public void launchFrame(){
this.setSize(300, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
label.setBounds(140, 180, 200, 10);
this.add(label);
timer.schedule(new TimerFrame.TimeTask(), new Date(), 1000);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TimerFrame frame=new TimerFrame();
frame.launchFrame();
}
});
}
class TimeTask extends TimerTask{
@Override
public void run() {
Date date=new Date();
label.setText(format.format(date));
}
}
}
看看有没有什么启示。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TimerFrame extends JFrame{
private static final long serialVersionUID = 3974977196233834289L;
private JLabel label=new JLabel();
private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Timer timer=new Timer();
public void launchFrame(){
this.setSize(300, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
label.setBounds(140, 180, 200, 10);
this.add(label);
timer.schedule(new TimerFrame.TimeTask(), new Date(), 1000);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TimerFrame frame=new TimerFrame();
frame.launchFrame();
}
});
}
class TimeTask extends TimerTask{
@Override
public void run() {
Date date=new Date();
label.setText(format.format(date));
}
}
}
看看有没有什么启示。
全部回答
- 1楼网友:渡鹤影
- 2021-11-17 08:09
都是使用线程来跑的,在线程里作个循环,每个循环sleep(1000),再setText值并repaint窗体就能实现
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯