java 如何修改按钮的文本,如果它满足某些期望?

emeijp43  于 12个月前  发布在  Java
关注(0)|答案(2)|浏览(266)

我是JavaFX的新手,我做了一个5X5的网格。我想让它在用户点击按钮时检查字符串是否以a开头。如果是,我想把文字改成“X”。它没有做任何其他的事情。为了构建网格,我使用了一个按钮分配。我现在只有在最后一个按钮以a开头时才能工作。我是否只需要单独制作每个按钮,还是有别的办法
gid是如何制作的ps板是一个先前制作的字符串[][]

for(int i = 0; i < board.length; i++) {
    for(int j = 0; j < board[i].length; j++) {
        word = new Button(board[i][j]);
        grid.add(word, i, j);
    }
}
private class MouseClickedHandler implements EventHandler<MouseEvent>{
    @Override
    public void handle(MouseEvent arg0) {
        // TODO Auto-generated method stub
        String text = word.getText();
        if (text.charAt(0) == 'A' || text.charAt(0) == 'a') {
           word.setText("X");
        }       
    }

字符串

brtdzjyr

brtdzjyr1#

如何有条件地执行按钮的操作:
1.为按钮定义操作处理程序。
1.在操作处理程序中,进行测试以查看是否满足所需的条件。
1.如果测试通过,则执行所需的操作。

简短示例

final Button button = new Button(word);
button.setOnAction(e -> {
    if (isRequiredWord(word)) {
        button.setText(MATCHED_VALUE);
    }
});

字符串

申请样本

显示带有单词的按钮网格。如果用户单击带有以字母“a”开头的单词(不区分大小写)的按钮,则按钮文本将从显示的单词更改为“X”。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MatchingGameApp extends Application {
    private static final String MATCHED_VALUE = "X";
    private static final String[][] animalBoard = {
            { "cat", "ape" },
            { "aardvark", "dog" }
    };

    /**
     * Add actionable buttons to a grid.
     *
     * @param board A 2D array of words.
     * @param grid a GridPane to which buttons for each word will be added.
     */
    private void addButtons(String[][] board, GridPane grid) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                final String word = board[i][j];

                final Button button = new Button(word);
                button.setPrefWidth(80);
                button.setOnAction(e -> {
                    if (isRequiredWord(word)) {
                        button.setText(MATCHED_VALUE);
                    }
                });

                grid.add(button, i, j);
            }
        }
    }

    /**
     * A required word is any word starting with a letter "a" in any case.
     */
    public boolean isRequiredWord(String word) {
        return word != null && !word.isEmpty() &&
                word
                        .substring(0, 1)
                        .toLowerCase()
                        .startsWith("a");
    }

    @Override
    public void start(Stage stage) {
        GridPane layout = new GridPane(10, 10);
        layout.setPadding(new Insets(10));

        addButtons(animalBoard, layout);

        stage.setScene(new Scene(layout));
        stage.show();
    }

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

cigdeys3

cigdeys32#

我想用一个EventHandler变量来展示这个例子,所以我修改了@jewelsea代码。

短代码

final EventHandler<ActionEvent> buttonActionEventnHandler =
        (actionEvent) -> 
        {
            Button tempButton = (Button)actionEvent.getSource();
            
            if (isRequiredWord(tempButton.getText())) 
            {
                tempButton.setText(MATCHED_VALUE);
            }
        };

字符串

完整代码

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class App extends Application {
    private static final String MATCHED_VALUE = "X";
    private static final String[][] animalBoard = {
            { "cat", "ape" },
            { "aardvark", "dog" }
    };

    /**
     * Add actionable buttons to a grid.
     *
     * @param board A 2D array of words.
     * @param grid a GridPane to which buttons for each word will be added.
     */
    private void addButtons(String[][] board, GridPane grid) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                final String word = board[i][j];

                final Button button = new Button(word);
                button.setPrefWidth(80);
                button.setOnAction(buttonActionEventnHandler);

                grid.add(button, i, j);
            }
        }
    }    

    @Override
    public void start(Stage stage) {
        GridPane layout = new GridPane(10, 10);
        layout.setPadding(new Insets(10));

        addButtons(animalBoard, layout);

        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    
    final EventHandler<ActionEvent> buttonActionEventnHandler =
        (actionEvent) -> 
        {
            Button tempButton = (Button)actionEvent.getSource();
            
            if (isRequiredWord(tempButton.getText())) 
            {
                tempButton.setText(MATCHED_VALUE);
            }
        };

    /**
     * A required word is any word starting with a letter "a" in any case.
     * @param word
     * @return 
     */
    public boolean isRequiredWord(String word) {
        return word != null && !word.isEmpty() &&
                word
                        .substring(0, 1)
                        .toLowerCase()
                        .startsWith("a");
    }
}

输出

x1c 0d1x的数据

相关问题