【问题标题】:JavaFX Animation(TranslateTransition) Not running but scene is being displayed without error messagesJavaFX Animation(TranslateTransition) 未运行,但正在显示场景且没有错误消息
【发布时间】:2021-03-10 07:51:08
【问题描述】:

我使用 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();
    
    
}

}

【问题讨论】:

    标签: javafx


    【解决方案1】:

    您没有将控制器“链接”到 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 代码中指定它,反之亦然。你可能只在一个地方或另一个地方定义控制器,所以这真的是个人喜好,取决于你的需要。

    【讨论】:

    • 谢谢!我记得在一些帖子中看到加载器是这样设置的,但我的加载器保持了我在教程中看到的方式。
    猜你喜欢
    • 2021-07-19
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2013-10-14
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多