搞个子类用WHILE循环就可以了class main{ //存在一个LABEL JLabel ex; //定义一个子类 new time().start(); } //子类 class time extends Thread{ public void run(){ while(true){ ex.setText(显示内容); Thread.sleep(100); //每秒变化10次 } } }
import javax.swing.*; import java.awt.event.*;class TimerTest { JFrame mainFrame; JPanel mainPanel; JLabel label; Timer timer; static int count = 0; public TimerTest() { mainFrame = new JFrame ( ); mainPanel = new JPanel (); label = new JLabel ("this is the 0th time the timer executes"); timer = new Timer( 1000, new ActionListener(){
public void actionPerformed( ActionEvent e){ label.setText( String.format("this is the " + count +"th time the timer executes")); count++; } }); timer.setInitialDelay(1000); timer.start(); mainPanel.add(label); mainFrame.getContentPane().add( mainPanel ); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.setSize(300,100); mainFrame.setLocationRelativeTo(null); mainFrame.setVisible( true ); } public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable(){ public void run(){ new TimerTest(); } }); } }
//存在一个LABEL
JLabel ex;
//定义一个子类
new time().start();
}
//子类
class time extends Thread{
public void run(){
while(true){
ex.setText(显示内容);
Thread.sleep(100); //每秒变化10次
}
}
}
import java.awt.event.*;class TimerTest
{
JFrame mainFrame;
JPanel mainPanel;
JLabel label;
Timer timer;
static int count = 0;
public TimerTest() {
mainFrame = new JFrame ( );
mainPanel = new JPanel ();
label = new JLabel ("this is the 0th time the timer executes");
timer = new Timer( 1000, new ActionListener(){
public void actionPerformed( ActionEvent e){
label.setText( String.format("this is the " + count +"th time the timer executes"));
count++;
}
});
timer.setInitialDelay(1000);
timer.start();
mainPanel.add(label);
mainFrame.getContentPane().add( mainPanel );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.setSize(300,100);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible( true );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable(){
public void run(){
new TimerTest();
}
});
}
}