在将十进制转换为各种数据类型时在文本字段中获取错误

时间:2015-10-23 04:53:38

标签: java eclipse javafx

这是我在StackOverflow上的第一篇文章。我在我的程序中遇到了设置十进制十六进制二进制转换器的问题。这个想法是有三个文本字段:十进制,十六进制,二进制(在一个VBox中)和标签:十进制,十六进制,二进制,匹配那些文本字段位置。我用这段代码获得了这个:

GridPane paneForVBoxes = new GridPane();
        paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
        paneForVBoxes.setVgap(10);
        paneForVBoxes.setHgap(10);

        VBox units = new VBox(12);
        units.setAlignment(Pos.CENTER_LEFT);
        Label lbDec = new Label("Decimal");
        Label lbHex = new Label("Hex");
        Label lbBin = new Label("Binary");
        units.getChildren().addAll(lbDec, lbHex, lbBin);


        VBox textFields = new VBox(5);
        TextField tfDec = new TextField();
            tfDec.setPrefColumnCount(19);
            tfDec.setAlignment(Pos.CENTER_RIGHT);
        TextField tfHex = new TextField();
            tfHex.setPrefColumnCount(19);
            tfHex.setAlignment(Pos.CENTER_RIGHT);
        TextField tfBin = new TextField();
            tfBin.setPrefColumnCount(19);
            tfBin.setAlignment(Pos.CENTER_RIGHT);

        textFields.getChildren().addAll(tfDec, tfHex, tfBin);

        paneForVBoxes.add(units, 0, 0);
        paneForVBoxes.add(textFields, 1, 0);

因此,我们的想法是在键入值时为每个文本字段提供实时更新,例如:在dec文本字段中键入11,分别在十六进制和二进制字段1A和1011中自动显示。

我达到了这一点,但是当我删除文本字段中的所有字符时,我收到了NumberFormatExceptions,我不知道如何解决这个问题。这就是我尝试解决问题的方法: 请注意这只是十进制到二进制字段的编码,通过对此的简单修复,我可以将其应用于尚未开发的其余代码。 注意我在Integer.parseInt(s)内和周围尝试了tfDec.setText("0");但是eclipse确实不喜欢

 tfDec.setOnKeyReleased(e -> {
                if (tfDec.equals("")) {
                    tfDec.setText("0");
                }
                else {
                    String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
                    tfBin.setText(bin);
                }
            });

/ 整个代码 /

    package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Elias16_5 extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane paneForVBoxes = new GridPane();
            paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
            paneForVBoxes.setVgap(10);
            paneForVBoxes.setHgap(10);

            VBox units = new VBox(12);
            units.setAlignment(Pos.CENTER_LEFT);
            Label lbDec = new Label("Decimal");
            Label lbHex = new Label("Hex");
            Label lbBin = new Label("Binary");
            units.getChildren().addAll(lbDec, lbHex, lbBin);


            VBox textFields = new VBox(5);
            TextField tfDec = new TextField();
                tfDec.setPrefColumnCount(19);
                tfDec.setAlignment(Pos.CENTER_RIGHT);
            TextField tfHex = new TextField();
                tfHex.setPrefColumnCount(19);
                tfHex.setAlignment(Pos.CENTER_RIGHT);
            TextField tfBin = new TextField();
                tfBin.setPrefColumnCount(19);
                tfBin.setAlignment(Pos.CENTER_RIGHT);

            textFields.getChildren().addAll(tfDec, tfHex, tfBin);

            paneForVBoxes.add(units, 0, 0);
            paneForVBoxes.add(textFields, 1, 0);

            tfDec.setOnKeyReleased(e -> {
                if (tfDec.equals("")) {
                    tfDec.setText("0");
                }
                else {
                    String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
                    tfBin.setText(bin);
                }
            });







            Scene scene = new Scene(paneForVBoxes, 300, 100);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setTitle("Data Conversion");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

3 个答案:

答案 0 :(得分:0)

你应该尝试StringUtils。 所以这段代码:

if (tfDec.equals("")) {

将是

if (StringUtils.isBlank(tfDec.getText())) {

在这里阅读更多内容: StringUtils.isBlank() vs String.isEmpty()

另外,在第一行中,它应该是

tfDec.getText().equals("")

tfDec.equals("")

答案 1 :(得分:0)

我不确定你是否已经学过"尝试/捕捉"块。如果是这样,那么为什么不捕获异常并处理它,而不是试图阻止异常。例如,如果有人输入" G"并且发生异常,那么你可以打印"无效"在bin字段中和/或弹出一个对话框,警告用户十进制文本字段中只允许0-9。

您的声明为git log B..A 。这有没有回归真实?我很惊讶TextField首先可以与字符串进行比较。我希望更像tfDec.equals("")

答案 2 :(得分:0)

虽然setOnKeyReleased()适用于此方案,但这不是最佳解决方案。无论何时释放任何键,都将触发此方法,无论它是否对文本字段中的文本产生任何影响。例如,当textfield为空并且用户按BackSpace时。

更好的方法是在TextField的textProperty()上添加更改侦听器。只有在文本字段的文本发生更改时才会触发此操作:

tfDec.textProperty().addListener((observable, oldValue, newValue) -> {
     if (!newValue.isEmpty()) {
         tfBin.setText(Integer.toBinaryString(Integer.parseInt(newValue)));
     } else {
         tfBin.setText("0");
     }
});