将数组从一个 class 调用到另一个

Calling an array from one class onto another

我正在制作蛇梯游戏。我的问题是我有两个 classes,一个是游戏的 JFrame 中的主要 GUI,带有蛇和梯子板的图像,另一个是我想叠加在棋盘游戏,所以图像中的方块与网格的方块匹配。

我想我需要将它作为 Grid 的一个实例来调用 class,但我似乎无法让它工作(或者也许,放置在正确的位置!)。有人可以帮助我吗?

提前致谢

游戏板class:

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


    }



}

;

网格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}
};

}

我猜 GameBoard 需要一个实例 Grid 才能知道在哪里放置游戏块。

您可以更改 GameBoard 以便它需要将 Grid 的实例传递给它...

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

然后在创建 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);
        }
    });
}

但是,我还会向 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

似乎适合我...

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

    }

}