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(); }} JPanel在被载入后为什么不会自动重绘 代码没错,编译没错,最后还是错了,特悬赏50分 菜鸟问题:怎么写操作界面? 跪求...... 急急急!JAVA访问数据库出错! java技术交流站 群号:2303735 在线等怎么把Char c='9',转换成整型数9.为什么我import java.math.*,还是不能识别round这样的函数? java连接数据库时怎样用ADO,就是不用在odbc中建立数据源而直接连接数据库? jre与jdk区别何在? 告急文书 控制台无法执行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();
}
}