解决onAction时出错

时间:2016-08-15 20:27:57

标签: java javafx upgrade fxml fxmlloader

我知道之前已经多次询问过,但没有一个答案对我来说是一个解决方案。我刚刚将JDK更新为java-8-openjdk-amd64(来自jdk7),这些错误刚刚出现:

javafx.fxml.LoadException: Error resolving onAction='#changeStyle', either the event handler is not in the Namespace or there is an error in the script./myapp.fxml:1778

第1778行:

<Button layoutX="250.0" layoutY="170.0" mnemonicParsing="false" onAction="#changeStyle" text="Reset Style" />

Controller .java:

public void changeStyle(){
    String url_orig = CUSTOM_HTMLEditor.class.getClassLoader().getResource(pathToOrigCss).toExternalForm();
    String url_1 = CUSTOM_HTMLEditor.class.getClassLoader().getResource(pathToStyle_1Css).toExternalForm();
    String url_tango = CUSTOM_HTMLEditor.class.getClassLoader().getResource(pathToStyle_tangoCss).toExternalForm();
    String url_tango_invers = CUSTOM_HTMLEditor.class.getClassLoader().getResource(pathToStyle_tangoInversCss).toExternalForm();

    root_AnchorPane.getStylesheets().clear();   

    final Toggle selectedToggle = styleToggle.getSelectedToggle();
    int selectedToggleIndex = styleToggle.getToggles().indexOf(selectedToggle);

    switch(selectedToggleIndex)
    {
    case 0:
        root_AnchorPane.getStylesheets().add(url_orig);
        System.out.println("Style changed to original.");
        currentPathToCSS = url_orig;
        break;
    case 1:
        root_AnchorPane.getStylesheets().add(url_1);
        System.out.println("Style changed to style_1.");
        currentPathToCSS = url_1;
        break;
    case 2:
        root_AnchorPane.getStylesheets().add(url_tango);
        System.out.println("Style changed to tango.");
        currentPathToCSS = url_tango;
        break;
    case 3:
        root_AnchorPane.getStylesheets().add(url_tango_invers);
        System.out.println("Style changed to tango_invers.");
        currentPathToCSS = url_tango_invers;
        break;
    default:
        root_AnchorPane.getStylesheets().add(url_orig);
        System.out.println("Style changed to original.");
        currentPathToCSS = url_orig;

    }
}

控制器的设置:

<AnchorPane id="root" fx:id="root_AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="500.0" minWidth="700.0" prefHeight="680.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.semproc.hergarten.gui.MemotrainerController">

编辑: 谢谢您的帮助。这是方法中的静态。

1 个答案:

答案 0 :(得分:1)

请改为尝试:

@FXML添加到方法中。例如:

@FXML public void changeStyle(){
....
....
....
}