镜像JFrame板上的2d阵列

时间:2017-06-19 21:27:11

标签: java arrays swing multidimensional-array 2d-games

我正在创建一个棋盘游戏,其中n×n板(本例中为5 x 5)在每个单元格中包含随机字母。这些字母是从一个字符串数组中随机选择的,并且有一个2d数组的按钮可以将它们放入JFrame中,然后是for循环,用于随机选择的字母的2d数组(如字符串)。下面的程序的目的是显示一个5乘5的按钮板,这是不起作用的。我将非常感谢有关解决此问题的任何帮助或指示。

package com.content;

import javax.swing.*;
import java.awt.Dimension;

public class WordDisplay {
   static int random;
   static JFrame frame = new JFrame("WordMatch");

   public static void newFrame() {
      frame.setSize(new Dimension(800, 600));
      //frame.setLayout(new GridLayout(3, 2));
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setVisible(false);
   }

   public static void fillFrame(){


      String[] Letters = {"a","b","c","d","e","f","g", 
                          "h","i","j","k","l","m","n",
                          "o","p","q","r","s","t","u",
                          "v","w","x","y","z"};

      JButton[][] buttons = new JButton[5][5];
      for (int r = 0;r<5;r++){
          for (int c = 0;c<5;c++){
              buttons[r][c] = new JButton("");
          }
      }
      String[][] Board = new String[5][5];
      for (int r = 0;r<5;r++){

         for (int c = 0;c<5;c++){

            random  = (int)(Math.random() * 25);
            Board[r][c] = Letters[random];
            buttons[r][c].setText(Board[r][c]);

            System.out.print(Board[r][c]);

            frame.add(buttons[r][c]);

          }
      }
   }

   public static void main(String args[]){

      newFrame();
      fillFrame();
      frame.setVisible(true);
   }
}

(对不起任何代码格式问题)

1 个答案:

答案 0 :(得分:0)

基本上,当你有一个2D数组而你想要镜像它时,你应该做的基本上是这样的:

for(int x=0;x<array.length;x++){
    for(int y=0;y<array[x].length;y++){
        array[x][y]=array[(array[x].length)-x][y]
    }
}

如果您对代码问题更具体,我应该能够更有帮助...