JavaFX Animation(TranslateTransition) 不是 运行 但场景显示时没有错误消息
JavaFX Animation(TranslateTransition) Not running but scene is being displayed without error messages
我在 java 工作了一年,对 javaFX 还很陌生,到目前为止,我一直在学习使用 scenebuilder 的基本教程。到目前为止,我已经尝试将 translatetransition 应用于我的按钮,但它似乎根本没有移动。当我 运行 显示带有背景的场景的程序时,按钮只是停留在场景生成器中定义的开始位置并且不会移动。在检查了该站点上的其他类似问题后,我确定我在初始化函数之前实现了 Initializable 并添加了 @Override,并且我确保播放了我的过渡。我也在矩形上尝试过 translatetransition,它不会移动。可能只是因为我使用的是 eclipse 而不是 netbeans
Driver Class:
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中,您可以在此处指定控制器class:
这会将 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();
}
注意: 如果您通过 fx:controller
在 FXML 中指定了控制器,则不能在 Java 代码中也指定它,反之亦然反之亦然。您可能只在一个地方或另一个地方定义控制器,所以这真的是个人喜好,取决于您的需要。
我在 java 工作了一年,对 javaFX 还很陌生,到目前为止,我一直在学习使用 scenebuilder 的基本教程。到目前为止,我已经尝试将 translatetransition 应用于我的按钮,但它似乎根本没有移动。当我 运行 显示带有背景的场景的程序时,按钮只是停留在场景生成器中定义的开始位置并且不会移动。在检查了该站点上的其他类似问题后,我确定我在初始化函数之前实现了 Initializable 并添加了 @Override,并且我确保播放了我的过渡。我也在矩形上尝试过 translatetransition,它不会移动。可能只是因为我使用的是 eclipse 而不是 netbeans
Driver Class:
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中,您可以在此处指定控制器class:
这会将 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();
}
注意: 如果您通过 fx:controller
在 FXML 中指定了控制器,则不能在 Java 代码中也指定它,反之亦然反之亦然。您可能只在一个地方或另一个地方定义控制器,所以这真的是个人喜好,取决于您的需要。