如何在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 为例子。 引用初始化问题 如何让工具栏随着frame大小的变化而变化 登陆后如何处理登陆窗口 论坛中的人有在北京工作的吗 运行一个关于ArrayList的例子,编译出错,不知是什么原因,请赐教。 一个关于JOptionPane的问题??? 与数据库的连接所报的错误 求教 eclipse怎么运行?是不是需要另外安装jdk? 請問怎樣把一個OutputStream 轉換成 InputStream啊, 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 为例子。