如何在javafx中分离我的gui? [英] How to separate my gui in javafx?

查看:147
本文介绍了如何在javafx中分离我的gui?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我创建我的gui时,我使用swing并创建我自己的类扩展jframe。然后我创建一个控制器类,并将我的程序基于mvc设计。但我最近决定试用javafx并且我有点失落。

Usually when I create my gui I use swing and create my own class extending jframe. Then I create a controller class and base my program on mvc design. But I decided to try out javafx recently and I'm a bit lost.

目前我只是在我的主要课程中使用start方法获得我所有的gui,其中所有使用和引用的对象都在我的主类中声明为字段。这看起来非常混乱,并没有如此正确地实施。反正用javafx来做我以前用swing做的事情吗?

Currently I just have my all of my gui in my main class on the start method, where all used and referenced objects are declared as fields in my main class. This looks very messy and not so rightly implemented. Is there anyway to do what I was previously doing with swing, with javafx?


  • 我不想使用fxml,也不打算使用它。

推荐答案

解决方案:创建一个扩展Scene作为参数的阶段的类。移动将在main中的所有内容。这允许常规摆动mvc设计。

Solution: Create a class that extends Scene that takes stage as param. Move everything that would be in main into start. Which allows the regular swing mvc design.

public class Main extends Application {

    public static void main(String args[]){
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {       

        CardModel model = new CardModel();
        CardView scene = new CardView(stage);       
        CardController controller= new CardController(model,scene);

        stage.setTitle("Title");
        stage.setMaximized(true);
        stage.setMinWidth(500);
        stage.setMinHeight(550);
        stage.setScene(scene);
        stage.show();
    }

}

这篇关于如何在javafx中分离我的gui?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆