简体   繁体   English

将jframe(作为主类)的变量位置(作为整数)传递给另一个类进行操作

[英]Passing variable locations (as integers) from a jframe (as main class) to another class for manipulation

I have a programming professor that would like us to write a Tic Tac Toe game a specific way. 我有一位编程教授,希望我们以特定的方式来编写井字游戏。 He wants us to have the "board" class and have specific mutators within to deal with the data and keep as much info in that class as possible. 他希望我们拥有“ board”类,并在其中包含特定的变种器以处理数据并在该类中保留尽可能多的信息。 Specifically, He would like us to have a few methods where the only variable passed is an integer that has to do with the location of a variable. 具体地说,他希望我们有一些方法,其中唯一传递的变量是与变量位置有关的整数。 for example: 例如:

public char getMarkAt(int location) //would return the mark at the given location.

Would I be able to have an instantiation of my jframe within this class, and have another class which handled all other work? 我是否可以在此类中实例化我的jframe,并拥有另一个处理所有其他工作的类? For example: Have a jframe which was simply my nine boxes and a character array (let's say charArray for simplicity) full of a random character (We'll say 0 for the time being) and then get and set marks from the class by creating an instance of the class JFrame1 (named Picture) doing something like 例如:有一个jframe,它只是我的九个盒子和一个字符数组(为简单起见,用charArray表示),里面充满了一个随机字符(我们暂时说0),然后通过创建类JFrame1(名为Picture)的实例,其行为类似于

JFrame1 Picture = new JFrame1;    
Picture.charArray[location] = 'X';

Or do I have this all wrong? 还是我全都错了? I have been using JFrames as my main class in the past, and he has thrown in these mandatory passed variables to (I believe) keep us from doing so. 过去,我一直将JFrames用作我的主类,并且他将这些强制传递的变量扔了出去(我相信)可以阻止我们这样做。 Sorry for the craziness! 对不起,疯狂!

The best way to handle this situation would be to use the "Board" class to handle the data and have a sort of "Virtual" game board. 处理这种情况的最佳方法是使用“ Board”类来处理数据并拥有某种“ Virtual”游戏板。 Then use the JFrame specifically to show a physical copy of that virtual board to the user, so that the user may interact with the program. 然后,专门使用JFrame向用户显示该虚拟板的物理副本,以便用户可以与程序进行交互。 Doing this will allow all of the logic behind "Tic Tac Toe" To be left to the "Board" class, and any logic within the GUI will solely be to allow interaction between the user and the game. 这样做将使“井字游戏”背后的所有逻辑都留给“棋盘”类,并且GUI中的任何逻辑仅是为了允许用户与游戏之间进行交互。

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

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