2017-07-03 6 views
2

ゲーム用のscorePanel(Yahtzee)を作成しようとしています。 scorepanelは各プレイヤーの22行と1列で構成されていなければなりませんが、次のコードは各プレイヤーに12行と2列を表示しています。GridLayout `new GridLayout(22,1)`の2行を表示する

import javax.swing.*; 
import javax.swing.border.MatteBorder; 
import java.awt.*; 

public class PanelTest { 

    private final String player1 = "krishna"; 
    private final String player2 = "Suresh"; 

    public PanelTest(){ 

     JFrame gameWindow = new JFrame("Play Game"); 
     gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel gamePanel = new JPanel(); 
     gamePanel.setLayout(new GridLayout(1,2)); 

/************ Scorepannel Creation starts here********************/ 

     JPanel scorePanel; 
     scorePanel = createScorePanel(); 

/***************** Scorepanel Creation Ends Here*****************/ 

     //adding scorePanel to gamePanel 
     scorePanel.setSize(500,540); 
     gamePanel.add(scorePanel, BorderLayout.WEST); 

     //adding gamePanel to gameWindow 
     gameWindow.add(gamePanel); 

     gameWindow.setSize(1000, 540); 
     gameWindow.setVisible(true); 
    } 

    public JPanel createScorePanel(){ 

     JPanel scorePanel = new JPanel(); 

     JPanel[] columns; 
     columns = new JPanel[3]; 

     JLabel[] player1score; 
     player1score = new JLabel[22]; 

     JLabel[] playerNames; 
     playerNames = new JLabel[2]; 

     playerNames[0] = new JLabel(player1); 
     playerNames[1] = new JLabel(player2); 

     columns[0] = new JPanel(); 
     columns[0].setSize(200, 540); 
     columns[0].setLayout(new GridLayout(22,0)); 

     for (int count = 1; count <= (playerNames.length) ; count++){ 
      columns[count] = new JPanel(); 
      columns[count].setSize(150,540); 
      columns[count].setLayout(new GridLayout(22,1)); 

      for (int i = 0; i < player1score.length; i++){ 
       if(count == 1 && i == 0){ 
        player1score[i] = new JLabel(player1); 
        player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.GREEN)); 
        columns[count].add(player1score[i]); 
       } 

       if (count == 2 && i == 0){ 
        player1score[i] = new JLabel(player2); 
        player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.GREEN)); 
        columns[count].add(player1score[i]); 
       } 

       player1score[i] = new JLabel(); 
       player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK)); 
       columns[count].add(player1score[i]); 
      } 

      scorePanel.add(columns[count]); 
     } 


     return scorePanel; 
    } 

    public static void main(String[] args) { 
     new PanelTest(); 
    } 
} 
+0

フレンドリーなヒント:JavaFXとシーンビルダを使用することをお勧めします。これは、レイアウトを簡単に把握し、ハードコーディングすることが非常に簡単で、管理も非常に簡単です。 :) – ZeldaZach

答えて

2

あなたcreateScorePanel()方法は、プレイヤーのラベルの1つが作成されたときにこれらのうちの2つを追加しなかったので、あまりにも多くのJlabel Sを追加しました。

public JPanel createScorePanel(){ 

    JPanel scorePanel = new JPanel(); 

    JPanel[] columns; 
    columns = new JPanel[3]; 

    JLabel[] player1score; 
    player1score = new JLabel[22]; 

    JLabel[] playerNames; 
    playerNames = new JLabel[2]; 

    playerNames[0] = new JLabel(player1); 
    playerNames[1] = new JLabel(player2); 

    columns[0] = new JPanel(); 
    columns[0].setSize(200, 540); 
    columns[0].setLayout(new GridLayout(22,0)); 

    for (int count = 1; count <= (playerNames.length) ; count++){ 
     columns[count] = new JPanel(); 
     columns[count].setSize(150,540); 
     columns[count].setLayout(new GridLayout(22,1)); 

     for (int i = 0; i < player1score.length; i++){ 
      if (count == 1 && i == 0) { 
       player1score[i] = new JLabel(player1); 
       player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.GREEN)); 
       columns[count].add(player1score[i]); 
      } 
      else if (count == 2 && i == 0) { 
       player1score[i] = new JLabel(player2); 
       player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.GREEN)); 
       columns[count].add(player1score[i]); 
      } 
      else { 
       player1score[i] = new JLabel(); 
       player1score[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK)); 
       columns[count].add(player1score[i]); 
      } 
     } 
     scorePanel.add(columns[count]); 
    } 
    return scorePanel; 
} 

編集:ここでは修正されたバージョンである私が固定する他の条件のいずれも適合していない時に、以前に無防備なコードのみが実行されていることを確認するために、forループの内側にelse節を追加することでした。

+0

あなたはあなたが修正したものをよりよく説明するべきですが、それは正しいです。 +1 :) – CodingNinja