我正在编写一个小游戏,其中我在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(); ArrayListbunchOfBricks = 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 }
请帮我解决问题?