问题描述
我想将JavaFX项目分离为模型,视图和控制器。
我在创建JavaFX应用程序时使用netbeans。
但是我想要代码分离,自己的GUI,自己的逻辑和Main类只是为了启动应用程序(我想要3个单独的类)。
但我无法解决这个问题。
自动创建的代码如下所示:
I want to seperate a JavaFX project to model, view and controller.I use netbeans when creating a JavaFX application.But I want the code seperate, an own GUI, own logic and a Main class just to start the application (I want 3 seperate classes).But I am not able to solve this problem.The automatic created code looks like this:
package at.wueschn.www;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
推荐答案
如果您使用 NetBeans
,请先选择文件 - >新项目
。然后 JavaFX - > JavaFX FXML应用程序
If you are using NetBeans
, first choose File -> New Project
. Then JavaFX -> JavaFX FXML Application
注意:这是一个基本的 MVC
设置。您可以使用纯代码完成所有这些操作。 James_D可能会帮助你提供更高级的 MCV
想法。
Note: This is a basic MVC
setup. You could do all of this using pure code. James_D could probably help you with more advanced MCV
ideas.
注意:如果你我将建议您下载可以帮助您查看视图。
Note: If you are going to take this simple approach, I suggest you download SceneBuilder
to help you with the view.Tutorial
这篇关于如何分离JavaFX应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!