阶段中的节点顺序(Javafx)

时间:2014-10-16 20:27:23

标签: javafx

使用以下代码(JavaFX 8),我得到4个矩形(卡片)。矩形以15px移位显示。最后一张卡位于甲板顶部。我怎样才能用右键鼠标事件将另一张牌带到顶端? 换句话说,我怎样才能primaryStage重建?

package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

    private static ObservableList<Rectangle> cardList = FXCollections.observableArrayList();

    public static Rectangle getCard(int i) {

        Image card = new Image("file:C:\\Workspace\\pic\\" + i + "_12.png");

        Rectangle rect = new Rectangle(100 + i * 15, 120, 71, 96);
        rect.setFill(new ImagePattern(card));

        rect.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent t) {
                if (t.getButton().equals(MouseButton.SECONDARY)) {
                    rect.setStrokeWidth(2);
                    rect.setStroke(Color.YELLOW);
                }
            }
        });
        rect.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent t) {
                if (t.getButton().equals(MouseButton.SECONDARY)) {
                    rect.setStrokeWidth(0);
                }
            }
        });
        return rect;
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("4 cards");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 300);

        for (int i = 0; i < 4; i++) {
            cardList.add(getCard(i));
        }

        for (int i = 0; i < 4; i++) {
            root.getChildren().addAll(cardList.get(i));
        }

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

0 个答案:

没有答案