回覆列表
  • 1 # 使用者2330099431147

    取得時間用

    java.util.Calendar或者java.util.Date

    在控制元件上控制時間用

    javax.swing.Timer

    下面給個例子:

    import java.awt.BorderLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.util.Calendar;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.Timer;

    public class ClockTest extends JFrame {

    public ClockTest() {

    super("Timer Demo");

    setSize(300, 100);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    ClockLabel clock = new ClockLabel();

    getContentPane().add(clock, BorderLayout.NORTH);

    }

    public static void main(String args[]) {

    ClockTest ct = new ClockTest();

    ct.setVisible(true);

    }

    }

    class ClockLabel extends JLabel implements ActionListener {

    public ClockLabel() {

    super((Calendar.getInstance().getTime()).toString());

    Timer t = new Timer(1000, this);

    t.start();

    }

    public void actionPerformed(ActionEvent ae) {

    setText(Calendar.getInstance().getTime().toString());

    }

    }

  • 中秋節和大豐收的關聯?
  • 和平精英:新手適合打野嗎?