将发送发送到TextArea

时间:2018-02-26 03:26:03

标签: java javafx jtextarea

我正在处理作业,但我似乎无法在TextArea中打印出我的文字。我正在使用递归方法,我需要将递归方法的结果发送到TextArea。我已经尝试了多种方法但它没有用,并且在网上搜索无济于事。请帮忙!

public class Sheppard_Week5 extends Application {
Button convert = new Button("Convert");
Button clear = new Button("Clear");
Button exitBtn =  new Button("Exit");
Label enterInt = new Label("Enter Number:");
TextField enterText = new TextField();
TextArea binaryText = new TextArea();
ImageView mCEscher = new ImageView("sheppard_week5/M.C.Escher.jpg");
ArrayList<String> remainderList = new ArrayList();
ScrollPane scrollPane = new ScrollPane(binaryText)

@Override
public void start(Stage primaryStage) throws Exception {
    BorderPane bPane = new BorderPane();
    GridPane gPane = new GridPane();
    gPane.setStyle("-fx-background-color: #fffaf0;");
    bPane.setStyle("-fx-background-color: #fffaf0;");
    bPane.setCenter(gPane);
    Scene scene = new Scene(bPane);
    bPane.setBottom(buttonBox());
    bPane.setTop(titleBox());

    mCEscher.setFitHeight(300);
    mCEscher.setFitWidth(300);

    for (int i = 0; i <= 3; i++) {
        ColumnConstraints column = new ColumnConstraints();
        column.setPercentWidth(25);
        gPane.getColumnConstraints().add(column);
    }

    /*Gridpane
    *GridPane.setConstraints(node, column Index, row Index, column span, row span);
    */
    gPane.add(enterInt, 0, 0);
    gPane.add(enterText, 1, 0);
    gPane.add(binaryText, 2, 0);
    GridPane.setConstraints(binaryText, 2, 0, 2, 4);
    gPane.add(mCEscher, 0, 1);
    GridPane.setColumnSpan(mCEscher, 2);
    binaryText.setEditable(false);
    scrollPane.setContent(binaryText);

    primaryStage.setTitle("Binary Converter");
    primaryStage.setScene(scene);
    primaryStage.show();
}
private VBox titleBox() throws Exception{
    VBox titlesBox = new VBox();
    Text title = new Text("Welcome to GUI #5");
    title.setStyle("-fx-text-fill: #000000");
    title.setFont(Font.font("Monospace",FontWeight.NORMAL, FontPosture.REGULAR, 30));
    Text subtitle = new Text("I think this class will be the death of me, "
            + "\nI really wish I could get one thing right...");
    subtitle.setFont(Font.font("Monospace", FontWeight.NORMAL, FontPosture.ITALIC, 10));
    subtitle.setStyle("-fx-text-fill: #000000");
    Text subsubtitle = new Text("Please enter an integer.");
    subsubtitle.setFont(Font.font("Monospace", FontWeight.NORMAL, FontPosture.ITALIC, 7));
    subsubtitle.setStyle("-fx-text-fill: #000000");
    Text subsubsubtitle = new Text("Please dear God work");
    subsubsubtitle.setFont(Font.font("Monospace", FontWeight.NORMAL, FontPosture.ITALIC, 3));
    subsubsubtitle.setStyle("-fx-text-fill: #000000");
    titlesBox.setAlignment(Pos.TOP_CENTER);
    titlesBox.getChildren().addAll(title, subtitle, subsubtitle, subsubsubtitle);
    return titlesBox;
}

private HBox buttonBox() throws Exception{
    HBox buttbox = new HBox();
    buttbox.getChildren().addAll(convert, clear, exitBtn);
    buttbox.setAlignment(Pos.CENTER);
    buttbox.setMargin(clear,new Insets(10,10,10,10));

    convert.setOnAction(new convertHandler());
    clear.setOnAction(new resetHandler());
    exitBtn.setOnAction(e ->{
        Platform.exit();
    });
    return buttbox;
}

class convertHandler implements EventHandler<ActionEvent>{
    @Override
    public void handle(ActionEvent event){
        if (enterText.getText().trim().isEmpty()){
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("Warning");
            alert.setHeaderText("Required Fields Empty");
            alert.setContentText("You must enter an integer.\n"
                    + "Please try again.");
            alert.showAndWait();

        } 
        else if (!enterText.getText().trim().isEmpty()){
            try {
                Integer.parseInt(enterText.getText());
            } catch (NumberFormatException nfe){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("Warning");
                alert.setHeaderText("Field is not an integer");
                alert.setContentText("The entered text is not an integer.\n"
                        + "Please try again.");
                alert.showAndWait();
            }
        } else {
            int enteredNumb = Integer.parseInt(enterText.getText());
            ToBinary(enteredNumb);
            Collections.reverse(remainderList);
            binaryText.appendText("-------------------------------\n");
            for (int i = 0; i < remainderList.size(); i++){
                binaryText.setText(remainderList.get(i));
            }
        }            
    }
}

class resetHandler implements EventHandler<ActionEvent>{
    @Override
    public void handle(ActionEvent event){
        remainderList.clear();
        binaryText.clear();
        enterText.clear();            
    }
}
public int ToBinary(int number) {
    /* Define variable required in method */
    if (number == 0) {
        return 0;
    } else {
        int remainder = number - 2 * (number / 2);
        String remainderString = Integer.toString(remainder);
        String numberString = Integer.toString(number);
        remainderList.add(remainderString);
        String number2String = Integer.toString(number);
        binaryText.appendText(numberString + "/2= " + number2String + " Remainder "
        + remainderString + "\n");
        ToBinary(number / 2);      //Recurrsive call to method
    }
    return number;
}
} //End Class Sheppard_Week5

1 个答案:

答案 0 :(得分:0)

您根本无法接听ToBinaryconvertHandler.handle方法的调用:

if (enterText.getText().trim().isEmpty()){
    doA();
} 
else if (!enterText.getText().trim().isEmpty()){
    doB();
} else {
    doC();
}

可以有3个结果:

  1. enterText.getText().trim().isEmpty()引发异常,例如NullPointerException,代码继续使用适当的catch子句在下一个catch块中继续。
  2. enterText.getText().trim().isEmpty()产生true并执行doA()
  3. enterText.getText().trim().isEmpty()产生false并执行doB()
  4. 由于您对toBinary的调用位于doC()部分,因此从未执行过。如果在解析成功时需要继续,则需要在try块中执行此操作或退出catch块中的方法,并将最后else的逻辑放在最后catch后面{1}}:

    try {
        int enteredNumb = Integer.parseInt(enterText.getText());
        ToBinary(enteredNumb);
        Collections.reverse(remainderList);
        binaryText.appendText("-------------------------------\n");
        for (int i = 0; i < remainderList.size(); i++){
            binaryText.setText(remainderList.get(i));
        }
    } catch (NumberFormatException nfe){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Warning");
        alert.setHeaderText("Field is not an integer");
        alert.setContentText("The entered text is not an integer.\n"
                + "Please try again.");
        alert.showAndWait();
    }
    

    int enteredNumb;
    try {
        enteredNumb = Integer.parseInt(enterText.getText());
    } catch (NumberFormatException nfe){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Warning");
        alert.setHeaderText("Field is not an integer");
        alert.setContentText("The entered text is not an integer.\n"
                + "Please try again.");
        alert.showAndWait();
        return;
    }
    
    ToBinary(enteredNumb);
    Collections.reverse(remainderList);
    binaryText.appendText("-------------------------------\n");
    for (int i = 0; i < remainderList.size(); i++){
        binaryText.setText(remainderList.get(i));
    }
    

    附加说明:

    • int remainder = number - 2 * (number / 2);
      

      已经有办法在一次操作中执行此操作:

      int remainder = number % 2;
      

      在这种情况下,假设非负number,它也可以使用二进制运算符进行优化,该运算符应该比余数运算符快一点:

      int remainder = number & 1; // extract least significant bit
      
    • 我猜你应该返回与ToBinary方法不同的东西。否则,您可以轻松地使用void作为返回类型。
    • 如果希望从最高有效位到最低有效位对剩余部分进行排序,则需要在添加到列表之前进行递归调用。这样,在初始调用toBinary后,您无需撤消列表。
    • 除非您要求用户在点击remainderList.clear()按钮之间点击clear按钮,否则您需要致电convert。否则列表仍将包含最后的结果。