0

宿題として、人生ゲームを Java で実装しました。

2 次元配列があり、ダイアログ ボックスに出力したいと考えています。

私の配列タイプはブール値です。

私がしたことは、次の方法で1つの大きな文字列に変換することです

public void Build_Matrix_String()
{
    String str1 = "";
    int i, j;

    for (i = 0; i < Rows; i++){
        str1 = str1.concat("    ");
        for (j = 0; j < Columns; j++){                
            if (Matrix[i][j] == true){
                str1 = str1.concat(" + ");
            }
            else {
                str1 = str1.concat(" - ");
            }
        }
        str1 = str1.concat("    \n");
    }
    JOptionPane.showMessageDialog(null, str1);
}

最初に文字列に変換するよりも良い方法はありますか?

そうでない場合、見栄えを良くする方法はありますか?

どうもありがとう。

アサフ。

4

2 に答える 2

0

そのように?

ここに画像の説明を入力してください

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ShowMatrix {
   public static void main( String[] args ) {
      boolean[][]   matrix = new boolean[3][3];
      matrix[ 0 ][ 0 ] = true;
      matrix[ 0 ][ 2 ] = true;
      matrix[ 1 ][ 1 ] = true;
      matrix[ 2 ][ 0 ] = true;
      matrix[ 2 ][ 2 ] = true;

      StringBuilder str1   = new StringBuilder();
      for( int i = 0; i < matrix.length; i++ ) {
         for( int j = 0; j < matrix[ i ].length; j++ ) {
            str1 = str1.append( matrix[i][j] ? " + " : " - " );
         }
         str1 = str1.append( '\n' );
      }
      JOptionPane.showMessageDialog(
         null, new JLabel( "<html><pre>" + str1.toString()));
   }
}
于 2013-02-23T16:42:59.377 に答える
0

私はこれが求められていないことを知っており、そうするべきではありませんが、私は抵抗できません...なぜそのためにダイアログボックスを使用するのですか? JTextFieldこれまでのところ、swing を使用してプログラムを作成したことはありませんが、たとえば、色付きのボックスとして悪用して実行し、次のようにマトリックス レイアウトに積み上げた方がはるかに優れているように思えます。

static int MATRIX_SIZE = 15;
static int CELL_SIZE = 20;

static Color ACTIVE = new Color(255, 128, 128);
static Color INACTIVE = new Color(0, 127, 127);

static JComponent items[][] = new JComponent[MATRIX_SIZE][MATRIX_SIZE];

// here we should use something more appropriate than dummy text fields
private static JComponent createItem() {
    JTextField item = new JTextField("");
    Dimension d = new Dimension(CELL_SIZE, CELL_SIZE);
    item.setPreferredSize(d);
    item.setBackground(INACTIVE);
    item.setEditable(false);
    item.setBorder(new EmptyBorder(0, 0, CELL_SIZE, CELL_SIZE));
    return item;
}

private static void createMatrixView() {
    JFrame frame = new JFrame("Show Pixels");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent panel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    panel.setLayout(layout);

    GridBagConstraints c = new GridBagConstraints();
    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            items[i][j] = createItem();
            c.gridx = i;
            c.gridy = j;
            panel.add(items[i][j], c);
        }
    }

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
}

private static void update(boolean[][] values) {
    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            items[i][j].setBackground(values[i][j] ? ACTIVE : INACTIVE);
        }
    }
}

public static void main(String[] args) {
    createMatrixView();

    boolean[][] matrix = new boolean[MATRIX_SIZE][MATRIX_SIZE];
    matrix[0][0] = true;
    matrix[0][2] = true;
    matrix[1][1] = true;
    matrix[2][0] = true;
    matrix[2][2] = true;

    update(matrix);
}
于 2013-02-23T18:54:42.117 に答える