我想让程序显示0hello停顿一秒然后变成1hello一直repeat到9hello,如果这么写,就不显示任何东西了,timer。start()放在constructor里面,就没有delay总是显示9hello了,有没有什么方法可以delay一下每当for loop完成一个循环。先谢过
--------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class NakeTime implements ActionListener{
JFrame f=null;
JLabel label = null;
int count = 0;
Timer timer; public NakeTime(){
f=new JFrame();
label = new JLabel();
f.getContentPane().add(label);
f.setSize(200,100);
f.setLocation(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); timer = new Timer(1000,this); } public static void main(String[] args){
new NakeTime();
}
public void actionPerformed(ActionEvent e){
for (int i=0;i<10;i++)
{ timer.start();
label.setText(i+ "Hello! ");
}
}
}
--------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class NakeTime implements ActionListener{
JFrame f=null;
JLabel label = null;
int count = 0;
Timer timer; public NakeTime(){
f=new JFrame();
label = new JLabel();
f.getContentPane().add(label);
f.setSize(200,100);
f.setLocation(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); timer = new Timer(1000,this); } public static void main(String[] args){
new NakeTime();
}
public void actionPerformed(ActionEvent e){
for (int i=0;i<10;i++)
{ timer.start();
label.setText(i+ "Hello! ");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace timerTest
{
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
this.textBox1.Text = count+++"hello";
this.timer1.Interval = 1000;
if(count==10)
{
this.timer1.Enabled = false;
}
}
}
}java开发桌面程序不太容易。
public class NakeTime implements ActionListener {
JFrame f = null;
JLabel label = null;
int count = 0;
Timer timer; public NakeTime() {
f = new JFrame();
label = new JLabel();
f.getContentPane().add(label);
f.setSize(200, 100);
f.setLocation(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); timer = new Timer(1000, this);
timer.start();
} public static void main(String[] args) {
new NakeTime();
} public void actionPerformed(ActionEvent e) {
label.setText(count + "Hello! ");
count++;
if (count>9) timer.stop();
}
}