public class MyClock extends JFrame implements ActionListener,Runnable
{
private JLabel L1;
private JButton bt;
public void run()
{
try{
while(true){
Date a = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
L1.setText(f.format(a));
Thread.sleep(1000);
}
}
catch(Exception ee){
}
}
public void actionPerformed(ActionEvent e)
{
new Thread(this).start();
}
public MyClock()
{
this.setBounds(100,100,300,100);
Container cc = this.getContentPane();
cc.setLayout(new FlowLayout());
L1 = new JLabel("xxxxx");
bt = new JButton("闹钟");
cc.add(L1);
cc.add(bt);
bt.addActionListener(this);
}
public static void main(String[] args)
{
new MyClock().setVisible(true);
}
}