简体   繁体   English

使用get方法初始化在不同类中声明的JButton的多维数组

[英]Initialize a multidimensional array of JButtons declared in a different class using get method

I have to create a battleship game where there is a multidimensional array of JButtons to denote the player grid. 我必须创建一个战舰游戏,其中有一个JButton的多维数组来表示玩家网格。 I am supposed to initialize the JButtons in the main UI class but they need to be declared in a separate class holding the player grid. 我应该在主UI类中初始化JButton,但需要在一个单独的类中声明JButton,这些类包含播放器网格。 I have tried this to add the buttons: 我试图这样做来添加按钮:

for(int i = 0; i <= 10; i++){
        for(int j = 0; j <= 10; j++){
           playerOnePanel.add(playerOne.getBoard()); 
         }
    }   

But this returns a null pointer exception. 但这将返回空指针异常。 The class I'm trying to reference is: 我要参考的课程是:

public class Player {
    private Color shipColor;
    private String userName;
    private boolean isFirst;
    private JButton[][] buttonBoard = new JButton[rows][cols];
    private final static int rows = 10;
    private final static int cols = 10;

    public Player(String name){
        initComponents();
    }

    private void initComponents(){
        for(int i = 0; i <= rows; i++){
            for(int j = 0; j <= cols; j++){
                buttonBoard[i][j] = new JButton();
            }
        }
    }

     public JButton[][] getBoard(){
         return buttonBoard;
     } 
}

And the code I want to use the object in is: 我要在其中使用该对象的代码是:

public class BattleshipUI extends JFrame {
    private JMenuBar menuBar;    
    private JMenu gameMenu;
    private JMenu optionMenu;
    private JMenuItem playerPlayer;
    private JMenuItem playerComputer;
    private JMenuItem computerComputer; 
    private JMenuItem exit; 
    private JMenuItem game;
    private JMenuItem player;
    private JButton deploy;
    private JPanel shipLayoutPanel;
    private JPanel playerOnePanel;
    private JComboBox shipCb;
    private JComboBox directionCb;
    // Data arrays for various components on the UI
    private String[] rowLetters = {" ","A","B","C","D","E","F","G","H","I","J"};
    private String[] columnNumbers = {" ","1","2","3","4","5","6","7","8","9","10"};
    private String[] ships = {"Carrier","Battleship","Submarine","Destroyer", "Patrol Boat"};
    private String[] direction = {"Horizontal","Vertical"};
    private static final int PLAYER_ONE = 0;
    private static final int PLAYER_TWO = 1;
    private Player playerOne;
    private Player playerTwo;
    private Player[] players = new Player[2];
    private Color[] color = {Color.cyan, Color.green, Color.yellow, Color.magenta, Color.pink, Color.red, Color.white};

    public BattleshipUI(){
        initComponents();
        initObjects();
    }

    public BattleshipUI getThisParent(){
        return this;
    }

    private void initObjects(){
        playerOne = new Player("Player One");
        playerTwo = new Player("Player Two");
        players[1] = playerOne;
        players[2] = playerTwo;
    }

    private void initComponents(){
        this.setTitle("Battleship");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(500,500));
        this.setMinimumSize(new Dimension(500,500));

        menuBar = new JMenuBar();
        gameMenu = new JMenu("Game");
        optionMenu = new JMenu("Option");
        menuBar.add(gameMenu);
        menuBar.add(optionMenu);

        playerPlayer = new JMenuItem("Player vs. Player");
        playerComputer = new JMenuItem("Player vs. Computer");
        computerComputer = new JMenuItem("Computer vs. Computer");
        exit = new JMenuItem("Exit");
        gameMenu.add(playerPlayer);
        gameMenu.add(playerComputer);
        gameMenu.add(computerComputer);
        gameMenu.add(exit);
        playerPlayer.setEnabled(false);
        computerComputer.setEnabled(false);

        game = new JMenuItem("Game Options");
        player = new JMenuItem("Player Options");
        optionMenu.add(game);
        optionMenu.add(player);

        shipLayoutPanel = new JPanel();
        shipLayoutPanel.setBorder(BorderFactory.createTitledBorder("Select Ship and Direction"));

        shipCb = new JComboBox(ships);
        directionCb = new JComboBox(direction);
        deploy = new JButton("DEPLOY");
        deploy.setEnabled(false);

        shipLayoutPanel.add(shipCb);
        shipLayoutPanel.add(directionCb);
        shipLayoutPanel.add(deploy);  

        playerOnePanel = new JPanel(new GridLayout(11,11));
        playerOnePanel.setMinimumSize(new Dimension(400,400));
        playerOnePanel.setPreferredSize(new Dimension(400,400));
        playerOnePanel.setBorder(BorderFactory.createTitledBorder("Player  One"));

        for(int i = 0; i <= 10; i++){
            for(int j = 0; j <= 10; j++){
               playerOnePanel.add(playerOne.getBoard()); 
             }
        }   

        this.setJMenuBar(menuBar);
        this.add(shipLayoutPanel, BorderLayout.NORTH);
        this.add(playerOnePanel, BorderLayout.WEST);
        this.setVisible(true);
    }
}

Is there a way to do this? 有没有办法做到这一点? I need to use the methods and variables listed. 我需要使用列出的方法和变量。

You seem to initialize the players (and their boards respectively) after trying to add their board to a panel, which is impossible since you did not create the players yet. 尝试将其板子添加到面板后,似乎要初始化播放器(和它们的板子),这是不可能的,因为尚未创建播放器。

 public BattleshipUI(){
     initComponents(); //Here you try to add the board of a player
     initObjects(); //Here you initialize the players (ergo nullpointer)
 }

 private void initComponents(){
     for(int i = 0; i <= 10; i++){
         for(int j = 0; j <= 10; j++){
             //playerOne is not instantiated yet
             playerOnePanel.add(**playerOne**.getBoard()); 
         }
     }   
 }

  private void initObjects(){
        **playerOne** = new Player("Player One");
        playerTwo = new Player("Player Two");
        players[1] = playerOne;
        players[2] = playerTwo;
}

Another thing I see is: You are trying to add a multi-dimensional JButton-Array at once to the JPanel. 我看到的另一件事是:您正在尝试一次向JPanel添加多维JButton-Array。 I think this will not work (please correct me if i am wrong). 我认为这行不通(如果我错了,请纠正我)。 You have to do this Button by Button: 您必须逐个按钮地执行以下操作:

JButton[][] board = playerOne.getBoard();
for(int i=0;i<rowSize;i++){
   for(int j=0;j<columnSize;j++){
       playerOnePanel.add(board[i][j]);
   }
}

Notice: Obviously you have to get somehow the row size and column size and declare as a variable as here as rowSize and columnSize . 注意:显然,您必须以某种方式获取行大小和列大小,并将其声明为变量,如rowSizecolumnSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM