GridLayout为`new GridLayout(22,1)`显示2行

时间:2017-07-03 02:33:01

标签: java swing layout layout-manager grid-layout

我正在尝试为游戏(Yahtzee)创建一个scorePanel。每个玩家的得分面板必须包含22行和1列,但我正在编写的以下代码为每位玩家显示12行和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();
    }
}

1 个答案:

答案 0 :(得分: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; } 。这是更正后的版本:

1) Error:
Linked_ListTest#test_next_node_after_head_is_nil:
NameError: uninitialized constant Linked_ListTest::Nil
test/linked_list_test.rb:26:in `test_next_node_after_head_is_nil'

This is my test:
24 def test_next_node_after_head_is_nil
25  list = LinkedList.new
26  assert_equal Nil, head.next_node
27 end

编辑:我修复的是在内部for循环中添加else子句,以确保只有在其他任何条件都不适合时才执行以前无人看守的代码。