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

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

}

最佳答案

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

您可以更改 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();
        }

    }

}

关于java - 将数组从一个类调用到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29270407/

相关文章:

Java/Selenium (ieDriver) - 如何避免在每次测试运行中重复 webapp 登录 Before Method (jUnit)

java - 如何判断数组中的某个项是否也是数组?

java - 在 Java 中将用户输入的单词转换为 unicode 数组

java - JPanel 中的 PaintComponent 未被调用

java - 验证页码/打印范围

java - 在字符串数组中搜索子字符串?

java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException中的异常和JTable问题

javascript - 在 es6 map 上调用 `Array.prototype.some()` 的更好方法

java - 多个showMessageDialog可以断swing吗?

java - 将按键绑定(bind)到 JButton