为什么我在 Java FX 中没有得到任何比例转换。?

Why am I NOT getting any scale transition in Java FX.?

我一直在尝试使用 Java Fx 对 imageView 进行缩放转换。我写的代码似乎没问题。但是当我 运行 这段代码时,我没有得到任何过渡效果,也没有错误,图像停留在其各自的位置。有人可以帮帮我吗???

在下面的代码中,我想对图像 resumeBtn 进行缩放转换。

import java.io.IOException;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

public class TryFXML extends Application{

    @Override
    public void start(Stage stage) throws IOException  {
        // TODO Auto-generated method stub
        try {
            Parent root = FXMLLoader.load(getClass().getResource("PauseScreen.fxml"));
            Scene scene=new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Pause Screen");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    
    stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

我的控制器class:

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.annotation.Resource;

import javafx.animation.AnimationTimer;
import javafx.animation.ScaleTransition;
import javafx.event.ActionEvent;
public class TimeController {
    
    
    
     @FXML
        private ImageView resumeBtn;
     @FXML
        private ImageView restartBtn;
     @FXML
        private ImageView saveBtn;
    
     
     
     
    @FXML
    public void initialize(Stage stage) throws IOException {
        
        
 
        ScaleTransition rsm=new ScaleTransition(Duration.seconds(5),resumeBtn);
        rsm.setAutoReverse(true);
        rsm.setCycleCount(1000);
        rsm.setFromX(1);
        rsm.setToX(4);
        rsm.setFromY(1);
        rsm.setToY(4);
        rsm.play();
        
        
    }
    
    

    
    @FXML
    void restartClicked(MouseEvent event) {
        System.out.println("you clicked here 1");
    }
    
    @FXML
    void saveClicked(MouseEvent event) {
        System.out.println("You clicked here 2");
    }
}

我的 FXML 代码::

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

<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="850.0" prefWidth="580.0" style="-fx-background-color: rgb(41,41,41);" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TimeController">
   <children>
      <ImageView fx:id="resumeBtn" accessibleRole="BUTTON" accessibleRoleDescription="Button " fitHeight="200.0" fitWidth="200.0" layoutX="190.0" layoutY="104.0" style="-fx-scale-x: 1;">
         <image>
            <Image url="@Image/PlayButton.png" />
         </image>
         <effect>
            <InnerShadow choke="0.32" color="#5e5959" height="22.56" radius="11.1675" width="24.11" />
         </effect>
      </ImageView>
      <ImageView fx:id="restartBtn" fitHeight="200.0" fitWidth="200.0" layoutX="190.0" layoutY="354.0" onMouseClicked="#restartClicked">
         <image>
            <Image url="@Image/RestartButton.png" />
         </image>
         <effect>
            <Lighting diffuseConstant="1.63" specularConstant="0.15" specularExponent="40.0">
               <bumpInput>
                  <Shadow />
               </bumpInput>
               <light>
                  <Light.Distant />
               </light>
            </Lighting>
         </effect>
      </ImageView>
      <ImageView fx:id="saveBtn" fitHeight="168.0" fitWidth="166.0" layoutX="212.0" layoutY="611.0" onMouseClicked="#saveClicked">
         <image>
            <Image url="@Image/SaveButton.png" />
         </image>
         <effect>
            <ColorAdjust brightness="0.45" contrast="0.09" hue="1.0" saturation="0.02" />
         </effect>
      </ImageView>
   </children>
</Pane>

来自the documentation of javafx.fxml.Initializable

FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller.

注意 no-arg 部分。您需要定义一个接受零参数的 initialize() 方法。您当前的初始化方法不符合规定的要求,因此永远不会被调用。

改变这个:

public void initialize(Stage stage) throws IOException {

对此:

public void initialize() throws IOException {