要求显示一个frame,上有一个button,每点击一下该button,就system.out.println点击时的时间,这道题目是否要用到ActionEvent里的getWhen(),可是这样显示出来的时间是一串数字,不是年,月,日,时,分秒啊。
题目程序的一部分如下:
Use the following class as your main class:
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
}
请给出类ClickListener的代码。
题目程序的一部分如下:
Use the following class as your main class:
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
}
请给出类ClickListener的代码。
SimpleDateFormat formatter = new SimpleDateFormat("....");
public void actionPerformed(ActionEvent e){
System.out.println(formatter.format(new Date(e.getWhen())));
}
}