javafx - JavaFX Animation(TranslateTransition) 未运行,但正在显示场景且没有错误消息
问题描述
我使用 java 已经一年了,对 javaFX 还是很陌生,到目前为止,我一直在学习使用 scenebuilder 的基本教程。到目前为止,我已经尝试将 translatetransition 应用于我的按钮,但它似乎根本没有移动。当我运行程序时,会显示带有背景的场景,但按钮只是停留在场景构建器中定义的起始位置并且不会移动。在检查了此站点上的其他类似问题后,我确保我实现了 Initializable 并在我的初始化函数之前添加了 @Override,并且我确保我的转换已播放。我也在一个矩形上尝试过 translatetransition,它不会移动。可能只是我使用的是 eclipse 而不是 netbeans
司机等级:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
//import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent myroot = FXMLLoader.load(getClass().getClassLoader().getResource("MyFxml.fxml"));
//BorderPane root = new BorderPane();
Scene scene = new Scene(myroot);
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
FXML
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="413.0" fitWidth="638.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../Downloads/campusmap.png" />
</image>
</ImageView>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Rectangle fx:id="myrectangle" arcHeight="5.0" arcWidth="5.0" fill="#128cff" height="200.0" layoutX="14.0" layoutY="64.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
<Button fx:id="startbutton" layoutX="202.0" layoutY="232.0" mnemonicParsing="false" prefHeight="64.0" prefWidth="197.0" style="-fx-background-color: #FFC0CB; -fx-background-radius: 100;" text="Start Program">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font></Button>
</children>
</AnchorPane>
</children>
</StackPane>
FXML 控制器
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import javafx.animation.TranslateTransition;
public class MyFxmlController implements Initializable{
@FXML
private Button startbutton;
@FXML
private Rectangle myrectangle;
@Override
public void initialize(URL url, ResourceBundle rb) {
TranslateTransition transition = new TranslateTransition();
transition.setDuration(Duration.seconds(4));
transition.setNode(startbutton);
transition.setToX(-200);
transition.setToY(-200);
transition.play();
}
}
解决方案
您没有将控制器“链接”到 FXML 文档。因此,当您显示 FXML 布局时,Transition
代码永远不会执行。
你有几个选择来做到这一点。在 SceneBuilder 中,您可以在此处指定控制器类:
这会将fx:controller
属性添加到您的 FXML 文件中:
fx:controller="temp.translate.MyFxmlController"
显然,您需要在这里使用自己的包路径。
另一个选项是通过更新 FXML 文档的加载来指定 Java 代码中的控制器。
为此,您需要获取对 的引用FXMLLoader
并将控制器设置在那里。您可以像这样更改start()
方法:
@Override
public void start(Stage primaryStage) {
try {
// Create a new FXMLLoader and set the FXML path
FXMLLoader loader = new FXMLLoader(getClass().getResource("MyFxml.fxml"));
// Set the controller for this FXML document
loader.setController(new MyFxmlController());
// Load the FXML into your Parent node
Parent myRoot = loader.load();
//BorderPane root = new BorderPane();
Scene scene = new Scene(myRoot);
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
注意:如果您在 FXML 中通过 指定了控制器fx:controller
,则不能在 Java 代码中指定它,反之亦然。你可能只在一个地方或另一个地方定义控制器,所以这真的是个人喜好,取决于你的需要。
推荐阅读
- excel - Excel - 找到包含 X 元素的最佳数量
- shell - 通过 CI 将本地无版本文件复制/提交到 SVN 版本文件
- php - 删除图像背景在 php imagick 中无法正常工作
- html - 从 Excel 公式进行 Google 搜索并将结果放入单元格中
- openedge - 访问过程库中的单例对象时访问冲突
- javascript - 在浏览器中调整 ImageData 大小的快速方法?
- javascript - 循环通过香草JS中的对象内部的对象
- reactjs - 使用 Typescript 导入 React-Select 失败
- acumatica - 如何将自定义 ZPL 标签从移动设备打印到连接到设备集线器的打印机?
- rabbitmq - RabbitMQ - “Not Before”过滤器