简体   繁体   中英

JavaFX change Text by clicking on Button

Following task: I want a text with the Letter 'L' and two Button s, one to "increase" and one to "decrease" the letter. But only until the Letters 'A' or 'Z' are reached. Got to the point where I can increase/decrease the letter until the wished Letters. Only Problem: When I get to 'Z' and then click the button to go back it increases the letter one more time to '[' . I got the feeling that the text doesn't react properly to the button. Sorry if the description is confusing

public class Main extends Application implements EventHandler<ActionEvent> {
    private Character defaultLetter = 'L';

    private Button btnred;
    private Button btngreen;
    private Text text;


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

    }

    @Override
    public void start(Stage stage) throws Exception {
        btnred = new Button();
        btnred.setText("Previous");
        btnred.getStyleClass().add("green-btn-small-font");
        btnred.setOnAction(this);
        btnred.setStyle("-fx-font: 22 arial; -fx-base: #E70116;");

        btngreen = new Button();
        btngreen.setText("Next");
        btngreen.getStyleClass().add("red-btn-small-font");
        btngreen.setOnAction(this);
        btngreen.setStyle("-fx-font: 22 arial; -fx-base: #01E736;");

        text = new Text();
        text.setText(String.valueOf(defaultLetter));
        text.setStyle("-fx-font: 22 arial;");
        BorderPane pane = new BorderPane();
        pane.setTop(text);
        pane.setLeft(btngreen);
        pane.setRight(btnred);

        Scene scene = new Scene(pane);
        stage.setTitle("Hausaufgabe 1");
        stage.setX(400);
        stage.setY(200);
        stage.setHeight(500);
        stage.setWidth(500);
        stage.setScene(scene);
        stage.show();

    }


    @Override
    public void handle(ActionEvent event) {
        if ((event.getSource() == btnred) && defaultLetter >= 'A') {
            text.setText(String.valueOf(defaultLetter--));
        }
        if ((event.getSource() == btngreen) && defaultLetter <= 'Z') {
            text.setText(String.valueOf(defaultLetter++));
        }
    }
}
if ((event.getSource() == btnred) && defaultLetter > 'A') {
    defaultLetter--;
    text.setText(String.valueOf(defaultLetter));
}
if ((event.getSource() == btngreen) && defaultLetter < 'Z') {
     defaultLetter++;
     text.setText(String.valueOf(defaultLetter));
}

you need to update defaultLetter value each time

You should read the new letter after incrementing/decrementing. Furthermore you shouldn't use the same event handler for multiple buttons, if the functionality for the possible sources is completely distinct (in your case at most one of the if bodys can be entered).

Also since you check before the update to the letter, you cannot allow the increment to happen, if defaultLetter == boundChar .

Better do something like this:

ObjectProperty<Character> letter = new SimpleObjectProperty<>('L');
btnred.setOnAction(evt -> {
    char newLetter = (char) (letter.get() - 1);

    // just checking for demonstration
    // disable binding should prevent the action from triggering
    // when the following condition is false
    if (newLetter >= 'A') {
        letter.set(newLetter);
    }
});
btngreen.setOnAction(evt -> {
    char newLetter = (char) (letter.get() + 1);

    // just checking for demonstration
    // disable binding should prevent the action from triggering
    // when the following condition is false
    if (newLetter <= 'Z') {
        letter.set(newLetter);
    }
});

text.textProperty().bind(letter.asString());
btnred.disableProperty().bind(letter.isEqualTo('A'));
btngreen.disableProperty().bind(letter.isEqualTo('Z'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM