简体   繁体   English

如何在javafx中分隔我的GUI?

[英]How to separate my gui in javafx?

Usually when I create my gui I use swing and create my own class extending jframe. 通常,当我创建GUI时,我会使用swing并创建自己的扩展jframe的类。 Then I create a controller class and base my program on mvc design. 然后,我创建一个控制器类并将程序基于mvc设计。 But I decided to try out javafx recently and I'm a bit lost. 但是我决定最近尝试使用javafx,我有点迷茫。

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. 目前,我只是在start方法上将所有gui放在我的主类中,其中所有使用过的和引用的对象都在我的主类中声明为字段。 This looks very messy and not so rightly implemented. 这看起来很凌乱,没有正确实现。 Is there anyway to do what I was previously doing with swing, with javafx? 无论如何,有没有用javafx做我以前使用swing的事情?

  • I prefer not to use fxml and don't plan on it either. 我宁愿不使用fxml,也不打算对其进行计划。

In general you you have a main class which justs starts you GUI. 通常,您有一个主类,它可以启动GUI。 The View part of you MVC is handle by an XML file (called FXML). 您的MVC的“视图”部分由XML文件(称为FXML)处理。 This FXML is connected to an controller class. 该FXML连接到控制器类。 JavaFX also allows you to directly injects all GUI components into an proper variable inside of the controller, so you don't need to look them up. JavaFX还允许您将所有GUI组件直接注入到控制器内部的适当变量中,因此您无需查找它们。 The easiest way to get an example project is using maven archetypes for JavaFX : 获得示例项目的最简单方法是使用JavaFX的Maven原型

mvn archetype:generate -DarchetypeGroupId=com.zenjava -DarchetypeArtifactId=javafx-basic-archetype

Just go to an directory where you project should be an type the above command. 只需转到您要在其中输入上述命令的目录即可。 It then directly generates an example projects for you. 然后直接为您生成示例项目。 So lets start with the main class 因此,让我们从主要课程开始

public class MainApp extends Application {

    private static final Logger log = LoggerFactory.getLogger(MainApp.class);

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

    public void start(Stage stage) throws Exception {

        log.info("Starting Hello JavaFX and Maven demonstration application");

        String fxmlFile = "/fxml/hello.fxml";
        log.debug("Loading FXML for main view from: {}", fxmlFile);
        FXMLLoader loader = new FXMLLoader();
        Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));

        log.debug("Showing JFX scene");
        Scene scene = new Scene(rootNode, 400, 200);
        scene.getStylesheets().add("/styles/styles.css");

        stage.setTitle("Hello JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }
}

As you can see it just loads the the FXMl and displays the main window. 如您所见,它仅加载FXM1并显示主窗口。 The FXML specifies the visual structure of your GUI (labels, textfields, images, buttons scroll bars etc) FXML指定GUI的视觉结构(标签,文本字段,图像,按钮滚动条等)

<MigPane id="rootPane" fx:controller="example.HelloController" styleClass="main-panel" layout="insets 20" cols="[label, pref!][grow, 50::]" rows="" xmlns:fx="http://javafx.com/fxml">
    <Label text="First Name:" />
    <TextField fx:id="firstNameField" prefColumnCount="30" MigPane.cc="growx, wrap" />
    <Label text="Last Name:" /> <TextField fx:id="lastNameField" prefColumnCount="30" MigPane.cc="growx, wrap" />
    <Button text="Say Hello" onAction="#sayHello" MigPane.cc="skip, gap :push, gaptop 15, wrap" />
    <Label fx:id="messageLabel" styleClass="hello-message" MigPane.cc="span, growx, gaptop 15" />
</MigPane >

The Controller which is used to separate the GUI from its logic is specified int the FXML and the fx:id is used to injects the element directly into the proper variable inside of the controller 在FXML中指定了用于将GUI与逻辑分离的Controller,而fx:id用于将元素直接注入到控制器内部的适当变量中

public class HelloController
{
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @FXML private TextField firstNameField;
    @FXML private TextField lastNameField;
    @FXML private Label messageLabel;

    public void sayHello() {

        String firstName = firstNameField.getText();
        String lastName = lastNameField.getText();

        StringBuilder builder = new StringBuilder();

        if (!StringUtils.isEmpty(firstName)) {
            builder.append(firstName);
        }

        if (!StringUtils.isEmpty(lastName)) {
            if (builder.length() > 0) {
                builder.append(" ");
            }
            builder.append(lastName);
        }

        if (builder.length() > 0) {
            String name = builder.toString();
            log.debug("Saying hello to " + name);
            messageLabel.setText("Hello " + name);
        } else {
            log.debug("Neither first name nor last name was set, saying hello to anonymous person");
            messageLabel.setText("Hello mysterious person");
        }
    }

}

The controller has fields with an @FXML annotation those are injected GUI elements. 控制器具有带有@FXML批注的字段,这些字段是注入的GUI元素。 As you can the the variable name equals the fx:id this is necessary in order for JavaFX to injects the elements. 您可以使变量名等于fx:id这对于JavaFX注入元素是必需的。 For creating the FXML you can use the Oracle Scene Builder or the on from Gluon both are very easy to use and allow you to quickly generate an nice looking GUI. 要创建FXML,您可以使用Oracle Scene BuilderGluon的on都非常易于使用,并且可以快速生成美观的GUI。

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. 允许定期摆动MVC设计。

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();
    }

}

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

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