1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package application;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static SimpleDoubleProperty MoouseLocationX = new SimpleDoubleProperty();
public static SimpleDoubleProperty MoouseLocationY = new SimpleDoubleProperty();
public static Rectangle DiscutionBackground;
public static AnchorPane root;
public void start(Stage primaryStage) {
try {
root = new AnchorPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
DiscutionBackground = new Rectangle();
DiscutionBackground.setWidth(40);
DiscutionBackground.setHeight(40);
DiscutionBackground.setLayoutX(0);
DiscutionBackground.setLayoutY(0);
AnchorPane.setTopAnchor(DiscutionBackground,scene.heightProperty().get() /3);
AnchorPane.setRightAnchor(DiscutionBackground,scene.widthProperty().get() / 3);
root.getChildren().addAll(DiscutionBackground);
scene.setOnMouseMoved(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
MoouseLocationX.set(event.getSceneX());
MoouseLocationY.set(event.getSceneY());
}
});
Button a = new Button("click");
a.setOnAction(ActionEvent ->{
DiscutionBackground.layoutYProperty().bind(MoouseLocationY);
// DiscutionBackground.layoutXProperty().bind(MoouseLocationX);
});
root.getChildren().add(a);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
System.out.print("error code 1");
}
}
public static void main(String[] args) {
launch(args);
}
} |
Partager