我正在尝试开发老虎机卷轴应用程序。我有一个自定义卷轴窗格,可垂直添加子项。单击旋转按钮时,子项必须移动,而最后一个子项到达边界时,子项必须将其位置移动到第一个子项上方。我所做的如下所示。
public class ReelPane extends Pane {
Timeline timeline = new Timeline();
@Override
protected void layoutChildren() {
List<Node> managed = getChildren();
double y = 0;
for (Node node : managed) {
node.setLayoutX(0);
node.setLayoutY(y);
y += node.getBoundsInLocal().getHeight();
}
}
public void spin() {
List<Node> managed = getChildren();
double dy = 4;
for (Node node : managed) {
timeline.getKeyFrames().addAll(new KeyFrame(Duration.millis(2000),new KeyValue(node.layoutYProperty(),node.getLayoutY()+dy)));
if(node.getLayoutY()>=600){
node.setLayoutY(-50);
}
}
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
}
fxml文件
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.Pane?>
<?import sample.ReelPane?>
<Pane stylesheets="@css/slot.css"
xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx"
fx:controller="sample.Controller">
<ReelPane fx:id="reel" styleClass="container">
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/apple.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/diamond.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/glass.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/grape.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="/sample/resources/star.png"/>
</ImageView>
</ReelPane>
<Button fx:id="spin" text="SPIN"/>
</Pane>
控制者
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
@FXML
ReelPane reel;
@FXML
Button spin;
public void initialize() {
spin.setOnAction(event -> reel.spin());
}
}
主要
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("resources/fxml/slot.fxml"));
primaryStage.setScene(new Scene(root, 400, 900));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
但是单击按钮时孩子们没有动。有人可以告诉我出了什么问题吗?
用于所需动画的更简单的工具是PauseTransition
。
如果您不想在fxml中指定布局,则可以像以前那样覆盖layoutChildren(),但是您需要禁用自动布局(layoutChildren()),以便动画可以更改位置:
import java.util.List;
import javafx.animation.PauseTransition;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
public class ReelPane extends Pane {
private List<Node> managed;
private double dy, numberOfCchildren;
private boolean isAnimating = false;
private static final double PAUSE = 1;
@Override
protected void layoutChildren() {
if(isAnimating) return;
managed = getChildren();
numberOfCchildren = managed.size();
double y = 0;
for (Node node : managed) {
node.setLayoutX(0);
node.setLayoutY(y);
dy = Math.max(dy, node.getBoundsInLocal().getHeight()); //dy stores the highest
y += dy;
}
}
public void spin() {
isAnimating = true;
PauseTransition pause = new PauseTransition(Duration.seconds(PAUSE));
pause.setOnFinished(event ->{
for (Node node : managed) {
if(node.getLayoutY()>= (numberOfCchildren -1) * dy){
node.setLayoutY(-dy);
}
node.setLayoutY(node.getLayoutY() +dy);
}
pause.play();
});
pause.play();
}
}
考虑在旋转时禁用按钮:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
@FXML
ReelPane reel;
@FXML
Button spin;
public void initialize() {
spin.setOnAction(event -> {
reel.spin();
spin.setDisable(true);
});
}
}
为了使代码MRE这里是slot.fxml
使用公开可用的资源:
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.Pane?>
<?import fx_tests.ReelPane?>
<Pane
xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx"
fx:controller="fx_tests.sample.Controller">
<ReelPane fx:id="reel">
<ImageView fitHeight="100" fitWidth="100">
<Image url="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Green.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100" >
<Image url="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Red.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Yellow.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100" >
<Image url="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Blue.png"/>
</ImageView>
<ImageView fitHeight="100" fitWidth="100">
<Image url="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Box_Orange.png"/>
</ImageView>
</ReelPane>
<Button fx:id="spin" text="SPIN" layoutX="20." layoutY="550.0" />
</Pane>
一个测试应用程序:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("slot.fxml"));
primaryStage.setScene(new Scene(root, 200, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我已经在ReelPane layoutChildren方法```protected void layoutChildren(){List <Node> managed = getChildren();中指定了布局值。双y = 0; 对于(节点节点:托管){node.setLayoutY(y); node.setLayoutX(0); y + = node.getBoundsInLocal()。getHeight(); }}
运行我发布的代码。这是工作。
我正在尝试开发自定义卷轴窗格,在其中可以添加图像和按钮,而无需指定layoutx和layouty值。我已经添加了用于垂直添加子代的代码,就像在layoutchildren方法内的vbox中一样。之后,我运行了相同的动画代码,但它不起作用
“ 而未指定layoutx和layouty值 ”我在帖子中看不到这一要求。另外还有
Reel
一个Pane
而不是a,VBox
因此您需要设置位置。动画可以用来实现,VBox
但需要不同的方法。ReelPane是一个窗格,该窗格使用其中的layoutchildren()方法垂直添加子项。所以我不想明确指定layoutx和layouty值,而是在reelpane.java中添加了layoutchildren代码