简体   繁体   English

从一个类调用数组到另一个类

[英]Calling an array from one class onto another

I am creating a snakes and ladders game. 我正在创建蛇和梯子游戏。 My issue is that I have two classes, one the main GUI in a JFrame for the game with an image of a snakes and ladders board, and the other a 2D array of a grid which I want to superimpose over the board game, so the squares in the image match the squares of the grid. 我的问题是我有两个类,一个类是游戏的JFrame中的主GUI,带有蛇形和梯子板的图像,另一个类是我要叠加在棋盘游戏上的2D网格数组,因此图像中的正方形与网格的正方形匹配。

I figure I need to call it as an instance of the Grid class, but I cant seem to get it to work (or perhaps, placed in the correct position!). 我认为我需要将其作为Grid类的实例进行调用,但是我似乎无法使其正常工作(或放置在正确的位置!)。 Can anybody help me? 有谁能够帮助我?

Thanks in advance 提前致谢

GameBoard class: GameBoard类:

public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel playerNumber;
private ButtonGroup group;
private JButton startButton;
private JRadioButton fourPlayer;
private JRadioButton threePlayer;
private JRadioButton twoPlayer;


/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            GameBoard inst = new GameBoard();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public GameBoard() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);
        {
            Board = new JLabel();
            getContentPane().add(Board);
            Board.setText("jLabel1");
            Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
            Board.setBounds(199, 0, 742, 484);
        }
        {
            playerNumber = new JLabel();
            getContentPane().add(playerNumber);
            playerNumber.setText("Number of Players");
            playerNumber.setBounds(40, 22, 117, 27);
        }
        {
            twoPlayer = new JRadioButton();
            getContentPane().add(twoPlayer);
            twoPlayer.setText("Two Player");
            twoPlayer.setBounds(40, 55, 93, 20);
        }
        {
            threePlayer = new JRadioButton();
            getContentPane().add(threePlayer);
            threePlayer.setText("Three Players");
            threePlayer.setBounds(40, 76, 88, 20);
        }
        {
            fourPlayer = new JRadioButton();
            getContentPane().add(fourPlayer);
            fourPlayer.setText("Four Players");
            fourPlayer.setBounds(40, 99, 82, 20);
        }
        {
            startButton = new JButton();
            getContentPane().add(startButton);
            startButton.setText("Start Game");
            startButton.setBounds(43, 136, 83, 23);
        }

        {
         //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(twoPlayer);
        group.add(threePlayer);
        group.add(fourPlayer);
        }

        pack();
        this.setSize(963, 523);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }


    }



}

;

Grid class: 网格类别:

public class Grid {

int[][] multi = {
        {0,0,-1,0,0,-1,0,-1,0,0},
        {0,0,0,0,0,0,-1,0,0,0},
        {0,0,0,0,0,0,0,0,0,1},
        {0,-1,0,-1,0,0,0,0,0,0},
        {0,0,0,0,0,0,-1,0,0,1},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {1,0,0,0,0,0,0,1,0,0},
        {0,0,0,-1,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,1,0}
};

} }

I'm guessing that GameBoard will need an instance Grid in order for it to know where to place the game pieces. 我猜想GameBoard需要一个实例Grid ,以便它知道将游戏部件放置在何处。

You could change GameBoard so that it required an instance of Grid to be passed to it... 您可以更改GameBoard ,使其需要将Grid实例传递给它。

public class GameBoard extends javax.swing.JFrame {
    //...
    private Grid grid;
    public GameBoard(Grid grid) {
        this.grid = grid;
        //...

Then create and pass an instance of Grid when you create an instance of GameBoard ... 然后在创建GameBoard实例时创建并传递Grid实例。

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Grid grid = new Grid();
            GameBoard inst = new GameBoard(grid);
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

But, I'd also add some functionality to Grid to control how it's modified, but that's me 但是,我还要在Grid添加一些功能以控制其修改方式,但这就是我

I cant seem to get it work using your original method. 我似乎无法使用您的原始方法来工作。 I would like to just simply create an instance but for the life of me I can't seem to get it working 我只想简单地创建一个实例,但就我的一生而言,我似乎无法使其正常运行

Seems to work okay for me... 似乎对我来说还可以...

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class GameBoard extends javax.swing.JFrame {

    private JLabel Board;
    private JLabel playerNumber;
    private ButtonGroup group;
    private JButton startButton;
    private JRadioButton fourPlayer;
    private JRadioButton threePlayer;
    private JRadioButton twoPlayer;

    private Grid grid;

    /**
     * Auto-generated main method to display this JFrame
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Grid grid = new Grid();
                GameBoard inst = new GameBoard(grid);
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public GameBoard(Grid grid) {
        super();
        this.grid = grid;
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                Board = new JLabel();
                getContentPane().add(Board);
                Board.setText("jLabel1");
                Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
                Board.setBounds(199, 0, 742, 484);
            }
            {
                playerNumber = new JLabel();
                getContentPane().add(playerNumber);
                playerNumber.setText("Number of Players");
                playerNumber.setBounds(40, 22, 117, 27);
            }
            {
                twoPlayer = new JRadioButton();
                getContentPane().add(twoPlayer);
                twoPlayer.setText("Two Player");
                twoPlayer.setBounds(40, 55, 93, 20);
            }
            {
                threePlayer = new JRadioButton();
                getContentPane().add(threePlayer);
                threePlayer.setText("Three Players");
                threePlayer.setBounds(40, 76, 88, 20);
            }
            {
                fourPlayer = new JRadioButton();
                getContentPane().add(fourPlayer);
                fourPlayer.setText("Four Players");
                fourPlayer.setBounds(40, 99, 82, 20);
            }
            {
                startButton = new JButton();
                getContentPane().add(startButton);
                startButton.setText("Start Game");
                startButton.setBounds(43, 136, 83, 23);
            }

            {
                //Group the radio buttons.
                ButtonGroup group = new ButtonGroup();
                group.add(twoPlayer);
                group.add(threePlayer);
                group.add(fourPlayer);
            }

            pack();
            this.setSize(963, 523);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }

    }

}

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

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