繁体   English   中英

为什么我看不到我的项目中的更改,但我在 fxml 文件中看到了它们?

[英]Why can't I see the changes in my project but I see them in the fxml file?

我在 JavaFX 中的项目有问题。 我使用 SceneBuilder 将所有更改保存到 fxml 文件,但是当我运行项目时,我只看到空的背景,甚至工作区的大小也没有改变。 我尝试清理项目和工作区,但问题是一样的。 我有刷新项目和所有文件,什么都没有,我选择“使用本机挂钩或池刷新”,但我一直遇到同样的问题。 有没有人有其他想法? 我使用 java 13 和 JavaFX 11

主窗口文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"  prefWidth="600.0"   xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com   /fxml/1" fx:controller="application.MainWindowController">
<children>
<Label fx:id="labelText" layoutX="286.0" layoutY="42.0"   prefHeight="17.0" prefWidth="0.0" text="" />
<Button fx:id="button1" layoutX="270.0" layoutY="82.0"  mnemonicParsing="false" text="OK" />
</children>
</Pane>

主类

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root);
             scene.getStylesheets().add(getClass()
                  .getResource("mainWindow.fxml").toExternalForm());
            primaryStage.setTitle("Test");
            primaryStage.setScene(scene);

            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

MainWindowController 类

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class MainWindowController implements Initializable {

    @FXML
    Label labelText;
    @FXML
    Button button1;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        labelText.setText("Start");
    }

}

我认为问题出在您的主要启动方法中。 试试这个实现。

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
            primaryStage.setTitle("Test");
            primaryStage.setScene(new Scene(root, 600, 400));
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是你所期望的吗?

暂无
暂无

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

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