자바 스윙 시스템 시간 동적 디 스 플레이

2602 단어 Java-Swing
package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestIFrame extends JFrame {
    private JTextField time;
    SimpleDateFormat myfmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public TestIFrame(){
        super();
        setBounds(100, 100, 200, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        time = new JTextField();
        this.add(time,BorderLayout.CENTER);
        time.addActionListener(new TimeActionListener());
        setVisible(true);

    }

    //     “     ”      ,          
    class TimeActionListener implements ActionListener{
        public TimeActionListener(){
            javax.swing.Timer t=new javax.swing.Timer(1000,this);
            t.start();
        }

        @Override
        public void actionPerformed(ActionEvent ae){
            time.setText(myfmt.format(new java.util.Date()).toString());
        }
    }

    public static void main(String[] args){
        new TestIFrame();
    }

}

좋은 웹페이지 즐겨찾기