JAVA FX - 使用按钮

时间:2016-04-10 18:07:21

标签: java javafx

这是我之前提出的一个问题。我已经把一切都搞定了。但是,当我在第一个文本字段中输入时,我需要添加一个提交按钮而不是动作。我将如何实现这一目标。

这就是我所拥有的。

    import java.util.Scanner;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class UppercaseLetters extends Application {

    public static void main(String[] args) {
        launch(args);
    }
@Override
public void start(Stage primaryStage) throws Exception {

    // Create Pane
    UpperCase pane = new UpperCase();

    primaryStage.setTitle("UpperCase");
    primaryStage.setScene(new Scene(pane));
    primaryStage.show();
    }

private class UpperCase extends GridPane {
    Label lblString = new Label("Enter String:");
    Label lblAmount = new Label("Number of capital Letters:");

    TextField tfString = new TextField();
    TextField tfAmount = new TextField();

    private UpperCase() {
        setHgap(10);
        setVgap(10);
        setPadding(new Insets(10,10,10,10));

        add(lblString, 0, 0);
        add(tfString, 1, 0);
        add(lblAmount, 0, 1);
        add(tfAmount, 1, 1);

        tfString.setText("0");
        tfString.setAlignment(Pos.CENTER_RIGHT);
        tfAmount.setText("0");
        tfAmount.setAlignment(Pos.CENTER_RIGHT);

        Button submit = new Button("Submit");
        HBox submitButtons = new HBox(submit);

        tfString.setOnAction(e -> {
            int count = count(tfString.getText().toCharArray());
            tfAmount.setText(String.valueOf(count));
        });


    }
}


public static int count(char[] chars) {

    return count(chars, chars.length - 1);
}

public static int count(char[] chars, int high) {

    int count = Character.isUpperCase(chars[high]) ? 1 : 0;
    if (high == 0)
        return count;
    else
        return count + count(chars, high - 1);

}

0 个答案:

没有答案