UIに反映された変更を有さないJavaFXのテキスト要素

A_Elric:

問題:私はタイルをクリックしたときに、第2の値がクリックされるまで、それはその値(テキスト)を表示する必要があり、タイルをクリックしようとしています。第二のタイルをクリックすると、それはそれの値を表示する必要があり、その値が一致しない限り、第1と第2の両方のタイルを削除します。現時点で最初の値が示されています。しかし、第二の値は、ペインに表示されることはありません。編集:それはEventDispatchThreadが私のベストを取得する可能性があるように感じているが、私はその獣をなだめるための軽量な方法を考えることはできません。

Tile.java

public class Tile extends StackPane {
    int val;
    Text text = new Text();

    Tile(int value) {
        val = value;
        text.setText(String.valueOf(value) );
        text.setFont(Font.font(30));
        text.setVisible(false);

        setAlignment(Pos.CENTER);
        getChildren().addAll(border, text);
        setOnMouseClicked(event -> compgraphics.handleTiles(this));
    }

    public void toggleTile(){
        if(text.isVisible()){
            text.setVisible(false);
        }
        else{
            text.setVisible(true);
        }
    }
}

handleTiles()関数

public static void handleTiles(Tile t){
    if (flip1 == null) {
        flip1 = t;
        flip1.toggleTile();
        return;
    }
    if (flip2 == null) {
        flip2 = t;
        flip2.toggleTile();
        if (flip1 != null && flip2 != null) {
            if(!hasSameValue(flip1,flip2)) {
                flip1.toggleTile();
                flip2.toggleTile();
                flip1 = null;
                flip2 = null;
            }
        }
    }
}
c0der:

あなたはで説明したように、ポーズを追加する必要がありファビアンの答え
あなたはまたのロジックでいくつかの小さな変更を必要とするhandleTileあなたは(コメントを参照)欲しいものを手に入れます。以下は、1ファイルのあるMRE(あなたは、一つのファイルにコード全体をコピー&ペーストすることができFxMain.java、および実行):

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FxMain extends Application {

    private static final int COLS = 5, ROWS = 5;
    private Tile flip1, flip2;
    private boolean busy = false;

    @Override
    public void start(Stage primaryStage){
        primaryStage.setScene(new Scene(makeGrid()));
        primaryStage.show();
    }

    private Pane makeGrid() {

        GridPane grid = new GridPane();
        grid.setHgap(5); grid.setVgap(5);
        grid.setPadding(new Insets(5));

        for(int rowIndex = 0; rowIndex < ROWS ; rowIndex++) {
            //an array to hold buttons of one row
            Node[] nodes = new Node[COLS];
            for(int colIndex = 0; colIndex < COLS ; colIndex++) {
                Tile tile=  new Tile(String.valueOf(rowIndex + colIndex));
                tile.setOnMouseClicked(e->handleTiles(tile));
                nodes[colIndex]= tile;
            }
            grid.addRow(rowIndex, nodes);
        }
        return grid;
    }

    public void handleTiles(Tile t){

        if(busy) return; //ignore new clicks until previous ones were processed
        busy = true;

        if (flip1 == null) {
            flip1 = t;
            flip1.toggleTile();
            busy = false;
            return;
        }else {

            flip2 = t;
            flip2.toggleTile();
            //set delay to 0 if values match
            double duration = flip1.getValue().equals(flip2.getValue()) ? 0 : 2 ;

            PauseTransition pauseTransition = new PauseTransition(Duration.seconds(duration));
            pauseTransition.setOnFinished(e->{

                if(!flip1.getValue().equals(flip2.getValue())) {
                    flip1.toggleTile();
                    flip2.toggleTile();
                }
                flip1 = null;
                flip2 = null;
                busy = false;
            });
            pauseTransition.play();
        }
    }

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

class Tile extends StackPane {

    private final Text text;

    Tile(String value) {
        text = new Text(value);
        text.setFont(Font.font(30));
        text.setVisible(false);
        setPrefSize(50,50);
        setAlignment(Pos.CENTER);
        setStyle("-fx-border-color: black");
        getChildren().add(text);
    }

    public void toggleTile(){
        text.setVisible( ! text.isVisible());
    }

    public String getValue(){
        return text.getText();
    }
}


ここでは、画像の説明を入力します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=333808&siteId=1
おすすめ