【发布时间】: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 无穷大,正确的?<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