4个回答
2016-12-27 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
不难的,就是开一个定时器,实时获取当前时间就行,下面代码可以参考:
import javax.swing.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.awt.*;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;
public class Time extends JFrame {
JLabel systemTime;
public Time(){
setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(20,20,200,200);
cp.setLayout(null);
cp.add(systemTime);
setSize(800,800);
setVisible(true); //add by me
}
JButton logoutbutton;
public static void main(String[] args){
TimerTask task = new TimerTask() {
Time time = new Time();
public void run() {
//comment by me
/* Calendar st = Calendar.getInstance();
String sdate;
Date ddate;
ddate=st.getTime();*/
//add by me below:
String sdate;
sdate=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date());
time. systemTime.setText(sdate);
}
};
Timer t = new Timer();
t.scheduleAtFixedRate(task, new Date(), 1000);
}
}
import javax.swing.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.awt.*;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;
public class Time extends JFrame {
JLabel systemTime;
public Time(){
setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(20,20,200,200);
cp.setLayout(null);
cp.add(systemTime);
setSize(800,800);
setVisible(true); //add by me
}
JButton logoutbutton;
public static void main(String[] args){
TimerTask task = new TimerTask() {
Time time = new Time();
public void run() {
//comment by me
/* Calendar st = Calendar.getInstance();
String sdate;
Date ddate;
ddate=st.getTime();*/
//add by me below:
String sdate;
sdate=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date());
time. systemTime.setText(sdate);
}
};
Timer t = new Timer();
t.scheduleAtFixedRate(task, new Date(), 1000);
}
}
展开全部
看看是否符合要求
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyGui {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
class MyFrame extends JFrame{
JTextField jf2 ;
public MyFrame(){
this.setTitle("Test");
this.setSize(400, 400);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
JTextField jf1 = new JTextField();
jf2 = new JTextField(" yyyy-MM-dd HH:mm:ss ");
jf1.setBorder(BorderFactory.createLoweredBevelBorder());
jf2.setBorder(BorderFactory.createLoweredBevelBorder());
jf2.setHorizontalAlignment(JTextField.RIGHT);
jf1.setEditable(false);
jf2.setEditable(false);
p3.add(jf1);
p3.add(jf2,BorderLayout.EAST);
p3.setBorder(BorderFactory.createEtchedBorder());
this.add(p3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t = new Timer();
t.schedule(new MyTask(), 1000,1000);
}
class MyTask extends TimerTask{
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(new Date());
jf2.setText(s);
}
}
}
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyGui {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
class MyFrame extends JFrame{
JTextField jf2 ;
public MyFrame(){
this.setTitle("Test");
this.setSize(400, 400);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
JTextField jf1 = new JTextField();
jf2 = new JTextField(" yyyy-MM-dd HH:mm:ss ");
jf1.setBorder(BorderFactory.createLoweredBevelBorder());
jf2.setBorder(BorderFactory.createLoweredBevelBorder());
jf2.setHorizontalAlignment(JTextField.RIGHT);
jf1.setEditable(false);
jf2.setEditable(false);
p3.add(jf1);
p3.add(jf2,BorderLayout.EAST);
p3.setBorder(BorderFactory.createEtchedBorder());
this.add(p3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t = new Timer();
t.schedule(new MyTask(), 1000,1000);
}
class MyTask extends TimerTask{
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(new Date());
jf2.setText(s);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;
public class Time extends JFrame {
JLabel systemTime;
public Time(){
setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(10,600,200,200);
cp.setLayout(null);
cp.add(systemTime);
setSize(800,800);
setVisible(true); //add by me
}
JButton logoutbutton;
public static void main(String[] args){
TimerTask task = new TimerTask() {
Time time = new Time();
public void run() {
String sdate;
sdate=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date());
time. systemTime.setText(sdate);
}
};
Timer t = new Timer();
t.scheduleAtFixedRate(task, new Date(), 1000);
}
}
import java.awt.*;
import java.util.Timer;
import java.util.*;
import java.text.*;
import java.util.Date;
public class Time extends JFrame {
JLabel systemTime;
public Time(){
setTitle("sbgaowenchao");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
systemTime = new JLabel("系统时间:");
Container cp = getContentPane();
systemTime.setBounds(10,600,200,200);
cp.setLayout(null);
cp.add(systemTime);
setSize(800,800);
setVisible(true); //add by me
}
JButton logoutbutton;
public static void main(String[] args){
TimerTask task = new TimerTask() {
Time time = new Time();
public void run() {
String sdate;
sdate=(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date());
time. systemTime.setText(sdate);
}
};
Timer t = new Timer();
t.scheduleAtFixedRate(task, new Date(), 1000);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个用java不好吧,建议你写js,仅供参考:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript 时间显示</title>
</head>
<body>
<span id=localtime></span>
<script type="text/javascript">
function showLocale(objD)
{
var str,colorhead,colorfoot;
var yy = objD.getYear();
if(yy<1900) yy = yy+1900;
var MM = objD.getMonth()+1;
if(MM<10) MM = '0' + MM;
var dd = objD.getDate();
if(dd<10) dd = '0' + dd;
var hh = objD.getHours();
if(hh<10) hh = '0' + hh;
var mm = objD.getMinutes();
if(mm<10) mm = '0' + mm;
var ss = objD.getSeconds();
if(ss<10) ss = '0' + ss;
var ww = objD.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="星期日";
if (ww==1) ww="星期一";
if (ww==2) ww="星期二";
if (ww==3) ww="星期三";
if (ww==4) ww="星期四";
if (ww==5) ww="星期五";
if (ww==6) ww="星期六";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;
return(str);
}
function tick()
{
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
window.setTimeout("tick()", 1000);
}
tick();
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript 时间显示</title>
</head>
<body>
<span id=localtime></span>
<script type="text/javascript">
function showLocale(objD)
{
var str,colorhead,colorfoot;
var yy = objD.getYear();
if(yy<1900) yy = yy+1900;
var MM = objD.getMonth()+1;
if(MM<10) MM = '0' + MM;
var dd = objD.getDate();
if(dd<10) dd = '0' + dd;
var hh = objD.getHours();
if(hh<10) hh = '0' + hh;
var mm = objD.getMinutes();
if(mm<10) mm = '0' + mm;
var ss = objD.getSeconds();
if(ss<10) ss = '0' + ss;
var ww = objD.getDay();
if ( ww==0 ) colorhead="<font color=\"#FF0000\">";
if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";
if ( ww==6 ) colorhead="<font color=\"#008000\">";
if (ww==0) ww="星期日";
if (ww==1) ww="星期一";
if (ww==2) ww="星期二";
if (ww==3) ww="星期三";
if (ww==4) ww="星期四";
if (ww==5) ww="星期五";
if (ww==6) ww="星期六";
colorfoot="</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;
return(str);
}
function tick()
{
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
window.setTimeout("tick()", 1000);
}
tick();
</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询