简体   繁体   English

将基于文本的 UI 传输到 JavaFX GUI

[英]Transferring a text-based UI to a JavaFX GUI

I'm a beginner programmer learning Java and I am making a simpler, university-themed version of Monopoly.我是一名初学者程序员,正在学习 Java,我正在制作一个更简单的、以大学为主题的 Monopoly 版本。 I currently have a fully functional text-based version of the game, and I now want to implement a JavaFX GUI while still being able to use the text-based version.我目前有一个功能齐全的基于文本的游戏版本,我现在想要实现 JavaFX GUI,同时仍然能够使用基于文本的版本。

The repository of the text-based version can be found here: https://github.com/desobob11/TeamP-Project/tree/Arnuv可以在此处找到基于文本的版本的存储库: https://github.com/desobob11/TeamP-Project/tree/Arnuv

A quick rundown of how the code works: there is a Tile class that represents a tile on the board, which is inherited by all of the specific types of tiles: Chance, Community, Go, Parking, Probation (sort of like jail), and Course (equivalent of property in regular monopoly).代码如何工作的快速概述:有一个 Tile class 代表板上的一个瓷砖,它被所有特定类型的瓷砖继承:机会,社区,Go,停车,缓刑(有点像监狱),和课程(相当于普通垄断中的财产)。 Then there is a Student class, which is the equivalent of a player, and a CourseList class, which acts as a container for all of the tiles.然后有一个 Student class,它相当于一个播放器,还有一个 CourseList class,它充当所有图块的容器。 The Application acts as the "brain" of the program and incorporates all of these classes, and finally there is the UI aspect, which is what I need help with:应用程序充当程序的“大脑”,包含所有这些类,最后是 UI 方面,这是我需要帮助的:

I made a UI interface that contains all of the methods that the back-end code needs for I/O, that way the back-end doesn't care if the UI is text-based or graphical.我制作了一个 UI 界面,其中包含后端代码进行 I/O 所需的所有方法,这样后端就不会关心 UI 是基于文本的还是图形的。 Then, my TextVisualizer class implements this.然后,我的 TextVisualizer class 实现了这个。 I wanted it so that when I make my GUI, I would simply implement UI again and rewrite all of these UI methods in terms of JavaFX functions.我想要它,这样当我制作我的 GUI 时,我只需再次实现 UI 并根据 JavaFX 函数重写所有这些 UI 方法。 I would ideally want it to be so that I ask the user if they want a text-based or graphical UI, then pass the appropriate object to my Application and let it run from there.理想情况下,我希望它是这样我询问用户是否需要基于文本或图形的 UI,然后将适当的 object 传递给我的应用程序并让它从那里运行。 For the graphics, I wanted to have a window for the board, and separate windows for each student's stats (money, courses owned, etc.).对于图形,我想为板子设置一个 window,并为每个学生的统计数据(金钱、拥有的课程等)单独设置 windows。 I have the graphics set up for the board and the courses, which are simply PNGs.我为董事会和课程设置了图形,它们只是 PNG。 I am very lost on how to even start constructing this, as my knowledge of how JavaFX works is quite limited.我什至不知道如何开始构建它,因为我对 JavaFX 如何工作的了解非常有限。 If someone could point me in the direction of the classes and code necessary to set this up, I would be very grateful.如果有人能指出我设置它所需的类和代码的方向,我将不胜感激。 Thanks!谢谢!

The question is pretty broad but I can give you an overview of some basic concepts of JavaFX.这个问题相当广泛,但我可以给你一个关于 JavaFX 的基本概念的概述。

  • First of all, make sure all your functions are working properly in the text-based version.首先,确保您的所有功能在基于文本的版本中都能正常工作。
  • Each screen of your application will have a separate FXML file.应用程序的每个屏幕都有一个单独的FXML文件。
  • Now you may use a tool like Gluon Scene Builder to design the layout of each of the screens.现在您可以使用像Gluon Scene Builder这样的工具来设计每个屏幕的布局。 This part is pretty easy as you just need to drag & drop the required elements.这部分非常简单,因为您只需要拖放所需的元素。
  • To link the GUI elements to your original code, you will have to make a controller class for each FXML file.要将 GUI 元素链接到您的原始代码,您必须为每个FXML文件创建一个controller class Here you can write your JavaFX functions which would merely call your original functions.在这里,您可以编写您的 JavaFX 函数,这些函数只会调用您的原始函数。
  • In the scene builder, on the right menu pane, you can find a section for code.在场景构建器的右侧菜单窗格中,您可以找到代码部分。 Here you can simply insert name of your JavaFX function next to the desired action event.在这里,您只需在所需的操作事件旁边插入 JavaFX function 的名称即可。

I suggest you start by designing the FXML files first and implementing screen transitions and then moving to the individual elements of the screen.我建议您首先设计 FXML 文件并实现屏幕转换,然后移动到屏幕的各个元素。 Do refer online tutorials to get a better grasp.请参考在线教程以更好地掌握。

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

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