如何在Button或者输入框中添加系统时间 如何在Button或者输入框中添加系统时间 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么意思?让Button 上显示的文字是 系统时间 ? 需要引进 java.util.Calendar 和 java.text.SimpleDateFormat 这两个类:Calendar rightNow = Calendar.getInstance();SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");String sysDatetime = fmt.format(rightNow.getTime()); JButton button=new JButton(); button.add(sysDatetime); JButton 有继承 add方法吗?又不是Container。 import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;import javax.swing.JButton;import javax.swing.JFrame;/** * @author bzwm * */public class TimeButton extends JButton implements ActionListener{ java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); public static void main(String args[]){ JFrame jf = new JFrame(); TimeButton b = new TimeButton(); javax.swing.Timer t = new javax.swing.Timer(1000, b); t.start(); jf.add(b); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(300, 300); } protected void paintComponent(Graphics g){ this.setText(df.format(new Date())); super.paintComponent(g); } public void actionPerformed(ActionEvent e) { this.repaint(); }}以JButton 为例子。 java swing中jtextfield中如何控制键盘的输入 xml解析问题!急 序列化问题 linux下java解析zip流里的中文名文件 java如何获取class文件所在本机路径,非URL 请教-关于java编写播放音频文件 [ 求助 ]菜鸟提问了,帮个忙吧~ http接口调用问题 eclipse里如何把某个目录添加到classpath里啊? 大家请进来做一道SCJP认证试题!!!并说明你答案的理由! ToolTipManger是什么类??有什么作用? java动态数组问题
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddhhmmss");
String sysDatetime = fmt.format(rightNow.getTime()); JButton button=new JButton();
button.add(sysDatetime);
又不是Container。
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;/**
* @author bzwm
*
*/
public class TimeButton extends JButton implements ActionListener{
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static void main(String args[]){
JFrame jf = new JFrame();
TimeButton b = new TimeButton();
javax.swing.Timer t = new javax.swing.Timer(1000, b);
t.start(); jf.add(b);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
}
protected void paintComponent(Graphics g){
this.setText(df.format(new Date()));
super.paintComponent(g);
} public void actionPerformed(ActionEvent e) {
this.repaint();
}
}
以JButton 为例子。