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

时间:2015-03-26 03:04:21

标签: java arrays swing 2d

我正在创造一个蛇和梯子游戏。我的问题是我有两个类,一个是用于游戏的JFrame中的主GUI,带有蛇和梯子板的图像,另一个是我希望在棋盘游戏上叠加的网格的2D数组,所以图像中的正方形与网格的正方形匹配。

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

提前致谢

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


    }



}

;

网格类:

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

}

1 个答案:

答案 0 :(得分:1)

我猜测GameBoard需要一个实例Grid才能知道游戏块的放置位置。

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

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

然后在创建Grid的实例时创建并传递GameBoard的实例...

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添加一些功能来控制它的修改方式,但这就是我

  

我似乎无法使用您的原始方法使其工作。我想简单地创建一个实例,但对于我的生活,我似乎无法让它工作

似乎对我有用......

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

    }

}