如何使用JavaFx中的箭头键更改Line的起点和终点? [英] How to change the start and end point of Line using Arrow Keys in JavaFx?

查看:350
本文介绍了如何使用JavaFx中的箭头键更改Line的起点和终点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaFx在Java中创建一个简单的程序,使用箭头键设置线对象的起点和终点。基本上,这个想法是按下箭头键来制作像小吃一样的线条。我使用了 setOnPressedKey 事件,但它不起作用,但是当我删除事件并运行程序时, setStartX 并且 setStartY 等工作原理。

I am trying to make a simple program in Java using JavaFx to set the start and end point of the line object with Arrow Keys. Basically, the idea is to make line like snack with pressing the Arrow Keys. I used the setOnPressedKey event but it doesn't work, but when I remove the event and run the program the setStartX and setStartY and so on works.

Line line = new Line();
line.setStroke(Color.BLACK);

line.setOnKeyPressed(e -> {
    if (e.getCode() == KeyCode.UP) {
        line.setStartX(line.getEndX() + 0);
        line.setStartY(line.getEndY() + 15);
    }
});
layout.getChildren().add(line);


推荐答案

您正在组件上添加事件请求焦点。

You're adding an event on a component that doesn't request focus.

设置事件并关注根组件。

Set the event and focus on the root component.

Line line = new Line();
line.setStroke(Color.BLACK);

layout.setOnKeyPressed(e -> {
    if (e.getCode() == KeyCode.UP) {
        line.setStartX(line.getEndX() + 0);
        line.setStartY(line.getEndY() + 15);
    }
});
layout.getChildren().add(line);
layout.requestFocus();

这篇关于如何使用JavaFx中的箭头键更改Line的起点和终点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆