java 怎样在JTextField中显示当前时间 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、写内容 setText2、当前时间 SimpleDateFormat + Calendar类 = 你所需要格式的当前时间。 我给你说一个经常用的方法。来显示时间的。public class Time implements Runnable{ private JLabel label; public Time(JLabel label){ this.label=label; } public void run(){ while(true){ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=dateFormat.format(new Date()); label.setText("当前时间:"+time); label.setFont(new Font("微软雅黑",Font.BOLD,16)); try{ Thread.sleep(1000); }catch(Exception e){ label.setText("系统时间暂时无法显示"); } } }}我法的Demo里边是用JLabel来显示的。LZ可以拿去测试下,这只是一个单纯动态显示当前时间的类 final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");final String currentTime = formatter.format(new Date());final JLabel label = new JLabel(currentTime);javax.swing.Timer timer = new javax.swing.Timer(1000,new ActionListener(){ @Override void actionPerformed(e:ActionEvent) { label.setText(formatter.format(new Date())); } });timer.start() mport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JFrame;import javax.swing.JTextField;import javax.swing.Timer;public class Swing_test extends JFrame{ public Swing_test(){ this.setSize(300,150); this.setLocation(300,200); this.setDefaultCloseOperation(EXIT_ON_CLOSE); final JTextField jt=new JTextField(); jt.setSize(150, 20); Timer time=new Timer(1000,new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { jt.setText(DateToString(new Date())); } }); time.start(); this.add(jt); this.setVisible(true); } public static String DateToString(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = format.format(date); return dateStr; } public static void main(String[] ar){ new Swing_test(); }} 菜鸟求救!!! 如何在myeclipse中使用ultraedit jfreechart的问题 起点坐标和间隔相关的设定 输出流的小问题!!!!!马上给分!!! 如何返回对象数组!? ***java.io中的管道可以用来做什么?有什么好处? 当我在cmd窗体中执行java文件时出现意想不到的错误!请高手指正!(在线等待!) 请问Jbuild4.0版本的ID是多少? 用JB的朋友来看看…… 谁见过这样的问题? 控制台无法执行Java?? java swing 问题
2、当前时间
SimpleDateFormat + Calendar类 = 你所需要格式的当前时间。
public class Time implements Runnable{
private JLabel label;
public Time(JLabel label){
this.label=label;
}
public void run(){
while(true){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=dateFormat.format(new Date());
label.setText("当前时间:"+time);
label.setFont(new Font("微软雅黑",Font.BOLD,16));
try{
Thread.sleep(1000);
}catch(Exception e){
label.setText("系统时间暂时无法显示");
}
}
}
}
我法的Demo里边是用JLabel来显示的。LZ可以拿去测试下,这只是一个单纯动态显示当前时间的类
final String currentTime = formatter.format(new Date());
final JLabel label = new JLabel(currentTime);
javax.swing.Timer timer = new javax.swing.Timer(1000,new ActionListener(){
@Override void actionPerformed(e:ActionEvent)
{
label.setText(formatter.format(new Date()));
}
});
timer.start()
mport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Timer;
public class Swing_test extends JFrame{
public Swing_test(){
this.setSize(300,150);
this.setLocation(300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
final JTextField jt=new JTextField();
jt.setSize(150, 20);
Timer time=new Timer(1000,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
jt.setText(DateToString(new Date()));
}
});
time.start();
this.add(jt);
this.setVisible(true);
}
public static String DateToString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = format.format(date);
return dateStr;
}
public static void main(String[] ar){
new Swing_test();
}
}