package swing;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SuanSuoDong {
public static void main(String[] args) {
JFrame jf = new JFrame("闪闪闪"); // 创建窗口
jf.setBounds(0, 0, 10000, 10000);//设置窗口的位置
JPanel jp = new JPanel(); // 创建容器
Random random = new Random();
ArrayList<JButton> ar = new ArrayList<>(); // 新建集合来存放按键
for (int i = 0; i < 1000; i++) {
JButton jb = new JButton("我爱你");
Font font = new Font("黑体", Font.BOLD, 15);//设置字体
jb.setFont(font);
jb.setForeground(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));//通过Random来随机变换按钮的前景色
jb.setBackground(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
随机变换按钮的背景颜色
jb.setSize(60,60);
jp.add(jb);
ar.add(jb); // 集合存放按键
}
// 延迟的毫秒数和初始侦听器
Timer timer = new Timer(10, new ActionListener() {
/*
* 初始化侦听器
*/
public void actionPerformed(ActionEvent e) {
for (JButton bt : ar) {
bt.setForeground(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
bt.setBackground(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
}
}
});
timer.start(); //使侦听器开始
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口的关闭,点击右上角的X图标时,程序退出
jf.setVisible(true);//设置窗口是否可见
}
}
Swing编程中的闪烁按钮
最新推荐文章于 2022-05-31 16:52:45 发布