JavaFXKeyEvent和MouseeEvent
我正在努力学习javafx。我编写了大部分代码,但在使用start方法时遇到了问题 我想做的是通过点击在屏幕上添加点。如果我按1或0,将来添加的斑点会变成不同的颜色。因此,我知道我必须使用JavaFXKeyEvent和MouseeEvent,java,javafx,Java,Javafx,我正在努力学习javafx。我编写了大部分代码,但在使用start方法时遇到了问题 我想做的是通过点击在屏幕上添加点。如果我按1或0,将来添加的斑点会变成不同的颜色。因此,我知道我必须使用setOnMouseClicked 和setOnKeyPressed方法,但互联网上没有太多关于它的内容 import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Group; import jav
setOnMouseClicked
和setOnKeyPressed
方法,但互联网上没有太多关于它的内容
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;
}
}
}
导入javafx.application.application;
导入javafx.stage.stage;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
公共类站点扩展应用程序{
公共静态最终整数大小=500;
公共静态最终int SPOT_半径=20;
私人链接列表;
私人色彩;
公共静态void main(字符串…参数){
发射(args);
}
公众假期开始(阶段){
舞台。片名(“现场”);
dotList=new SinglyLinkedList();
组根=新组();
场景=新场景(根,500,500,颜色.黑色);
斑点r;
// ...
stage.show();
}
私家班的地点扩大了{
公共场所(双XPO、双YPO){
超级(XPO、YPO、SPOT_半径);
设置填充(颜色);
}
公共布尔包含(双XPO、双YPO){
double dx=xPos-getCenterX();
double dy=yPos-getCenterY();
双距离=Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
返回距离通常,您会使用setOnAction
,如中所示
例如:
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Hello World");
}
});
圆圈不接受的原因是它没有聚焦。要让节点响应关键事件,它们应该聚焦。您可以通过
在节点上调用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();
}
public void start(Stage primaryStage)引发异常{
窗格=新窗格();
最终场景=新场景(窗格,500500);
最终循环=新循环(250、250、20);
圆形。设置填充(颜色。白色);
圆整定行程(颜色为黑色);
pane.getChildren().add(圆圈);
圆圈。setFocusTraversable(真);
circle.setOnKeyPressed(新的EventHandler(){
@凌驾
公共无效句柄(KeyEvent e){
如果((e.getCode()==KeyCode.UP)&&(circle.getCenterY()>=5)){
circle.setCenterY(circle.getCenterY()-5);
}
否则如果((e.getCode()==KeyCode.DOWN&&(circle.getCenterY()解决方案方法
您可以监视场景中键入的关键点事件,并在此基础上切换颜色模式。您可以在场景根窗格上放置鼠标事件处理程序,并在用户单击窗格中的任意位置时向场景添加一个圆圈(颜色与当前颜色模式相适应)
示例代码
进一步资料
- 有关JavaFX中事件处理的详细信息,请参阅
谢谢!但我如何才能在单击屏幕时显示一个新的点?我的根本问题是鼠标事件。非常感谢!在您的帮助下,我现在理解了。不客气。我建议您看看这个。它通过从头开始创建JavaFX应用来教您。Ty用于链接!我还有最后一个问题。如果我想使用群插件除了pane,我怎么做?谢谢!如果我使用group而不是pane,我将如何做?我想利用我提供的点列表。使用pane或group与点列表无关。pane和group都是子项,并且都有子项,因此子项列表handin的实现如果您使用窗格或组,则g没有什么不同。
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();
}
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
// Java 8+ code.
public class Spots extends Application {
private static final int SIZE = 500;
private static final int SPOT_RADIUS = 20;
private Color color = Color.BLUE;
public void start(Stage stage) {
Pane root = new Pane();
root.setOnMouseClicked(event ->
root.getChildren().add(
new Spot(
event.getX(),
event.getY(),
color
)
)
);
Scene scene = new Scene(root, SIZE, SIZE, Color.BLACK);
scene.setOnKeyTyped(event -> {
switch (event.getCharacter()) {
case "0":
color = Color.BLUE;
break;
case "1":
color = Color.RED;
break;
}
});
stage.setScene(scene);
stage.show();
}
private class Spot extends Circle {
public Spot(double xPos, double yPos, Color color) {
super(xPos, yPos, SPOT_RADIUS);
setFill(color);
}
}
public static void main(String... args) {
launch(args);
}
}