2016-08-15 81 views
0

我知道這個問題之前已經被問過好幾次了,但沒有一個答案對我來說是一個解決方案。我剛剛更新的JDK爲java-8的OpenJDK-AMD64(從JDK7)和這些錯誤只是出現了:1778解決onAction時出現錯誤

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 

線:

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

控制器的.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"> 

編輯: 感謝您的幫助。這是該方法中的靜態。

+0

您可以編輯問題以顯示您設置控制器的部分嗎? –

+1

我不認爲你可以指定一個'static'方法作爲處理程序。 (無論如何,它是沒有意義的;處理程序是UI組件的屬性,它必然是控制器*實例*的一個屬性。) –

回答

1

試試這個:

添加@FXML的方法。例如:

@FXML public void changeStyle(){ 
.... 
.... 
.... 
} 
相關問題