Mat*_*rty 22
这是一种方式:
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.input.*;
var r = Rectangle {
x: 50, y: 50
width: 120, height: 120
fill: Color.RED
onMouseClicked: function(e:MouseEvent):Void {
if (e.button == MouseButton.SECONDARY) {
println("Right button clicked");
}
}
}
Stage {
title : "ClickTest"
scene: Scene {
width: 200
height: 200
content: [ r ]
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想知道如何在 JavaFX 中处理右键单击事件,并且发现 2009 的答案现在已经有些过时了……这是 java 11 (openjfx) 中的一个工作示例:
public class RightClickApplication extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("Example");
Rectangle rectangle = new Rectangle(100, 100);
BorderPane pane = new BorderPane();
pane.getChildren().add(rectangle);
rectangle.setOnMouseClicked(event ->
{
if (event.getButton() == MouseButton.PRIMARY)
{
rectangle.setFill(Color.GREEN);
} else if (event.getButton() == MouseButton.SECONDARY)
{
rectangle.setFill(Color.RED);
}
});
primaryStage.setScene(new Scene(pane, 200, 200));
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19977 次 |
| 最近记录: |