java实时改变Jlabel的值

用Java的窗体实现实时改变JLabel的值,给它赋0-1000随机数,500以上变色。
package demo1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class RealTime extends JFrame {
	static boolean  flag = false;
	static JLabel label= new JLabel("0.00");
	static JLabel label1= new JLabel("0.00");
	static JLabel label2= new JLabel("0.00");
	public RealTime(){
		super("title");
		Container cont = getContentPane();
		this.setBounds(50,50,200,150);
		

		
		cont.add(label,BorderLayout.SOUTH);
		cont.add(label1,BorderLayout.CENTER);
		cont.add(label2,BorderLayout.NORTH);
		this.setVisible(true);
		while(true)
		{
			label.setText(""+(Math.round(Math.random()*1000)));
			label1.setText(""+(Math.round(Math.random()*1000)));
			label2.setText(""+(Math.round(Math.random()*1000)));
			if(Double.parseDouble(label.getText())>500.00)
				label.setForeground(Color.red);
			else
				label.setForeground(Color.black);
			if(Double.parseDouble(label1.getText())>500.00)
				label1.setForeground(Color.red);
			else
				label1.setForeground(Color.black);
			if(Double.parseDouble(label2.getText())>500.00)
				label2.setForeground(Color.red);
			else
				label2.setForeground(Color.black);
			try {
	            Thread.sleep(500);
	        } catch (InterruptedException e) {
	            e.printStackTrace(); 
	        }
			
		}
	}
	

	public static void main(String[] args){
		new RealTime();
	}
}


猜你喜欢

转载自blog.csdn.net/xuqimm/article/details/72705286