[英]Calling an array from one class to another
I'm trying to create a battleship game for my school project and its still getting out of hand. 我正在尝试为我的学校项目创建一个战舰游戏,但该游戏仍然一发不可收拾。 I'm new to Java and this is getting a personal issue.
我是Java的新手,这是一个个人问题。
I have six classes, and I'm trying to pass a 2d arraylist containing my boats from my main class to another class. 我有六个班级,并且我试图将包含我的船的2D数组列表从主班级传递到另一个班级。
My Main Class: 我的主班:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Boat_Class patrol1 = new Boat_Class("PatrolBoat",2);
Boat_Class Destroyer1 = new Boat_Class("Destroyer",3);
Boat_Class Battleship1 = new Boat_Class("Battleship",4);
Boat_Class Aircraftcarrier1 = new Boat_Class("AircraftCarrier",5);
/*Boat_Class patrol2 = new Boat_Class();
Boat_Class Destroyer2 = new Boat_Class();
Boat_Class Battleship2 = new Boat_Class();
Boat_Class Aircraftcarrier2 = new Boat_Class();*/
ArrayList<Boat_Class> player1 = new ArrayList<Boat_Class>();
player1.add(patrol1);
player1.add(Destroyer1);
player1.add(Battleship1);
player1.add(Aircraftcarrier1);
/*ArrayList<Boat_Class> player2 = new ArrayList<Boat_Class>();
player2.add(patrol1);
player2.add(Destroyer1);
player2.add(Battleship1);
player2.add(Aircraftcarrier1);*/
Cell [][] GridCellP1 = new Cell [10][10];
//Cell [][] GridCellP2 = new Cell [10][10];
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
North_Panel Top = new North_Panel(player1);
frame.add(Top, BorderLayout.NORTH);
Center_Panel Center = new Center_Panel();
frame.add(Center, BorderLayout.CENTER);
South_Panel South = new South_Panel();
frame.add(South , BorderLayout.SOUTH);
/*Cell c = new Cell ();
c.setColIndex(-2);
c.setRowIndex(4);
Center.add(c);*/
}
}
The Center Panel Class were I want my arraylist to be passed through. 中心面板类是我希望通过数组列表。
public class Center_Panel extends JPanel {
public Center_Panel() {
this.setLayout(new GridLayout(1, 2, 10, 10));
JPanel panel1 = new JPanel(new GridLayout(10, 10));
panel1.setBackground(Color.BLUE);
char c = 'A';
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
JButton button = new JButton((c + " " +j));
panel1.add(button);
button.addActionListener (new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e){
}});
GridCellP1 [i][j] = button;
}
c ++;
}
this.add(panel1)
Start by declaring your Center_Panel
so that it takes a parameter of Cell[][]
... 首先声明您的
Center_Panel
,使其采用Cell[][]
的参数...
public class Center_Panel extends JPanel {
public Center_Panel(Cell[][] cells) {
Then, when you construct the Center_Panel
, pass the value to it... 然后,在构造
Center_Panel
,将值传递给它。
Cell [][] GridCellP1 = new Cell [10][10];
//...
Center_Panel Center = new Center_Panel(GridCellP1);
You will also find if you call setVisible(true)
on the frame after you've finished building the UI, you will have less issues... 完成UI构造后,如果在框架上调用
setVisible(true)
,也会发现问题更少...
You might also want to have a read through Model–view–controller as it might help you make a better design ;) 您可能还希望通读Model-View-Controller,因为它可能有助于您进行更好的设计;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.