【问题标题】:Why can't I see the changes in my project but I see them in the fxml file?为什么我在我的项目中看不到更改,但我在 fxml 文件中看到了它们?
【发布时间】:2020-01-22 15:28:40
【问题描述】:

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

mainWindow.fxml

<?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");
    }

}

【问题讨论】:

  • 对 JavaFX 的了解不够,无法提供帮助,或者即使这是一个问题,但您确实意识到您将 max/min 值设置为 negative 无穷大,正确的? &lt;Pane maxHeight="-Infinity" maxWidth="-Infinity"
  • @Christopher Schneider 没关系,我可以清理 .fxml 文件或写最糟糕的愚蠢的东西,效果是一样的。不改变工作区。
  • @ChristopherSchneider 这实际上是具有特殊含义的常量:Region.USE_PREF_SIZE
  • @programbeginer0235 您是否仔细检查过资源在类路径中是否实际更新? try(InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream("mainWindow.fxml")); BufferedReader br = new BufferedReader(isr)) { String l; while ((l = br.readLine()) != null) System.out.println(l);}?
  • @fabian 是的,我已经检查过他在类路径中。

标签: java eclipse javafx scenebuilder


【解决方案1】:

我认为问题出在您的 main.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();
        }
    }

This is what you expected?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2023-03-03
    • 2012-10-11
    • 1970-01-01
    • 2015-09-27
    • 2021-03-25
    相关资源
    最近更新 更多