
public class ShowCurrentTime {
public static void main(String args[]){
//Obtain the total milliseconds since midnight,Jan 1,1970
long totalMilliseconds=System.currentTimeMillis()
//Obtain the total seconds since in midnight,Jan 1, 1970
long totalSeconds=totalMilliseconds/1000
//compute the current second in the minute in the hour
long currentSecond=totalSeconds%60
//Obtain the total minutes
long totalMinutes=totalSeconds/60
//compute the current minute in the hour
long currentMinute=totalMinutes%60
//Obtain the total hours
long totalHours=totalMinutes/60
//compute the current hour
long currentHour=totalHours%24
//display results
System.out.println("Current time is "+currentHour+" : "+currentMinute+" : "+currentSecond+" GMT")
}
}
你是想是实现,在某一列可以显示时间, 并且点击时间后面的图标可以显示时间选择器,然后选择时间吧..
1,写一个MyModel继承与AbstractTableModel: 用于当数据模型, 填充数据,删除数据等
2,写一个MyRenderer实现TableCellRenderer 主要实现getTableCellRendererComponent方法. 用于返回一个外观, 这就是日期列的渲染器. 可以渲染该列看起来的样子,比如看起来就是一个文本框,里面的文字是xx年xx月xx日
3,写一个MyEditor实现TableCellEditor接口, 也需要重写getTableCellEditorComponent等方法, 这就是日期列的编辑器, 就是当点击日期列时, 处于编辑状态时的状态,显示日期选择器 选择好时间后, 文本框的文字变成了cc年cc月cc日
比如可以把性别列渲染成单选按钮组, 兴趣列渲染成复选框....如下
显示实时时间的JPanel1.重写JPanel的paint方法,在JPanel上画上实时时间的字符串。(下边的例子是用的这个方法)
2.在JPanel加个label,在上面设置实时时间的字符串。
--------------------------------------------------------------------------
import java.awt.Graphics
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Timer
import java.util.TimerTask
import javax.swing.JFrame
import javax.swing.JPanel
public class ShowTimeApp extends JFrame {
// 时间格式
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
public ShowTimeApp() {
// 设置显示窗口的参数
setDefaultCloseOperation(EXIT_ON_CLOSE)
// 居中显示
setLocationRelativeTo(null)
// 窗口大小
setSize(200, 150)
// 窗口不可改变大小
setResizable(false)
// 添加时间的JPanel
getContentPane().add(new TimePanel())
// 显示
setVisible(true)
// 设置时间变化的任务
Timer timer = new Timer()
timer.schedule(new ShowTime(), new Date(), 1000)
}
class ShowTime extends TimerTask {
public void run() {
// 刷新
repaint()
}
}
class TimePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g)
// 显示时间
g.drawString(sdf.format(new Date()), 10, 10)
}
}
public static void main(String[] args) {
new ShowTimeApp()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)