2016-03-24 5 views
0

私は単純なボタンを作成しようとしていますが、この愚かなエラーが発生していて意味がありません。ここでコントローラ 'AdminSceneController'にイベントスロット 'logout'がありません - JavaFXエラー

私の管理シーンFXMLです:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.ListView?> 
<?import javafx.scene.control.Separator?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.text.Font?> 

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="437.0" prefWidth="582.0" stylesheets="@application.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="appController.AdminSceneController"> 
    <children> 
     <Separator layoutX="-14.0" layoutY="101.0" prefHeight="10.0" prefWidth="601.0" /> 
     <Label layoutX="14.0" layoutY="29.0" text="admin panel"> 
     <font> 
      <Font name="Book Antiqua" size="28.0" /> 
     </font> 
     </Label> 
     <Button layoutX="174.0" layoutY="32.0" mnemonicParsing="false" style="-fx-background-radius: 100px;" text="+" textFill="#369033" /> 
     <Button fx:id="logoutButton" layoutX="14.0" layoutY="65.0" mnemonicParsing="false" onAction="#logout" prefHeight="3.0" prefWidth="81.0" styleClass="logout" stylesheets="@application.css" text="(logout)" textFill="#070707" /> 
     <Button layoutX="387.0" layoutY="392.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="158.0" text="Delete" /> 
     <ListView layoutX="223.0" layoutY="106.0" prefHeight="327.0" prefWidth="128.0" /> 
    </children> 
</Pane> 

そして、これは私が言ってEclipseのから警告を受ける

package appController; 

import appDesign.PhotoAlbum; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.Node; 
import javafx.scene.control.Button; 

public class AdminSceneController { 

    public class MainSceneController { 

     @FXML 
     Button logoutButton; 

     @FXML 
     public void logout(ActionEvent event) throws Exception { 
      PhotoAlbum.primaryStage.show(); 
      ((Node)(event.getSource())).getScene().getWindow().hide(); 
     } 

    } 

} 

私AdminSceneController.javaです:

The controller 'AdminSceneController' has no event slot 'logout' 

そして私をプログラムを実行すると、エラーが表示されます。

javafx.fxml.LoadException: Error resolving onAction='#logout', either the event handler is not in the Namespace or there is an error in the script. 
/C:/Users/Peter/Documents/GitHub/PhotoAlbum40/bin/appDesign/AdminPanelScene.fxml:19 

誰でも助けてもらえますか?

答えて

1

AdminSceneControllerにはログアウトメソッドがありません。あなたのクラスMainSceneControllerは動作します。

public class MainSceneController { 

と終了}行を削除し、それが動作するはずです。

+0

...トップレベルのクラスにフィールド/メソッドを移動することができます。私はなぜAdminSceneがその中に入れ子になっていたのか分かりません。私にそれを見つけてくれてありがとう! – Laugh7

0

内部クラス(MainSceneController)は、囲むクラス(AdminSceneController)と同じクラスではないことを理解しておく必要があります。 fxmlにfx:controller="appController.AdminSceneController"を使用すると、AdminSceneControllerのインスタンスが作成されます。ただし、このクラスには単一のメソッドまたはフィールドが含まれていません。これにより、エラーが発生します。

さらに、FXMLLoaderは、staticの内部クラスを作成できないことに注意してください。 FXMLLoaderにコントローラインスタンスを作成する場合は、MainSceneControllerを静的にしてfx:controller="appController.AdminSceneController$MainSceneController"を使用する必要があります。この周り

つの方法controllerFactoryを指定するか、コントローラのインスタンスを自分で作成することになります。FXMLからfx:controller属性を削除する必要があります

FXMLLoader loader = new FXMLLoader(getClass().getResource(...)); 
AdminSceneController enclosingInstance = new AdminSceneController(); // or any other way to get your hands on a instance of the enclosing class 

// specify controller instance used yourself 
loader.setController(enclosingInstance.new MainSceneController()); 

... 

loader.load() 

を。もちろん

あなたは、単に私は、このためのいくつかの愚かな理由があることを知っていた

+0

AdminSceneはそこにはないはずです。私はそれがどうなったのか分からない。 – Laugh7

関連する問題