如何刷新JFrame中包含的按钮的颜色?

 兴霖先玲广_215 发布于 2023-01-06 12:25

我正在编写一个小游戏,其中我在JFrame中采用了JButtons网格,我想刷新JFrame中包含的按钮的颜色,这已经可见.如下所述

 void foo(){
     mainFrame.setVisible(true);//mainFrame is defined at class level.
     //color update code for the buttons.
     mainFrame.setVisible(true);
 }

我得到的结果不如预期,我的屏幕被冻结.这不是实现我想要的正确方法吗? 编辑 确定我正在详细解释我想要实现的目标.我有一个类,如: -

import javax.swing.*;
import java.awt.*;
import java.util.*;
class Brick extends JButton{
      public void setRandomColors(){
            int random = (int) (Math.random()*50);
            if(random%13==0){
                this.setBackground(Color.MAGENTA);
            }
            else if(random%10==0){
                this.setBackground(Color.red);
            }
            else if(random%9==0){

                this.setBackground(Color.yellow);
            }
            else if(random%7==0){
                this.setBackground(Color.orange);
            }
            else if(random%2==0){
                this.setBackground(Color.cyan);
            }
            else{
                this.setBackground(Color.PINK);
            }
    }
    public void setBlackColor(){
            this.setBackground(Color.black);
    }
}
class Grid {
    JFrame mainGrid = new JFrame();
    ArrayList bunchOfBricks = new ArrayList<>();
    int gridLength = 8;//gridlenth is equals to gridweight as i have considered a Square grid.
    int totalBricks = gridLength*gridLength;
    public void formBunchOfBricks(){
            for(int i=0;i

}

请帮我解决问题?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有