java 按鈕點擊次數顏色變化

問題描述:

在窗體中添加一個按鈕,將按鈕的點擊次數繪製在窗體中。繪製的點擊次數的字的顏色,在紅色、綠色、藍色、黑色之間隨機變化

代碼:

package disange;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
public class disange extends Frame implements ActionListener{
	Label r;
	int value=0;
	public disange(){
		super("按鈕單擊次數顏色隨機變化");
		r=new Label("。。結果。。");
		Button btn=new Button("計數");
		setLayout(new FlowLayout());
		add(btn);
		add(r);
		btn.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e){
		value++;
		int i=(int)(Math.random()*5);
		switch(i)
		{
		case 0:r.setForeground(Color.BLUE);
			break;
		case 1:r.setForeground(Color.RED);
			break;
		case 2:r.setForeground(Color.GREEN);
			break;
		case 3:r.setForeground(Color.BLACK);
			break;
		default:break;
		}
		r.setText(" "+value);
		
	}
	public static void main(String args[]){
		Frame x=new disange();
		x.setSize(400,100);
		x.setVisible(true);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章