我正在为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]);
}
}
}
}
这很好用。
然后我尝试以两种方式添加经销商板,其中没有一种似乎有用,因为它们都搞砸了,似乎弄乱了玩家板,它本身似乎工作得很好。
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);
}
}
}
}
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)
{
}
}
答案 0 :(得分:1)
问题在于,在所有代码中,您始终指的是同一个对象,E.G。
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方法将它们全部放在一个类中。看看一些最好的设计实践文档。例如,他们有一些很好的提示here