簡體   English   中英

(JAVA戰艦)按鈕/圖像網格工作正常,但是當我在其旁邊添加第二個網格時,它們都被弄亂了

[英](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面板在東側面板中(以及中面板和南面板中的一些其他信息和輸入方式)。

我首先添加了如下的播放器板(完整的代碼也可以在本文的底部找到):

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

這很好。

然后我嘗試以兩種方式添加發牌人板,但似乎都無法正常工作,因為它們都被弄亂了,而且似乎也弄亂了播放器板,這似乎可以正常工作。

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 AJ 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 AJ 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) { } } 

問題在於,在所有代碼中,您始終都引用同一個對象EG

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
}

從頭到尾,您一直在使用對相同對象的引用,當您要使用自己的網格構建兩個板時,這對您沒有幫助!

最好是將可讀性更好,尤其是為了提高可讀性,將您的開發板划分為不同的類,並讓它們創建這些對象的自己的實例,而不是嘗試將所有這些都適合於您的main方法。 查看一些最佳設計實踐文檔。 例如, 這里有一些不錯的提示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM