简体   繁体   English

从命令行到GUI

[英]From command line to GUI

What is the right approach to end up with a GUI Java program? 以GUI Java程序结束的正确方法是什么? Explain: I want to make a program that has a gui. 说明:我要制作一个带有GUI的程序。 Should i complete my program as command line aplplication and then creating the gui? 我应该以命令行应用程序的形式完成程序,然后创建gui吗? Or start with ex WindowBuilder, Java Fx and make it as a GUI application from the beggining? 还是从ex WindowBuilder,Java Fx开始并从一开始就使其成为GUI应用程序? Assume that i have a user input using Scanner. 假设我有使用扫描仪的用户输入。 When i want to end up with GUI should i have to change all the code to take input from JTextField? 当我想使用GUI结束时,是否必须更改所有代码以从JTextField输入? Be more specific: I have the following code 更具体地说:我有以下代码

    //Add task to the List
    public void AddToList() {
    myList.add(task.setTaskDesc());
    System.out.println("New item " + task.tascDesc + " is added in your task list!");
}

The AddToList method takes user (scanner input) and creates a task added in the list. AddToList方法采用用户(扫描仪输入)并创建一个添加到列表中的任务。 So i have to change ALL the code to become GUI? 所以我必须更改所有代码才能成为GUI? Its a general purpose question jast to understand wich is the normal way to make a full program as most of the programs need a GUI. 理解其普遍性问题是制作完整程序的通常方法,因为大多数程序都需要GUI。

It is best to seperate your algorithm/logic from the user interface (UI). 最好将算法/逻辑与用户界面(UI)分开。 So all input should be passed as function parameter to your logic. 因此,所有输入都应作为函数参数传递给逻辑。 Later to you can then decide wether it comes from a graphical user interface (GUI) or is passed over from the commandline. 之后,您可以决定它是来自图形用户界面(GUI)还是从命令行传递过来。 Also the output should be a UI independend datastructure. 输出也应该是UI独立数据结构。 Then you can visualize it later both as plan Text or graphically. 然后,您以后可以将其可视化为计划文本或图形。

Well, i can tell you from my experience what i consider as a good option. 好吧,根据我的经验,我可以告诉您我认为是不错的选择。 You want to use the terminal or the console with System class which i consider a good option but only when you have a consola or a terminal. 您想将终端或控制台与System类一起使用,我认为这是一个不错的选择,但前提是您有consola或终端。 Well, you can use the terminal input arguments and pass them to the main function to other function wich is used in the controller class of you JavaFX application. 好了,您可以使用终端输入参数,并将其传递给JavaFX应用程序的控制器类中使用的其他函数。 In this case i will recomend you use only the args as a alternative mode in the inputs that your program can manage. 在这种情况下,我建议您在程序可以管理的输入中仅将args用作替代模式。 In case that you have used nmap application as example, you can see that the inputs from the terminal can be provided from the terminal to the GUI application. 如果以nmap应用程序为例,则可以看到可以从终端向GUI应用程序提供来自终端的输入。 But i don't recommend you to use one terminal application or one terminal input for passing values to the GUI terminal, i mean you can but i don't recommend you. 但是我不建议您使用一个终端应用程序或一个终端输入将值传递给GUI终端,我的意思是您可以,但我不建议您。 In case that you want, you can pass values from the main function to as args value to the controller class and manage what do you want, or using System.in or what you want to the java controller class you can do it. 如果需要,可以将主函数中的值作为args值传递给控制器​​类,并管理所需的内容,或者使用System.in或所需的内容传递给java控制器类,就可以做到这一点。

But the best option is to use sceneBuilder for bulding the application and gives the logic to the javaFXController.java class which is in charge to manage the values. 但是最好的选择是使用sceneBuilder来构建应用程序,并将逻辑提供给负责管理值的javaFXController.java类。 The first step: To build the components from sceneBuilder always providing the fx:id and the handler name for the component. 第一步:要从sceneBuilder构建组件,请始终提供组件的fx:id和处理程序名称。

Second step: Manage the values that you want from the GUI Third Step: Manage the values from the terminal to the application but it is trivial 第二步:从GUI管理所需的值第三步:管理从终端到应用程序的值,但这很简单

Good luck! 祝好运!

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

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