【问题标题】:JavaFX Text element not having changes reflected in UIJavaFX 文本元素没有反映在 UI 中的更改
【发布时间】:2019-08-07 05:46:21
【问题描述】:

问题:我正在尝试单击一个磁贴,当单击该磁贴时,它应该显示其值(文本),直到单击第二个值。单击第二个图块时,它应该显示它的值,然后删除第一个和第二个图块,除非它们的值匹配。目前显示第一个值;但是,第二个值永远不会显示在窗格中。编辑:感觉 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;
            }
        }
    }
}

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    问题是您在第二次点击时会立即切换两个图块。如果方法在 JavaFX 应用程序线程上运行(事件处理程序就是这种情况),则只有当方法返回时 GUI 的状态很重要。由于这个原因,对于不匹配的磁贴,第二个磁贴会在任何布局/渲染发生之前切换两次。

    使用PauseTransition 添加延迟:

    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)) {
                    // hide text with delay
                    PauseTransition pause = new PauseTransition(Duration.seconds(2));
                    pause.setOnFinished(e -> {
                        flip1.toggleTile();
                        flip2.toggleTile();
                        flip1 = null;
                        flip2 = null;
                    });
                    pause.play();
                }
            }
        }
    }
    

    顺便说一句:我建议遵守 java 命名约定。类型名称是以大写字母开头的驼峰式命名。包含handleTiles 的类型不符合此模式。

    【讨论】:

    • 非常感谢你的回答,你和下面的答案都很棒——他确实更进一步让它可以运行——这完全没有必要,但他似乎做到了稍加努力。另外——我认为类名是 CamelCase 而方法名是 pascalCase?
    【解决方案2】:

    您需要按照fabian's answer 中的说明添加暂停。
    您还需要对handleTile 的逻辑进行一些小改动才能得到您想要的(参见 cmets)。 下面是一个文件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();
        }
    }
    


    【讨论】:

    • 感谢您提供周到、完整和精心呈现的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多