Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java摆动计时器;创建一个循环;数到4两次_Java_Swing_Loops_Timer_Count - Fatal编程技术网

Java摆动计时器;创建一个循环;数到4两次

Java摆动计时器;创建一个循环;数到4两次,java,swing,loops,timer,count,Java,Swing,Loops,Timer,Count,使用下面的代码,计时器计数4秒,如果达到1, “你好,世界!”打印出来。 如何使计时器计数为4两次并打印 你好,世界只在第一次计数1 我该怎么做 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Calendar; import java.awt.event.ActionEvent; import java.awt.event.Act

使用下面的代码,计时器计数4秒,如果达到1, “你好,世界!”打印出来。
如何使计时器计数为4两次并打印 你好,世界只在第一次计数1

我该怎么做

    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);
    }

}

这是您想要的演示程序:

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);
    }
}

这是您想要的演示程序:

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();

祝你好运,波罗。

这就是你需要放入时钟侦听器的代码

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

祝你好运,波罗。

只要做一个AlertnativeCounter,就像我在下面所做的那样

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);
    }

    }

只要有一个AletNativeCounter,就像我在下面所做的那样

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);
    }

    }

你不是忘了把这个标记为家庭作业吗;-)你说的简单是什么意思?哈哈,我是初学者!定时器是你自己的对象吗?我不认为java.util.Timer有一个名为start的方法?您是否忘记将其标记为家庭作业;-)你说的简单是什么意思?哈哈,我是初学者!定时器是你自己的对象吗?我不认为java.util.Timer有一个名为start的方法?非常感谢Sankar,这正是我的意思!非常感谢你,桑卡,这正是我的意思!