* Created by IntelliJ IDEA.
* User: Knight_Sun
* Date: 2008-5-20
* Time: 9:19:09
* To change this template use File | Settings | File Templates.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class Showtime extends JFrame implements ActionListener {
private JButton timeBox;
private JTextField timeField;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日 a h時m分s秒");
public Showtime(String title) throws HeadlessException {
super(title);
init();
doLay();
handleEvents();
}
private void init() {
timeBox = new JButton("刷新時間");
timeField = new JTextField(20);
}
private void doLay() {
Container container = getContentPane();
container.add(timeBox, BorderLayout.NORTH);
container.add(timeField, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void handleEvents() {
timeBox.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Timestamp nowtime=new Timestamp(System.currentTimeMillis());
timeField.setText(dateFormat.format(nowtime.getTime()));
}
public static void main(String[] args) {
new Showtime("Show Time");
}
}
看看是不是妳要的效果,壹個按鈕,點壹下就顯示最新時間.