【问题标题】:Scene builder event handler ( Key listener)场景构建器事件处理程序(键侦听器)
【发布时间】:2018-04-29 13:43:32
【问题描述】:

大家好,我有一个问题,我正在使用 eclipse、javafx 和场景生成器 当我使用箭头(右左......)时,我想移动椭圆(圆圈) 这是我的代码(我尝试了 10 件事,但似乎没有任何效果)这是我尝试过的最后一个想法,如果有人可以帮助谢谢:)

这是我的主要课程:

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语句,改变我的主要,改变fxml但没有奏效)

【问题讨论】:

标签: java javafx scenebuilder


【解决方案1】:

你必须使用场景而不是窗格来检测按键

    @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();
}

【讨论】:

  • 即使我只想移动圆圈?我使用场景?
  • 我相信您正在尝试检测整个场景的按键。因此,使用场景来检测按键是一个合适的解决方案。
  • 我不能像在示例中那样在其他类中编写代码并在主程序中使用它吗?
【解决方案2】:

在代码中

  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

【讨论】:

  • omg 感谢它的工作,我会尝试做更多的代码,看看是否一切正常,但你能告诉我最后一个你告诉我它的重要用途吗?
  • @AimenBadaoui 声明scene.getRoot().requestFocus() 很重要,因为根窗格在获得焦点之前不会收到关键事件。该语句只是请求焦点,以便根窗格可以侦听键事件
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2016-03-01
相关资源
最近更新 更多