2017-04-11 17 views
0

私は現在Farkleというゲームをプログラミングしています。私は、ダイスを表す四角形ごとにOnMouseClickイベントを追加したいと思います。javafx矩形onmouseclickイベント(fxml)

私の問題は、長方形がMouseEventを認識しないことです。 Scene BuilderでGUIを構築しました。これは私のFXMLコードの抜粋です:

<Rectangle fx:id="recDice1" arcHeight="5.0" arcWidth="5.0" fill="WHITE" 
height="40.0" onMouseClicked="#recDice1_OnMouseClicked" stroke="BLACK" 
strokeType="INSIDE" width="40.0" GridPane.halignment="CENTER" 
GridPane.valignment="CENTER" /> 

RectangleがGridPaneにあり、GridPaneがBorderPaneの左側に命じています。

そして、これは私のJavaコードの抜粋です:

@FXML private Rectangle recDice1; 
@FXML public void recDice1_OnMouseClicked(MouseEvent event){ 
    System.out.println("Funktioniert!"); 
} 

@Override 
public void start(Stage primaryStage) { 
    this.primaryStage = primaryStage; 
    this.primaryStage.setTitle("Farkle"); 

    initLayout(); 

} 

    public void initLayout(){ 
    try { 
     //Erstelle FXMLLoader 
     FXMLLoader loader = new FXMLLoader(); 
     loader.setLocation(getClass().getResource("../gui/FarkleGUI.fxml")); 
     loader.setController(new FarkleControl()); 
     rootLayout = (BorderPane) loader.load(); 



     //Lade Szene 
     Scene scene = new Scene(rootLayout); 
     primaryStage.setScene(scene); 
     primaryStage.setResizable(false); 

     //Setze Grösse der Stage (Somit entfallen Margins) 
     primaryStage.sizeToScene(); 

     //Zeige Szene 
     primaryStage.show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 


public static void main(String[] args) { 
    launch(args); 
} 

輸入:

package java2.farkle.mlz.control; 

import java.io.IOException; 

import javafx.application.Application; 

import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 

import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

私は長方形がMouseEventのを認識しません冒頭で言ったように。私はボタンで同じことを試して、それは正常に働いた。

+0

Scenebuilderを使用している場合は、MVCのアイデアを使用しているとみなすことができます。しかし、あなたのコードではそうではありません。 Scenebuilderを使用して変更したFXMLファイルには、作業する必要があるコントローラファイルがあります。 MVCではほとんどの場合、メインファイルに何もしないでください。あなたの@FXMLコードは、FXMLファイルに関連付けられているコントローラ内になければなりません。 – Sedrick

答えて

0

ここでは分析できるものがあります。

メイン:

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class Farkle extends Application 
{ 

    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

} 

FXML:

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.shape.Rectangle?> 

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="farkle.FXMLDocumentController"> 
    <children> 
     <Button fx:id="button" layoutX="128.0" layoutY="155.0" onAction="#handleOnButtonAction" text="Click Me!" /> 
     <GridPane layoutX="6.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <columnConstraints> 
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
     </columnConstraints> 
     <rowConstraints> 
      <RowConstraints minHeight="100.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
     </rowConstraints> 
     <children> 
      <Rectangle fx:id="recDice1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="100.0" onMouseClicked="#handleOnMouseClicked" stroke="BLACK" strokeType="INSIDE" width="160.0" /> 
      <Rectangle fx:id="recDice2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="100.0" onMouseClicked="#handleOnMouseClicked" stroke="BLACK" strokeType="INSIDE" width="160.0" GridPane.columnIndex="1" /> 
     </children> 
     </GridPane> 
    </children> 
</AnchorPane> 

コントローラー:

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.shape.Rectangle; 

/** 
* 
* @author blj0011 
*/ 
public class FXMLDocumentController implements Initializable 
{  
    @FXML 
    private void handleOnButtonAction(ActionEvent event) 
    { 
     System.out.println("You clicked button: " + ((Button)event.getSource()).getId()); 
    } 

    @FXML private void handleOnMouseClicked(MouseEvent event) 
    { 
     System.out.println("You clicked rectangle: " + ((Rectangle)event.getSource()).getId()); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 
     // TODO 
    }   
} 
+0

あなたの答えをありがとう。私はあなたのコードを試みたが、それは私のために働かなかった。私はFXMLを使用しないで、GUIをコード化する方が簡単です。 – Luca

+0

答えが問題の解決策であった場合は、正しい答えとして選択する必要があります。 – Sedrick

関連する問題