簡體   English   中英

Java Swing Timer; 創建一個循環; 數到4兩次

[英]Java Swing Timer; Create a loop; count to 4 Twice

使用下面的代碼,計時器計數4秒,如果達到1,則“Hello World!” 打印出來。
我怎樣才能使計時器計數到4 TWICE並僅在第一次計數1時打印Hello World。

我該怎么做呢?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.Color;
    import java.awt.Toolkit;


class clockExample1 extends JFrame {


    private JTextField _textfield1; 

    public clockExample1() {

        _textfield1 = new JTextField(5);
        _textfield1.setEditable(false);
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(_textfield1); 
        JButton button1 = new JButton("click here");


        this.setContentPane(panel1);
        this.setTitle("Text Clock 1");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        panel1.add(button1);




        ClockListener cl = new ClockListener();
        Timer t = new Timer(1000, cl);
        t.start();


            }



class ClockListener implements ActionListener {

    int count = 0;

    public void actionPerformed(ActionEvent e) {

        int fakeSecond = (count++ % 4) + 1; 
        if (fakeSecond == 1) {  System.out.println( "Hello, World!" );

 }
        Calendar now = Calendar.getInstance();
        int h = now.get(Calendar.HOUR_OF_DAY);
        int m = now.get(Calendar.MINUTE);
        int s = now.get(Calendar.SECOND);
        _textfield1.setText("" + fakeSecond + "");


    }


}




    public static void main(String[] args) {
        JFrame clock = new clockExample1();
        clock.setVisible(true);
    }

}

只有一個Aletrnativecounter,就像我在下面做的那樣

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.Color;
    import java.awt.Toolkit;


    class clockExample1 extends JFrame {


    private JTextField _textfield1; 

    public clockExample1() {
        System.out.println("Flow Here"+"2");
        _textfield1 = new JTextField(5);
        _textfield1.setEditable(false);
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(_textfield1); 
        JButton button1 = new JButton("click here");


        this.setContentPane(panel1);
        this.setTitle("Text Clock 1");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        panel1.add(button1);



        System.out.println("Flow Here"+"3");
        ClockListener cl = new ClockListener();
        Timer t = new Timer(1000, cl);
        t.start();


            }



    class ClockListener implements ActionListener {

    int count = 0, alternativecounter=0;

    public void actionPerformed(ActionEvent e) {
        System.out.println("Flow Here"+"4");
        int fakeSecond = (count++ % 4) + 1; 


        if (fakeSecond == 1) {  
            alternativecounter+=1;
            System.out.println("alternativecounter"+alternativecounter);
            if(alternativecounter==2)
            {

                alternativecounter=0;
                System.out.println("alternativecounter"+alternativecounter);
                JOptionPane.showMessageDialog(null,"Warm Welcome to you, My dear friend");
            }

    }
        System.out.println("Flow Here"+"5");
        Calendar now = Calendar.getInstance();
        int h = now.get(Calendar.HOUR_OF_DAY);
        int m = now.get(Calendar.MINUTE);
        int s = now.get(Calendar.SECOND);
        _textfield1.setText("" + fakeSecond + "");


    }


    }




    public static void main(String[] args) {
        System.out.println("Flow Here"+"1");
        JFrame clock = new clockExample1();
        clock.setVisible(true);
    }

    }

這是您想要的演示程序:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class TestCls {
    static int i=1;
    static Timer timer;
    static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {
        timer = new Timer(100, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (i == 4){
                    if(!flag){
                        System.out.println("Print me on 4.");
                        i=0;
                        flag = true;
                    } else {
                        timer.stop();
                    }
                }
                i = i + 1;
            }
        });
        timer.start();
        Thread.sleep(1000);
    }
}

這是您需要放入ClockListener的代碼。

//in timer
if(counter == 1)
   System.out.println( "Hello, World!" );
else if(counter == 8)
   t.stop();

祝你好運,博羅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM