Java 场景生成器事件处理程序(键侦听器)
大家好,我有个问题,我正在使用eclipse、javafx和场景生成器 我想在使用箭头(右-左…)时移动椭圆(圆) 这是我的代码(我尝试了10件事,但似乎都不管用),这是我最后一次尝试,如果有人能帮忙的话,谢谢:) 这是我的主要课程:Java 场景生成器事件处理程序(键侦听器),java,javafx,scenebuilder,Java,Javafx,Scenebuilder,大家好,我有个问题,我正在使用eclipse、javafx和场景生成器 我想在使用箭头(右-左…)时移动椭圆(圆) 这是我的代码(我尝试了10件事,但似乎都不管用),这是我最后一次尝试,如果有人能帮忙的话,谢谢:) 这是我的主要课程: package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}}
这是我的控制器类:
package application;
import javafx.fxml.FXML;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Ellipse;
public class SampleController {
@FXML
private Ellipse Test;
@FXML
public void keyPressed(KeyEvent key){
if(key.getCode().isArrowKey()){
System.out.println("rlrlrl");
}}}
我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.Double?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.shape.Ellipse?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.shape.Rectangle?>
<Pane onKeyPressed="#keyPressed" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0"
prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<Polygon fill="DODGERBLUE" layoutX="507.0" layoutY="587.0" rotate="180.0"
stroke="BLACK" strokeType="INSIDE">
<points>
<Double fx:value="-50.0" />
<Double fx:value="40.0" />
<Double fx:value="50.0" />
<Double fx:value="40.0" />
<Double fx:value="0.0" />
<Double fx:value="-60.0" />
</points>
</Polygon>
<Rectangle arcHeight="5.0" arcWidth="5.0" height="200.0" layoutX="-3.0" layoutY="577.0" stroke="BLACK" strokeType="INSIDE" width="809.0">
<fill>
<LinearGradient endX="1.0" endY="1.0">
<stops>
<Stop color="#4c6b33" />
<Stop color="WHITE" offset="1.0" />
</stops>
</LinearGradient>
</fill>
</Rectangle>
<Ellipse fx:id="Test" fill="DODGERBLUE" layoutX="60.0" layoutY="558.0"
radiusX="23.0" radiusY="19.0" stroke="BLACK" strokeType="INSIDE" />
</children>
</Pane>
我在每个论坛上都试过了;(switch语句,changing my main,changing on the fxml,但没有起作用)您必须使用场景窗格来检测按下的键
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/application/Sample.fxml"));
Scene scene = new Scene(root);
scene.setOnKeyPressed(event -> {
String codeString = event.getCode().toString();
if (event.getCode().isArrowKey()) {
System.out.println("rlrlrl");
}
});
stage.setScene(scene);
stage.show();
}
编码
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show()
将此更改为
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
Pane root = loader.load();
Scene scene = new Scene(root)
primaryStage.setScene(scene);
primaryStage.show()
scene.getRoot().requestFocus(); // important
可能的重复我已经看到了这一个,它不谈论FXML和场景生成器它只是关于JavaFx,谢谢你花时间,即使我想移动这个圆圈?我使用这个场景?我相信你是在试图检测整个场景中按下的键。所以,使用scene检测按键是一个合适的解决方案。我能不能像在示例中那样在其他类中编写代码并在主要部分使用它?omg感谢它的工作,我会尝试编写更多代码,看看是否所有代码都能正常工作,但你能告诉我最后一个代码在哪里说明它的重要性吗?@AimenBadaoui statement
scene.getRoot().requestFocus()
非常重要,因为根窗格只有在具有焦点时才会接收键事件。此语句只是请求用户的焦点,以便根窗格可以侦听keyevents