JavaFX Scenebuilder 按钮未正确调用

问题描述 投票:0回答:1

JavaFX 按钮没有从我的 Scenebuilder 程序传递,这导致我的代码认为它们为空。更准确地说:“无法调用“javafx.scene.control.Button.setOnAction(javafx.event.EventHandler)”,因为“this.calculate”在 main/lab.Controller.initialize(Controller.java:38) 处为空。 ..还有21个” 我不想初始化新按钮,因为那样它们就不是我需要的按钮,并且无法工作。我真的不知道该怎么做,我已经检查了我的 fx:id 以确保它是正确的(确实如此),所以我不知道这里出了什么问题。

我尝试更正 fx:id,我尝试初始化新按钮,但这不起作用。老实说,我真的不知道这是怎么错误的,尽管这可能是我不知道的初始化()方法中的一个问题。无论如何,这是我的控制器类,其中包含我正在讨论的代码:


public class Controller {
    @FXML
    private Button calculate;

    @FXML
    private Button clear;

    @FXML
    private Button exit;
    
    @FXML
    private TextField centimeters;

    @FXML
    private TextField inches;

    @FXML
    private TextField meters;

    @FXML
    private TextField yards;
    
    @FXML
    public  void initialize() {

        calculate.setOnAction(event-> {
            convert();
        });
        clear.setOnAction(e-> {
            centimeters.setText("");
            meters.setText("");
            yards.setText("");
            inches.setText("");
        });
        exit.setOnAction(e->{
            System.exit(0);
        });
    }
  
    public void alignmentFixer() {
        exit.setAlignment(Pos.CENTER_RIGHT);
        clear.setAlignment(Pos.CENTER);
        calculate.setAlignment(Pos.CENTER_LEFT);
    }
    @FXML
    public void convert() {
        if(!centimeters.getText().isEmpty() && meters.getText().isEmpty() &&
                yards.getText().isEmpty() && inches.getText().isEmpty()) {
            cmConvert(centimeters, inches, meters, yards);
        }
        else if(centimeters.getText().isEmpty() && meters.getText().isEmpty() &&
                yards.getText().isEmpty() && !inches.getText().isEmpty()) {
            inConvert(inches, centimeters,  meters, yards);
        }
        else if(centimeters.getText().isEmpty() && !meters.getText().isEmpty() &&
                yards.getText().isEmpty() && inches.getText().isEmpty()) {
            mConvert(meters, centimeters, inches, yards);
            }
            else if(centimeters.getText().isEmpty() && meters.getText().isEmpty() &&
                    !yards.getText().isEmpty() && inches.getText().isEmpty()) {
                yConvert(yards, centimeters, meters, inches);
                }
    }

    
    public static void cmConvert(TextField centimeters, TextField inches, TextField meters, TextField yards) {
        String centimetersText = centimeters.getText();
        double cm = Double.parseDouble(centimetersText);
        
        // Call the conversion methods and update the GUI with the converted values
        inches.setText(String.valueOf(cmConvertIn(cm)));
        meters.setText(String.valueOf(cmToMeters(cm)));
        yards.setText(String.valueOf(cmToYards(cm)));
    }

    public static void inConvert(TextField inches, TextField centimeters, TextField meters, TextField yards) {
        String inchesText = inches.getText();
        double in = Double.parseDouble(inchesText);
        
        // Call the conversion methods and update the GUI with the converted values
        centimeters.setText(String.valueOf(incToCm(in)));
        meters.setText(String.valueOf(incToMeters(in)));
        yards.setText(String.valueOf(incToYards(in)));
    }
    
    public static void mConvert(TextField meters, TextField centimeters, TextField inches, TextField yards) {
        String metersText = meters.getText();
        double meter = Double.parseDouble(metersText);
        
        // Call the conversion methods and update the GUI with the converted values
        centimeters.setText(String.valueOf(mToCm(meter)));
        inches.setText(String.valueOf(mToInc(meter)));
        yards.setText(String.valueOf(mToYards(meter)));
    }
    
    public static void yConvert(TextField yards, TextField centimeters, TextField inches, TextField meters) {
        String yardsText = yards.getText();
        double yard = Double.parseDouble(yardsText);
        
        // Call the conversion methods and update the GUI with the converted values
        centimeters.setText(String.valueOf(yardsToCm(yard)));
        inches.setText(String.valueOf(yardsToInc(yard)));
        meters.setText(String.valueOf(yardsToMeters(yard)));
    }
    
    public static double cmConvertIn(double cm) {
        return cm / 2.54;
    }

    public static double cmToMeters(double cm) {
        return cm / 100.0;
    }

    public static double cmToYards(double cm) {
        return cm / 91.44;
    }
    public static double incToCm(double in) {
        return in * 2.54;
    }

    public static double incToMeters(double in) {
        return in * 0.0254;
    }

    public static double incToYards(double in) {
        return in / 36.0;
    }
    
    public static double mToCm(double meter) {
        return meter * 100.0;
    }

    public static double mToInc(double meter) {
        return meter * 39.3701;
    }

    public static double mToYards(double meter) {
        return meter * 1.09361;
    }

    public static double yardsToCm(double yard) {
        return yard * 91.44;
    }

    public static double yardsToInc(double yard) {
        return yard * 36.0;
    }

    public static double yardsToMeters(double yard) {
        return yard * 0.9144;
    }
}
java button javafx methods nullpointerexception
1个回答
0
投票

啊,我相信你的错误代码在这里:

@FXML
public void convert() {

如果您在场景构建器上设置了按钮操作,则不需要在初始化时设置按钮操作,在这种情况下您无需设置。或者只设置idbutton,不在scenebuilder中设置action,并且像方法一样设置action而不需要“@FXML”和“public”。我相信如果你这样做,那就成功了。

检查来自 event 的库是否不是来自 awt.event 的。检查你的静态方法,真的有必要这么多吗? 我建议使用您的配置按钮创建一个方法并调用初始化;

© www.soinside.com 2019 - 2024. All rights reserved.