[Java 기초]--카운트다운 팝 업 창 구현

2305 단어 Java
우 리 는 자바 언어의 awt 특성 을 직접 사용 하여 실행 중인 팝 업 상자 카운트다운 을 쓸 수 있 습 니 다.
package test.dagong.testDecreaseDate;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

@SuppressWarnings("unused")
public class TestDecreaseDate extends JFrame implements ActionListener {

    private static final long serialVersionUID = 4603262282860990473L;

    private static final int DEFAULT_WIDTH = 200;
    private static final int DEFAULT_HEIGHT = 100;
    private static final int width = Toolkit.getDefaultToolkit()
            .getScreenSize().width;
    private static final int height = Toolkit.getDefaultToolkit()
            .getScreenSize().height;
    private Container container;
    private JButton btn;
    private JTextField jtfTime;
    private Timer tmr;

    public TestDecreaseDate() {
        initComponents();
        Timer tmr = new Timer(1000, this);
        this.tmr = tmr;
        setVisible(true);
    }
    private void initComponents() {
        this.setTitle("SY  ");
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        this.setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation((width - DEFAULT_WIDTH) / 2,
                (height - DEFAULT_HEIGHT) / 2);
        jtfTime = new JTextField("10000");
        btn = new JButton("     ");
        container = getContentPane();
        JPanel panel = new JPanel();
        panel.add(btn);
        panel.add(jtfTime);
        this.add(panel);
        btn.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == btn) {
            jtfTime.setText("10");  
             tmr.start();
        } else {
            int t;
            t = Integer.parseInt(jtfTime.getText());
            t--;
            jtfTime.setText("" + t);

            if (t <= 0) {
                tmr.stop();
            }
        }
    }
    public static void main(String[] args) {
        TestDecreaseDate timeController = new TestDecreaseDate();
    }
}

좋은 웹페이지 즐겨찾기