public class Foter extends JPanel{
}
来一个 程序。。学习学习
java 继承用Jpanel 能实现一个 获取系统时间吗? 自动更新
答案:2 悬赏:50 手机版
解决时间 2021-02-26 23:08
- 提问者网友:呐年旧曙光
- 2021-02-26 14:47
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-02-26 15:14
使用线程,线程中的run方法中安排一个无限循环,循环每秒sleep一下,循环每执行一次,就更改一下JPanel中的一个JLabel组件就行了。
示例:
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WorkTime extends JPanel{
private JLabel timeLabel = null;
private int hour;
public WorkTime() {
this(18);
}
public WorkTime(int hour) {
this.hour = hour;
timeLabel = new JLabel();
add(timeLabel);
//启动线程
new TimeThread().start();
}
public int getHour() {
return hour;
}
class TimeThread extends Thread{
//默认下午6点下班
public TimeThread() {
}
@Override
public void run() {
//获取当天下班时间
//获取当前时间
Calendar instance = Calendar.getInstance();
instance.setTime(new Date());
instance.set(Calendar.HOUR_OF_DAY, hour);
instance.set(Calendar.MINUTE, 0);
instance.set(Calendar.SECOND, 0);
long timeInMillis = instance.getTimeInMillis();
for(;;){
long currentTimeMillis = System.currentTimeMillis();
long time = timeInMillis-currentTimeMillis;
//小时
//分
double t = time/(1000*60*60D);
int minite = (int) ((t-Math.floor(t))*60);
int second = (int) ((time/1000)%60D);
//System.out.println("离下班还有:"+Math.floor(t)+"小时"+minite+"分"+second+"秒");
timeLabel.setText("离下班还有:"+Math.floor(t)+"小时"+minite+"分"+second+"秒");
if(t<0){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame();
WorkTime wt = new WorkTime(23);
frame.setTitle("今天"+wt.getHour()+"点下班,等待下班中……");
frame.add(wt);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
示例:
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WorkTime extends JPanel{
private JLabel timeLabel = null;
private int hour;
public WorkTime() {
this(18);
}
public WorkTime(int hour) {
this.hour = hour;
timeLabel = new JLabel();
add(timeLabel);
//启动线程
new TimeThread().start();
}
public int getHour() {
return hour;
}
class TimeThread extends Thread{
//默认下午6点下班
public TimeThread() {
}
@Override
public void run() {
//获取当天下班时间
//获取当前时间
Calendar instance = Calendar.getInstance();
instance.setTime(new Date());
instance.set(Calendar.HOUR_OF_DAY, hour);
instance.set(Calendar.MINUTE, 0);
instance.set(Calendar.SECOND, 0);
long timeInMillis = instance.getTimeInMillis();
for(;;){
long currentTimeMillis = System.currentTimeMillis();
long time = timeInMillis-currentTimeMillis;
//小时
//分
double t = time/(1000*60*60D);
int minite = (int) ((t-Math.floor(t))*60);
int second = (int) ((time/1000)%60D);
//System.out.println("离下班还有:"+Math.floor(t)+"小时"+minite+"分"+second+"秒");
timeLabel.setText("离下班还有:"+Math.floor(t)+"小时"+minite+"分"+second+"秒");
if(t<0){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame();
WorkTime wt = new WorkTime(23);
frame.setTitle("今天"+wt.getHour()+"点下班,等待下班中……");
frame.add(wt);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-02-26 15:22
System.currentTimeMillis()
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯