如何使用Java Swing和MVC进行真正的GUI开发
我正在创建一个Blackjack Card游戏模拟器。我是SCJP,熟悉Core Java概念。我对Java swings和awt有一个非常基本的了解。
已经完成了基本游戏逻辑的编写,一个CLI逻辑。我的设计包括几个类,如
经销商,玩家,桌子,卡片,赌场和其他一些..
卡片和套件的枚举。
I am creating a Blackjack Card game emulator. I am a SCJP,familiar with Core Java concepts. I have a very basic understanding of Java swings and awt. Already finished writing the basic game logic,a CLI logic. My design includes several classes like, Dealer,Player,Table,Card,Casino and some others.. Enums for Cards and suite.
我已经读过MVC作为一个理论概念,熟悉设计模式这个名称(对它们的实现方式一无所知)我建议在编写一些真正的代码时学习。所以我从这开始......
I have read about MVC as a theoretical concept, familiar with the name 'design patters'(no understanding whatsoever as to how they are implemented) Everywhere I am suggested to learn while writing some real code.So I started off with this...
我现在陷入困境,我该如何为我的项目编写代码?编写GUI代码并在已存在的代码中组织它。
I am stuck now, how should I go about writing code for my project?? Writing the GUI code and organising it within the already existent code.
我花了很多年才学习MVC(我教的不对关于它在大学的事情,另外当时许多在线资源都是错误的)。无论如何,你需要做的核心是你的模型中没有任何视图信息(即,播放器在屏幕上的样子,帧缓冲区,模型的多边形)。相反,您在单独的命名空间中创建视图和模型,然后使用事件将两者链接在一起。当模型中有时会发生这种情况时,会通知视图,并对视图进行更改。此外,当按下鼠标或按下某个键时,输入事件将转换为另一个面向模型的事件,该事件可以采用方法调用的形式进入模型。然后将模型中的任何更改反馈到视图上。
It took me ages to learn MVC (I got taught incorrect things about it in university, and additionally a lot of online sources at the time were wrong about it). Anyway, the core of you need to do is not have any view information in your model (i.e., what the player looks like on the screen, the framebuffer, the polygons of your models). Instead, you create the view and model in separate namespaces, and then use events to link the two together. When sometimes happens in your model, the view gets notified, and changes are made to the view. Additionally, when the mouse is pressed or a key is pressed, the input event gets transformed into another model-oriented event which can take the form of a method call into the model. Any changes in the model are then fed back onto the view.
请记住:模型应该在没有附加视图的情况下正常运行,并且不应在屏幕上显示任何内容执行时(控制台中的调试信息除外)。
Remember this: the model should be functional without the view being attached, and should not show anything on the screen while executing (except perhaps debug info in the console).