2017-06-19 2 views
0

私はn×nボード(この例では5 x 5)に各セルにランダムな文字が入ったボードゲームを作成しています。文字は文字列の配列からランダムに選択され、ランダムに選択された文字の2次元配列(for String)のforループの後にJFrameに配置するボタンの2次元配列があります。以下のプログラムの目的は、5×5ボードのボタンを表示することです。これは動作しません。私はこの問題を解決するための助けや指導に非常に感謝します。基本的にはJFrameボード上に2次元アレイをミラーリングする

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); 
    } 
} 

(問題をフォーマットする任意のコードのために申し訳ありませんが)

+0

特に動作しないものはありますか?文字の表示?あなたは何をしようとしているのか、そして現在何が起こっているのかについて少し具体的にすることができますか? –

+0

すべて25ではなく、最後のボタンだけが表示されています。 –

+0

frame.setLayout()行のコメントを外すことはできませんか? –

答えて

0

、あなたは2次元配列を持っていて、それをミラー化するとき、何をやるべきことは基本的にはこれです:

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] 
    } 
} 

の場合あなたのコードの問題でより具体的になるかもしれない、私はもっと役に立つことができるはずです...

関連する問題