(JAVA 战列舰)button/image 网格工作正常但是当我在它旁边添加第二个网格时它们都搞砸了

(JAVA battleships) button/image grid works fine but when I add a second grid next to it they both get messed up

我正在为 java 创建战舰程序。使用 gridlayout 我想在西面板中放置玩家面板,在东面板中放置 NPC 面板(以及在中心面板和南面板中放置一些其他信息和输入方式)。

我开始像下面这样添加玩家面板(完整代码也可以在 post 的底部找到):

public void placePlyrGrids()
{
    plyrBoard.add(cornerGridLabel); //add an empty grid to the top left
    for(n = 0; n < 10; n++)
        plyrBoard.add(letterGridLabels[n]); //the first row is the first 10 letters of the alphabet
    for (y = 1; y < 11; y++) //For every (y) row...
    {

        for (x = 0; x < 11; x++) //...add ten (x) grids to make columns
        {
            if (x == 0) //To the start of each row, add a number (image JLabel)
            {
                plyrBoard.add(numberGridLabels[numberCounter]);
                numberCounter++;
            }
            else //for the rest of each row, add buttons
            {
                plyrBoard.add(buttonGrids[y-1][x-1]);
            }
        }
    }
}

这很好用。

然后我尝试以两种方式添加经销商板,none 其中一种似乎有效,因为它们都搞砸了,而且似乎也弄乱了玩家板,而玩家板似乎工作正常在其自己的。

1:用同样的方法同时放置两块板。

public void placeBoards()
{
    plyrBoard.add(cornerGridLabel); //add an empty grid to the top left
    NPCBoard.add(cornerGridLabel);
    for(n = 0; n < 10; n++)
        plyrBoard.add(letterGridLabels[n]); //the first row is the first 10 letters of the alphabet
    NPCBoard.add(letterGridLabels[n]);
    for (y = 1; y < 11; y++) //For every (y) row...
    {
        for (x = 0; x < 11; x++) //...add ten (x) grids to make columns
        {
            if (x == 0) //To the start of each row, add a number (image JLabel)
            {
                plyrBoard.add(numberGridLabels[numberCounter]);
                NPCBoard.add(numberGridLabels[numberCounter]);
                numberCounter++;
            }
            else //for the rest of each row, add buttons
            {
                plyrBoard.add(buttonGrids[y-1][x-1]);
                NPCBoard.add(emptyGridLabel);
            }       
        }
    }
}

2:将两者放在不同的方法中(与顶部的 pladePlyrGrids 方法结合使用)

public void placeNPCGrids()
{
    NPCBoard.add(cornerGridLabel);
    for(n = 0; n < 10; n++)
        NPCBoard.add(letterGridLabels[n]);

    for (y = 1; y < 11; y++) 
    {
        for (x = 0; x < 11; x++)
        {
            if (x == 0)
            {
                NPCBoard.add(numberGridLabels[numberCounter]);
                numberCounter++;
            }
            else
            {
                NPCBoard.add(emptyGridLabel);
            }
        }
    }
}

我的完整代码:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class Battleships {

    public static void main(String[] args) 
    {
        Interface Win = new Interface ();   
    }
}

class Interface extends JFrame implements ActionListener
{   
    //Identify variables
    int n;
    int numberCounter = 0;
    int y;
    int x;

    // lengths of the various ships in the game

    //ADD IMAGE COMPONENTS
    //Coordinate axes and empty grids
    ImageIcon emptyGrid = new ImageIcon("EmptyGrid.png");
    ImageIcon cornerGrid = new ImageIcon("CornerGrid.png");
    ImageIcon [] numberGrids = new ImageIcon[11];
    {
        for (n = 0; n < 11; n ++)
            numberGrids[n] = new ImageIcon("Grid" + (n + 1) + ".png");
    }
    ImageIcon [] letterGrids = new ImageIcon [11];
    {
        for (n = 0; n < 11; n ++)
            letterGrids[n] = new ImageIcon("GridA" + (n + 1)+ ".png"); 
    }
    //Ship parts

    //Clickable ships (for placement)
    ImageIcon fullBattleship = new ImageIcon("FullBattleship.png");
    ImageIcon fullCruiser = new ImageIcon("FullCruiser.png");
    ImageIcon fullPatrolBoat1 = new ImageIcon("FullPatrolBoat.png");
    ImageIcon fullPatrolBoat2 = new ImageIcon("FullPatrolBoat.png");

    //JLabels   
    JLabel cornerGridLabel = new JLabel(cornerGrid);
    JLabel [] numberGridLabels = new JLabel[10]; 
    {
        //The first 11 grids are an empty grid followed by letters A-J
        for (n = 0; n < 10; n++)
        {
            numberGridLabels[n] = new JLabel(numberGrids[n]);
        }
    }
    JLabel [] letterGridLabels = new JLabel [10];
    {
        for (n = 0; n < 10; n ++)
        {
            letterGridLabels[n] = new JLabel (letterGrids[n]);
        }
    }
    JLabel emptyGridLabel = new JLabel(emptyGrid);

    JButton [][] buttonGrids = new JButton [10][10];
    {
        for (y = 0; y < 10; y++)
        {
            for (x = 0; x < 10; x++)
            {
                buttonGrids[x][y] = new JButton(emptyGrid);
                buttonGrids[x][y].setPreferredSize(new Dimension(50,50));
            }
        }
    }

    JLabel [] NPCGrids = new JLabel[121]; //grid placements for the dealer board
    {
        for (n = 0; n < 121; n ++)
            NPCGrids[n] = new JLabel (emptyGrid);
    }

    JLabel [] clickableBoats = new JLabel [4];
    {
        clickableBoats[0] = new JLabel (fullBattleship);
        clickableBoats[1] = new JLabel (fullCruiser);
        clickableBoats[2] = new JLabel (fullPatrolBoat1);
        clickableBoats[3] = new JLabel (fullPatrolBoat2);

    }

    JButton [] plyrClickableGrids = new JButton [100];
    {
        for (n = 0; n < 99; n++);
            plyrClickableGrids[n] = new JButton(emptyGrid);
    }


    //Add interface components
    JTextArea playerInformation = new JTextArea(20,5);
    JScrollPane textPane1 = new JScrollPane (playerInformation);
    JButton playTurnBtn = new JButton ("Play Turn");

    //add JPanels
    JPanel plyrBoard = new JPanel (new GridLayout(11,11));
    JPanel infoPanel = new JPanel(new GridLayout(1,1));
    JPanel NPCBoard = new JPanel(new GridLayout(11,11));
    JPanel inputPanel = new JPanel(new GridLayout(1,10));

    public Interface ()
    {
        super ("Battleships"); 
        setSize (1367,729); 
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

        //Set the background color
        Container contentArea = getContentPane(); 

        //Set each panel to "opaque", so that the background becomes visible
        plyrBoard.setOpaque(false);
        NPCBoard.setOpaque(false);
        inputPanel.setOpaque(false);
        setVisible (true);


        playerInformation.setEditable(false); 



        //Add panels to different compass points on the content area
        contentArea.add("West", plyrBoard);
        contentArea.add("Center", infoPanel);
        contentArea.add("East", NPCBoard);
        contentArea.add("South", inputPanel);

        //Add imageLabels and buttons to panels
        placePlyrGrids();
        numberCounter = 0;
        placeNPCGrids();


        infoPanel.add(playerInformation);

        for (n = 0; n < 4; n++)
        {
            inputPanel.add(clickableBoats[n]);
        }
        inputPanel.add(playTurnBtn);



        // TODO Vanity 
        //Button format
        playTurnBtn.setPreferredSize(new Dimension(1, 141));


    }

    public void placePlyrGrids()
    {
           plyrBoard.add(cornerGridLabel); //add an empty grid to the top left
            //NPCBoard.add(cornerGridLabel);
            for(n = 0; n < 10; n++)
                plyrBoard.add(letterGridLabels[n]); //the first row is the first 10 letters of the alphabet
                //NPCBoard.add(letterGridLabels[n]);

        for (y = 1; y < 11; y++) //For every (y) row...
        {
            for (x = 0; x < 11; x++) //...add ten (x) grids to make columns
            {
                if (x == 0) //To the start of each row, add a number (image JLabel)
                {
                    plyrBoard.add(numberGridLabels[numberCounter]);
                    //NPCBoard.add(numberGridLabels[numberCounter]);
                    numberCounter++;
                }
                else //for the rest of each row, add buttons
                {
                    plyrBoard.add(buttonGrids[y-1][x-1]);
                    //NPCBoard.add(emptyGridLabel);
                }
            }
        }
    }

    public void placeNPCGrids()
    {
        NPCBoard.add(cornerGridLabel);
        for(n = 0; n < 10; n++)
            NPCBoard.add(letterGridLabels[n]);

        for (y = 1; y < 11; y++) 
        {
            for (x = 0; x < 11; x++)
            {
                if (x == 0)
                {
                    NPCBoard.add(numberGridLabels[numberCounter]);
                    numberCounter++;
                }
                else
                {
                    NPCBoard.add(emptyGridLabel);
                }
            }
        }
    }


    public void actionPerformed(ActionEvent e) 
    {

    }
}

我相信你现在已经明白我对此很困惑,我完全不明白为什么会这样。毕竟,第一种方法本身就可以正常工作。为什么第二个不应该,为什么他们要自相残杀?

任何帮助我朝着正确方向前进的建议或技巧将不胜感激。

编辑:

解决方案:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class Battleships {


 public static void main(String[] args) 
 
 {
  
  Interface Win = new Interface ();
  
 }

}

class Interface extends JFrame implements ActionListener
{
 
 //Identify variables
 int n;
 int numberCounter = 0;
 int y;
 int x;

 // lengths of the various ships in the game


 //ADD IMAGE COMPONENTS
 //Coordinate axes and empty grids
 ImageIcon emptyGrid = new ImageIcon("EmptyGrid.png");
 ImageIcon cornerGrid = new ImageIcon("CornerGrid.png");
 ImageIcon [] numberGrids = new ImageIcon[11];
 {
  for (n = 0; n < 11; n ++)
   numberGrids[n] = new ImageIcon("Grid" + (n + 1) + ".png");
 }
 ImageIcon [] letterGrids = new ImageIcon [11];
 {
  for (n = 0; n < 11; n ++)
   letterGrids[n] = new ImageIcon("GridA" + (n + 1)+ ".png"); 
 }
 //Ship parts
 
 //Clickable ships (for placement)
 ImageIcon fullBattleship = new ImageIcon("FullBattleship.png");
 ImageIcon fullCruiser = new ImageIcon("FullCruiser.png");
 ImageIcon fullPatrolBoat1 = new ImageIcon("FullPatrolBoat.png");
 ImageIcon fullPatrolBoat2 = new ImageIcon("FullPatrolBoat.png");

 
 //JLabels  
 JLabel plyrCornerGridLabel = new JLabel(cornerGrid);
 JLabel [] plyrNumberGridLabels = new JLabel[10]; 
 {
  //The first 11 grids are an empty grid followed by letters A-J
  for (n = 0; n < 10; n++)
  {
   plyrNumberGridLabels[n] = new JLabel(numberGrids[n]);
  }
 }
 JLabel [] plyrLetterGridLabels = new JLabel [10];
 {
  for (n = 0; n < 10; n ++)
  {
    plyrLetterGridLabels[n] = new JLabel (letterGrids[n]);
 
  }
 
 }
 
 JLabel NPCCornerGridLabel = new JLabel(cornerGrid);
 JLabel [] NPCNumberGridLabels = new JLabel[10]; 
 {
  //The first 11 grids are an empty grid followed by letters A-J
  for (n = 0; n < 10; n++)
  {
   NPCNumberGridLabels[n] = new JLabel(numberGrids[n]);
  }
 }
 JLabel [] NPCLetterGridLabels = new JLabel [10];
 {
  for (n = 0; n < 10; n ++)
  {
    NPCLetterGridLabels[n] = new JLabel (letterGrids[n]);
 
  }
 
 }
 
 
 JLabel[][] emptyGridLabels = new JLabel [10][10];
 {
  for (y = 0; y < 10; y++)
  {
   for (x = 0; x < 10; x++)
   {
    emptyGridLabels[x][y] = new JLabel(emptyGrid);
   }
  }
 }
 
 JButton [][] buttonGrids = new JButton [10][10];
 {
  for (y = 0; y < 10; y++)
  {
   for (x = 0; x < 10; x++)
   {
    buttonGrids[x][y] = new JButton(emptyGrid);
    buttonGrids[x][y].setPreferredSize(new Dimension(50,50));
   }
  }
 }
 
 JLabel [] NPCGrids = new JLabel[121]; //grid placements for the dealer board
 {
  for (n = 0; n < 121; n ++)
   NPCGrids[n] = new JLabel (emptyGrid);
 }
 
 JLabel [] clickableBoats = new JLabel [4];
 {
  clickableBoats[0] = new JLabel (fullBattleship);
  clickableBoats[1] = new JLabel (fullCruiser);
  clickableBoats[2] = new JLabel (fullPatrolBoat1);
  clickableBoats[3] = new JLabel (fullPatrolBoat2);

 }
 
 JButton [] plyrClickableGrids = new JButton [100];
 {
  for (n = 0; n < 99; n++);
   plyrClickableGrids[n] = new JButton(emptyGrid);
   
 }
 
 
 //Add interface components
 JTextArea playerInformation = new JTextArea(20,5);
 JScrollPane textPane1 = new JScrollPane (playerInformation);
 JButton playTurnBtn = new JButton ("Play Turn");

 //add JPanels
 JPanel plyrBoard = new JPanel (new GridLayout(11,11));
 JPanel infoPanel = new JPanel(new GridLayout(1,1));
 JPanel NPCBoard = new JPanel(new GridLayout(11,11));
 JPanel inputPanel = new JPanel(new GridLayout(1,10));
 
 public Interface ()
 {
  super ("Battleships"); 
  setSize (1367,729); 
  setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
  
  //Set the background color
  Container contentArea = getContentPane(); 
 
  //Set each panel to "opaque", so that the background becomes visible
  plyrBoard.setOpaque(false);
  NPCBoard.setOpaque(false);
  inputPanel.setOpaque(false);
  setVisible (true);

  
  playerInformation.setEditable(false); 

        playTurnBtn.setPreferredSize(new Dimension(1, 141));

     
  //Add panels to different compass points on the content area
  contentArea.add("West", plyrBoard);
  contentArea.add("Center", infoPanel);
  contentArea.add("East", NPCBoard);
  contentArea.add("South", inputPanel);
  
  //Add imageLabels and buttons to panels
 infoPanel.add(playerInformation);
  
  for (n = 0; n < 4; n++)
  {
   inputPanel.add(clickableBoats[n]);
  }
  inputPanel.add(playTurnBtn);
  
  placePlyrGrids();
  numberCounter = 0;
  placeNPCGrids();
        
 
 

  
  
  // TODO Vanity 
  //Button format
  
 
 }
 
 public void placePlyrGrids()
 {
     plyrBoard.add(plyrCornerGridLabel); //add an empty grid to the top left
       //NPCBoard.add(cornerGridLabel);
       for(n = 0; n < 10; n++)
        plyrBoard.add(plyrLetterGridLabels[n]); //the first row is the first 10 letters of the alphabet
       //NPCBoard.add(NPCLetterGridLabels[n]);

  for (y = 1; y < 11; y++) //For every (y) row...
     {
      
      for (x = 0; x < 11; x++) //...add ten (x) grids to make columns
      {
       if (x == 0) //To the start of each row, add a number (image JLabel)
       {
        plyrBoard.add(plyrNumberGridLabels[numberCounter]);
        //NPCBoard.add(NPCNumberGridLabels[numberCounter]);
        numberCounter++;
       }
   
       else //for the rest of each row, add buttons
       {
        plyrBoard.add(buttonGrids[y-1][x-1]);
        //NPCBoard.add(emptyGridLabel);

       }
        
      }
      
     }
 }
 
 public void placeNPCGrids()
 {
  NPCBoard.add(NPCCornerGridLabel);
     for(n = 0; n < 10; n++)
      NPCBoard.add(NPCLetterGridLabels[n]);

     for (y = 1; y < 11; y++) 
     {
     
      for (x = 0; x < 11; x++)
      {
       if (x == 0)
       {
        NPCBoard.add(NPCNumberGridLabels[numberCounter]);
        numberCounter++;
       }
  
       else
       {
       
        NPCBoard.add(emptyGridLabels[x-1][y-1]);

       }
       
      }
     
     }
 }
    

 

 public void actionPerformed(ActionEvent e) 
 {
  
 }
 

}

问题在于,在所有代码中,您总是引用同一个对象,例如

public void placeBoards()
{
    //Both player board and npc board are going to have the same cornerGirdLabel, you should be creating two separate instances of it
    plyrBoard.add(cornerGridLabel); //add an empty grid to the top left
    NPCBoard.add(cornerGridLabel);

    //Rest of the code
}

在你的代码中,你一直在使用对相同对象的引用,当你想要构建两个具有自己的网格的板时,这对你没有帮助!

将您的棋盘建筑分成不同的 classes 并让它们创建自己的这些对象的实例可能会更好,尤其是为了提高可读性,而不是试图将所有这些都放在一个 class 用你的主要方法。查看一些最佳设计实践文档。例如,他们有一些不错的建议 here