微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的 fxml 没有在标签上显示传递的值?

如何解决为什么我的 fxml 没有在标签上显示传递的值?

我正在尝试制作一个 javafx 应用程序并且我正在创建一个警报窗口。我希望能够在显示时传递警报窗口的标题和消息。我正在使用场景构建器进行设计,并尝试使用 java 进行编程。

启动它并调用警报的主类

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package closet.app;

import closet.app.components.AlertMessage;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Objects;

public class App extends Application {

    @Override
    public void start(Stage window) throws Exception {
        window.setTitle("Temp Title");
        window.initStyle(StageStyle.UNDECORATED);
        Scene scene = new Scene(rootParentWrapper(window));
        window.setScene(scene);
        window.show();
        AlertMessage alert = new AlertMessage();
        alert.display("title","message test");
    }

    private Parent rootParentWrapper(final Stage window) throws Exception {
        final Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("sample.fxml")));
        root.setonmousepressed(pressEvent -> root.setonMouseDragged(dragEvent -> {
            window.setX(dragEvent.getScreenX() - pressEvent.getSceneX());
            window.setY(dragEvent.getScreenY() - pressEvent.getSceneY());
        }));
        return root;
    }

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

警报消息类/控制器

package closet.app.components;

import com.jfoenix.controls.JFXButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.util.Objects;

public class AlertMessage {

    public JFXButton okButton;
    public String message;
    public String title;
    private Stage window;

    public AlertMessage() {
    }

    @FXML
    public void display(String title,String message) throws Exception{
        this.message = message;
        this.title = title;

        Stage window = new Stage();
        Parent pane = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("components/AlertMessage.fxml")));
        window.initModality(Modality.APPLICATION_MODAL);
        window.setScene(new Scene(pane));
        window.show();

    }

    @FXML
    public void handleOkButtonAction(ActionEvent event) {
        Stage stage = (Stage) okButton.getScene().getwindow();
        stage.close();
    }

}

警报窗口 fxml

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

<?import com.jfoenix.controls.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefheight="138.0" prefWidth="418.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="closet.app.components.AlertMessage">
   <children>
      <Label layoutX="40.0" layoutY="29.0" prefheight="65.0" prefWidth="352.0" text="${message}" />
      <Label layoutX="6.0" layoutY="6.0" prefheight="17.0" prefWidth="159.0" text="${title}" />
      <JFXButton fx:id="okButton" layoutX="340.0" layoutY="99.0" mnemonicParsing="false" text="Button" onAction="#handleOkButtonAction" />
   </children>
</AnchorPane>

应用程序启动,弹出警告窗口,按钮正常工作以关闭它。但是警报窗口是空白的,它没有显示我传递给 ::display 的文本。

我做错了什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。