【问题标题】:Image Slider using JavaFX Error Syntax error on token(s), misplaced construct(s)图像滑块在标记上使用 JavaFX 错误语法错误,构造错误
【发布时间】:2015-04-08 10:58:03
【问题描述】:
package javafx;

import java.util.ArrayList;
import java.util.List;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ImageSlide extends Application {

    // Width and height of image in pixels
    private final double IMG_WIDTH = 600;
    private final double IMG_HEIGHT = 300;

    private final int NUM_OF_IMGS = 3;
    private final int SLIDE_FREQ = 4; // in secs

    @Override
    public void start(Stage stage) throws Exception {
        //root code
        StackPane root = new StackPane();

        Pane clipPane = new Pane();
        // To center the slide show incase maximized
        clipPane.setMaxSize(IMG_WIDTH, IMG_HEIGHT);
        clipPane.setClip(new Rectangle(IMG_WIDTH, IMG_HEIGHT));

        HBox imgContainer = new HBox();
        //image view
        ImageView imgGreen = new ImageView(new Image(getClass()
            .getResourceAsStream("\\Merged1.pngg")));
        ImageView imgBlue = new ImageView(new Image(getClass()
            .getResourceAsStream("\\Merged2.png")));
        ImageView imgRose = new ImageView(new Image(getClass()
            .getResourceAsStream("\\Merged3.png")));

        imgContainer.getChildren().addAll(imgGreen, imgBlue, imgRose);
        clipPane.getChildren().add(imgContainer);
        root.getChildren().add(clipPane);

        Scene scene = new Scene(root, IMG_WIDTH, IMG_HEIGHT);
        stage.setTitle("Image Slider");
        stage.setScene(scene);
        startAnimation(imgContainer);
        stage.show();
        }
    //start animation
        private void startAnimation(final HBox hbox) {
        //error occured on (ActionEvent t) line
        //slide action
            EventHandler<ActionEvent> slideAction = (ActionEvent t) {
                TranslateTransition trans = new TranslateTransition(Duration.seconds(1.5), hbox);
                trans.setByX(-IMG_WIDTH);
                trans.setInterpolator(Interpolator.EASE_BOTH);
                trans.play();
            };
     //eventHandler
            EventHandler<ActionEvent> resetAction = (ActionEvent t) {
                TranslateTransition trans = new TranslateTransition(Duration.seconds(1), hbox);
                trans.setByX((NUM_OF_IMGS - 1) * IMG_WIDTH);
                trans.setInterpolator(Interpolator.EASE_BOTH);
                trans.play();
            };

            List<KeyFrame> keyFrames = new ArrayList<>();
            for (int i = 1; i <= NUM_OF_IMGS; i++) {
                if (i == NUM_OF_IMGS) {
                    keyFrames.add(new KeyFrame(Duration.seconds(i * SLIDE_FREQ), resetAction));
                } else {
                    keyFrames.add(new KeyFrame(Duration.seconds(i * SLIDE_FREQ), slideAction));
                }
            }
//add timeLine
            Timeline anim = new Timeline(keyFrames.toArray(new KeyFrame[NUM_OF_IMGS]));

            anim.setCycleCount(Timeline.INDEFINITE);
            anim.playFromStart();
        }
    //call main function
        public static void main(String[] args) {
            launch(args);
        }
    }

【问题讨论】:

    标签: javafx event-handling imageview slideshow timeline


    【解决方案1】:

    线条

    EventHandler<ActionEvent> slideAction = (ActionEvent t) {
                    TranslateTransition trans = new TranslateTransition(Duration.seconds(1.5), hbox);
                    trans.setByX(-IMG_WIDTH);
                    trans.setInterpolator(Interpolator.EASE_BOTH);
                    trans.play();
                };
    

    应该是

    EventHandler<ActionEvent> slideAction = (ActionEvent t) -> {
                    TranslateTransition trans = new TranslateTransition(Duration.seconds(1.5), hbox);
                    trans.setByX(-IMG_WIDTH);
                    trans.setInterpolator(Interpolator.EASE_BOTH);
                    trans.play();
                };
    

    注意唯一添加的-&gt;。您使用的是哪个 IDE?

    【讨论】:

    • 我想把这个动画代码转换成MP4??请帮助我
    • @minku,你可能总是会问新问题,所以其他人也可以看到并回答,谁有更多的mp4经验。
    • 是的,我知道。我做什么?这是我的要求。我遇到了新问题,所以我要问一个新问题。
    猜你喜欢
    • 2013-05-20
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多