2017-07-03 54 views
2

我想创建一个游戏(Yahtzee)的scorePanel。记分板必须包含22行和1列每个玩家,但下面的代码我正在写为每个玩家显示12行和2列。GridLayout显示2行'新GridLayout(22,1)`

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()方法确实添加了太多的Jlabel因为你确实添加了其中的两个,当球员的一个标签已创建。以下是更正后的版本:

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

编辑:我是固定在内部添加一个else子句的for循环,以确保当没有其他条件符合先前无人防守的代码才会执行​​。

+0

你应该更好地解释你修正了什么,但它是正确的。 +1 :) – CodingNinja