javafx鼠标单击事件(fxml)
我目前正在编写一个名为Farkle的游戏。我想为每个代表骰子的矩形添加一个OnMouseClick事件 我的问题是矩形无法识别MouseEvent。我用场景生成器构建了GUI。这是我的FXML代码的摘录:javafx鼠标单击事件(fxml),javafx,Javafx,我目前正在编写一个名为Farkle的游戏。我想为每个代表骰子的矩形添加一个OnMouseClick事件 我的问题是矩形无法识别MouseEvent。我用场景生成器构建了GUI。这是我的FXML代码的摘录: <Rectangle fx:id="recDice1" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="40.0" onMouseClicked="#recDice1_OnMouseClicked" stroke="BLACK"
<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" />
进口:
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。我用一个按钮尝试了同样的方法,效果很好。下面是一些你可以分析的东西 主要内容: FXML:
如果您使用的是Scenebuilder,可以假定您使用的是MVC思想。然而,在你的代码中你不是。您使用Scenebuilder更改的FXML文件有一个您应该使用的控制器文件。在MVC中,几乎在任何情况下都不应对主文件执行任何操作。您的@FXML代码应该位于与FXML文件关联的控制器中。谢谢您的回答。我试过你的代码,但对我无效。我不会使用FXML,对我来说编写GUI更容易。如果答案是您问题的解决方案,您应该选择它作为正确答案。
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;
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);
}
}
<?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
}
}