JavaFXでSceneBuilderを使用して基本的なゲームランチャーを作成しています。 SceneBuilderはFXMLで動作するので、私のランチャーレイアウトはFXMLです。メインクラスには、ボタンをクリックして呼び出すメソッドがあります。私はあなたがボタンのボタンクリック時のJava FXMLメソッドの呼び出し
onAction
プロパティで
#methodName
を使用することができることを読んで、これは動作しません。
私の主なJavaクラス:
@FXML
private void launchGame(ActionEvent e) {
System.out.println("Launching...");
}
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(Main.class.getResource("launcher.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("First Week Login");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
}
マイFXMLファイル:あなたは別のコントローラクラスを作成し、このようfx:controller="packageName.Classname"
でトップAnchorPane
タグで指定する必要があります
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.web.WebView?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.102">
<children>
<BorderPane prefHeight="493.0" prefWidth="664.0" styleClass="background"
stylesheets="@launcher.css">
<bottom>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<children>
<Button alignment="CENTER" mnemonicParsing="false"
text="Launch Game" onAction="#launchGame" />
</children>
</HBox>
</bottom>
<top>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0"
styleClass="title" text="First Week" />
</children>
</HBox>
</top>
<center>
<WebView prefHeight="200.0" prefWidth="200.0"
BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</AnchorPane>
FXMLファイルを直接編集する必要はありません。 SceneBuilderでは、コントローラ、コンポーネントID、およびアクションを指定できます。 SceneBuilderでアクションを指定しましたか? –
いいえ、私はEclipseに書きました – JPadley
申し訳ありませんが、私はあなたのコメントを誤解しました。 SceneBuilderを使って間違いなく使用する必要があります。 SceneBuilderを使用しているときは、FXMLファイルを直接編集する必要はほとんどありません。 –