gpt4 book ai didi

Java Swing 定时器;创建一个循环;数到 4 两次

转载 作者:行者123 更新时间:2023-11-29 03:57:18 25 4
gpt4 key购买 nike

使用下面的代码,计时器计时 4 秒,如果计时到 1,' Hello World !'打印出来。
我怎样才能使计时器计数到 4 TWICE 并打印Hello World 仅在第一次计数时为 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);
}

}

最佳答案

只要有一个 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);
}

}

关于Java Swing 定时器;创建一个循环;数到 4 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5619016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com