为什么我在JavaFX中没有任何缩放转换?

mum43rcc  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(399)

我一直在尝试使用javafx对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);
    }
}

我的控制器类:

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>
oalqel3c

oalqel3c1#

从javafx.fxml.initializable的文档中: FXMLLoader 现在将自动调用任何适当注解的no arg initialize() 由控制器定义的方法。
注意no arg部分。你需要定义一个 initialize() 接受零参数的方法。您当前的initialize方法不满足所述的要求,因此永远不会调用它。
更改此项:

public void initialize(Stage stage) throws IOException {

对此:

public void initialize() throws IOException {

相关问题