• <small id='cmhyi'></small><noframes id='cmhyi'>

      <bdo id='cmhyi'></bdo><ul id='cmhyi'></ul>

      <tfoot id='cmhyi'></tfoot>
      <legend id='cmhyi'><style id='cmhyi'><dir id='cmhyi'><q id='cmhyi'></q></dir></style></legend>
    1. <i id='cmhyi'><tr id='cmhyi'><dt id='cmhyi'><q id='cmhyi'><span id='cmhyi'><b id='cmhyi'><form id='cmhyi'><ins id='cmhyi'></ins><ul id='cmhyi'></ul><sub id='cmhyi'></sub></form><legend id='cmhyi'></legend><bdo id='cmhyi'><pre id='cmhyi'><center id='cmhyi'></center></pre></bdo></b><th id='cmhyi'></th></span></q></dt></tr></i><div id='cmhyi'><tfoot id='cmhyi'></tfoot><dl id='cmhyi'><fieldset id='cmhyi'></fieldset></dl></div>

        如何在网格框中随机填充颜色

        How to fill color in grid boxes randomly(如何在网格框中随机填充颜色)
      1. <legend id='7LZtZ'><style id='7LZtZ'><dir id='7LZtZ'><q id='7LZtZ'></q></dir></style></legend>

        <i id='7LZtZ'><tr id='7LZtZ'><dt id='7LZtZ'><q id='7LZtZ'><span id='7LZtZ'><b id='7LZtZ'><form id='7LZtZ'><ins id='7LZtZ'></ins><ul id='7LZtZ'></ul><sub id='7LZtZ'></sub></form><legend id='7LZtZ'></legend><bdo id='7LZtZ'><pre id='7LZtZ'><center id='7LZtZ'></center></pre></bdo></b><th id='7LZtZ'></th></span></q></dt></tr></i><div id='7LZtZ'><tfoot id='7LZtZ'></tfoot><dl id='7LZtZ'><fieldset id='7LZtZ'></fieldset></dl></div>
            <tbody id='7LZtZ'></tbody>
            <tfoot id='7LZtZ'></tfoot>
            • <bdo id='7LZtZ'></bdo><ul id='7LZtZ'></ul>
              1. <small id='7LZtZ'></small><noframes id='7LZtZ'>

                1. 本文介绍了如何在网格框中随机填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在网格框上随机填充颜色?

                  How can I fill color on the grid boxes randomly?

                  这里不是如图所示有序:

                  Rather than orderly as shown in picture here:

                  网格 http://www.freeimagehosting.net/uploads/4ed76557de.jpg

                  public class grid extends JPanel{
                      Label one = new Label();
                      Label two = new Label();
                      Label three = new Label();
                      Label four = new Label();
                  
                      public static void main(String[] args){
                          JFrame jf=new JFrame();
                          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          jf.add(new YAnswers());
                          jf.pack();
                          jf.setVisible(true);
                      }
                  
                      grid (){
                         int rows=10; int cols=10;
                         setLayout(new GridLayout(rows,cols));
                         add(one); one.setBackground(Color.red);
                         add(two); two.setBackground(Color.orange);
                         add(three); three.setBackground(Color.green);
                         add(four); four.setBackground(Color.black);
                         boxes[] bx=new boxes[rows*cols];
                  
                          for(int i=0;i<rows*cols;i++){
                              System.out.println("i"+i);
                              bx[i]=new boxes();
                              if(i%2<1)
                                  bx[i].setColor(1);
                              add(bx[i]);
                          }
                      } //end grid()
                  }
                  

                  推荐答案

                  可以通过Math.random获得随机颜色:

                  You can get a random color by using Math.random:

                  new Color( (float)Math.random(), (float)Math.random(), (float)Math.random() );
                  

                  顺便说一句:Java 中的类名以大写字母开头,因此请使用 Grid 而不是 grid.

                  BTW: Class-names start with upper-case in Java, so use Grid instead of grid.

                  编辑

                  以下代码使用 GridBagLayout 产生这个结果:

                  The following code uses GridBagLayout to produce this result:

                  替代文字 http://img214.imageshack.us/img214/5426/so2374295.png

                  public Grid ()
                  {
                      final Color BACKGROUND = Color.LIGHT_GRAY;
                      final Color[] colors = new Color[]
                          {Color.BLACK, Color.BLACK, Color.BLUE, Color.BLUE};
                  
                      final int ROWS=10;
                      final int COLS=10;
                  
                      setBackground(Color.BLACK);
                      setLayout(new GridBagLayout());
                  
                      Label[][] label = new Label[ROWS][COLS];
                  
                      GridBagConstraints gc = new GridBagConstraints();
                      gc.weightx = 1d;
                      gc.weighty = 1d;
                      gc.insets = new Insets(0, 0, 1, 1);
                      gc.fill = GridBagConstraints.BOTH;
                  
                      // fill the whole panel with labels
                      for( int r=0 ; r<ROWS ; r++) {
                          for( int c=0 ; c<COLS ; c++) {
                              Label l = new Label();
                              l.setBackground(BACKGROUND);
                              gc.gridx = r;
                              gc.gridy = c;
                              add(l, gc);
                              label[r][c] = l;
                          }
                      }
                  
                      // now find random fields for the colors defined in BACKGROUND
                      for(Color col : colors) {
                          int r, c;
                          do { // make sure to find unique fields
                              r = (int)Math.floor(Math.random() * ROWS);
                              c = (int)Math.floor(Math.random() * COLS);
                          } while(!label[r][c].getBackground().equals(BACKGROUND));
                          label[r][c].setBackground(col);
                      }
                  }
                  

                  这篇关于如何在网格框中随机填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                      <tfoot id='mTdGw'></tfoot>
                      <i id='mTdGw'><tr id='mTdGw'><dt id='mTdGw'><q id='mTdGw'><span id='mTdGw'><b id='mTdGw'><form id='mTdGw'><ins id='mTdGw'></ins><ul id='mTdGw'></ul><sub id='mTdGw'></sub></form><legend id='mTdGw'></legend><bdo id='mTdGw'><pre id='mTdGw'><center id='mTdGw'></center></pre></bdo></b><th id='mTdGw'></th></span></q></dt></tr></i><div id='mTdGw'><tfoot id='mTdGw'></tfoot><dl id='mTdGw'><fieldset id='mTdGw'></fieldset></dl></div>
                        <tbody id='mTdGw'></tbody>

                      <small id='mTdGw'></small><noframes id='mTdGw'>

                        • <legend id='mTdGw'><style id='mTdGw'><dir id='mTdGw'><q id='mTdGw'></q></dir></style></legend>
                          • <bdo id='mTdGw'></bdo><ul id='mTdGw'></ul>