gpt4 book ai didi

Javafx KeyEvent 和 MouseEvent

转载 作者:行者123 更新时间:2023-11-29 03:10:19 25 4
gpt4 key购买 nike

我正在尝试学习 javafx。我完成了大部分代码,但我在使用 start 方法时遇到了问题。

我想做的是通过点击在屏幕上添加点。如果我按 1 或 0, future 将添加的点将更改为一些不同的颜色。因此,我知道我必须使用 setOnMouseClickedsetOnKeyPressed 方法,但互联网上没有太多关于它的内容。

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;


public class Spots extends Application {

public static final int SIZE = 500;
public static final int SPOT_RADIUS = 20;
private LinkedList<Spot> spotList;
private Color color;

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

public void start(Stage stage) {

stage.setTitle("Spots");
dotList = new SinglyLinkedList<>();
Group root = new Group();
Scene scene = new Scene(root, 500, 500, Color.BLACK);
Spot r;

// ...

stage.show();
}

private class Spot extends Circle {

public Spot(double xPos, double yPos) {
super(xPos, yPos, SPOT_RADIUS);
setFill(color);
}

public boolean contains(double xPos, double yPos) {
double dx = xPos - getCenterX();
double dy = yPos - getCenterY();
double distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
return distance <= SPOT_RADIUS;
}
}
}

最佳答案

圈子不接受的原因是没有聚焦。对于响应关键事件的节点,它们应该是 focusTraversable .你可以这样做在节点上调用 setFocusTraversable(true)。我编辑了您的 start() 方法,这是我最终得到的代码。

public void start(Stage primaryStage) throws Exception {

Pane pane = new Pane();
final Scene scene = new Scene(pane, 500, 500);
final Circle circle = new Circle(250, 250, 20);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
pane.getChildren().add(circle);
circle.setFocusTraversable(true);
circle.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent e) {
if ((e.getCode() == KeyCode.UP) && (circle.getCenterY() >= 5)) {
circle.setCenterY(circle.getCenterY() - 5);
}

else if ((e.getCode() == KeyCode.DOWN && (circle.getCenterY() <= scene.getHeight() - 5))) {
circle.setCenterY(circle.getCenterY() + 5);
}
else if ((e.getCode() == KeyCode.RIGHT) && (circle.getCenterX() <= scene.getWidth() - 5)) {
circle.setCenterX(circle.getCenterX() + 5);
}
else if ((e.getCode() == KeyCode.LEFT && (circle.getCenterX() >= 5))) {

circle.setCenterX(circle.getCenterX()-5);
}
}
});

//creates new spots by clicking anywhere on the pane
pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
double newX = event.getX(); //getting the x-coordinate of the clicked area
double newY = event.getY(); //getting the y-coordinate of the clicked area

Circle newSpot = new Circle(newX, newY,20);
newSpot.setFill(Color.WHITE);
newSpot.setStroke(Color.BLACK);
pane.getChildren().add(newSpot);

}
});

primaryStage.setTitle("Move the circle");
primaryStage.setScene(scene);
primaryStage.show();
}

另请查看以下链接的答案:

关于Javafx KeyEvent 和 MouseEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29722840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com