gpt4 book ai didi

java - 如何使用 ImageView 创建 Javafx 图像 slider

转载 作者:行者123 更新时间:2023-12-02 02:54:35 25 4
gpt4 key购买 nike

我想创建一个图像 slider 。我有 ImageView 和静态图像。我怎样才能随着过渡改变图像而淡出。

最佳答案

请使用我的下面简单的带按钮或不带按钮的图像 slider 代码。

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ImageSliderDemo extends Application {

private List<String> list = new ArrayList<String>();
int j = 0;
double orgCliskSceneX, orgReleaseSceneX;
Button lbutton, rButton;
ImageView imageView;

@Override
public void start(Stage primaryStage) {
// images in src folder.
try {
list.add("/1.jpg");
list.add("/1.png");
list.add("/12.png");
list.add("/13.png");
list.add("/2.jpg");
list.add("/2.png");
list.add("/3.jpg");
list.add("/4.png");
list.add("/a.jpg");
list.add("/Logo.png");

GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);

lbutton = new Button("<");
rButton = new Button(">");

Image images[] = new Image[list.size()];
for (int i = 0; i < list.size(); i++) {
images[i] = new Image(list.get(i));
}

imageView = new ImageView(images[j]);
imageView.setCursor(Cursor.CLOSED_HAND);

imageView.setOnMousePressed(circleOnMousePressedEventHandler);

imageView.setOnMouseReleased(e -> {
orgReleaseSceneX = e.getSceneX();
if (orgCliskSceneX > orgReleaseSceneX) {
lbutton.fire();
} else {
rButton.fire();
}
});

rButton.setOnAction(e -> {
j = j + 1;
if (j == list.size()) {
j = 0;
}
imageView.setImage(images[j]);

});
lbutton.setOnAction(e -> {
j = j - 1;
if (j == 0 || j > list.size() + 1 || j == -1) {
j = list.size() - 1;
}
imageView.setImage(images[j]);

});

imageView.setFitHeight(100);
imageView.setFitWidth(300);

HBox hBox = new HBox();
hBox.setSpacing(15);
hBox.setAlignment(Pos.CENTER);
// hBox.getChildren().addAll(lbutton, imageView, rButton);
hBox.getChildren().addAll(imageView);

root.add(hBox, 1, 1);
Scene scene = new Scene(root, 800, 300);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}

EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent t) {
orgCliskSceneX = t.getSceneX();
}
};

public static void main(String[] args) {
launch(args);
}
}

关于java - 如何使用 ImageView 创建 Javafx 图像 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com