场景构建器事件处理程序(Key listener)

时间:2018-04-29 13:43:32

标签: java javafx scenebuilder

你好我们有问题,我使用的是eclipse,javafx和场景构建器 我想使用箭头(左边......)移动椭圆(圆圈) 这是我的代码(我尝试了10件事,但似乎没有任何工作)这是我试过的最后一个想法,如果任何人可以帮助谢谢:)

这是我的主要课程:

<div id="widget">
  <div class="container">
    <div class="header">
      <h4>Let's Enhance</h4>
    </div>
    <div class="info">
      <div>
      </div>
    </div>

这是我的控制器类:

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

我的FXML文件:

 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");
    }}}

我在每个论坛上尝试过一切; (switch语句,改变我的主要,改变fxml但没有工作)

2 个答案:

答案 0 :(得分:0)

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

<?php echo ${'jne_reguler_' . $geo_zone['geo_zone_id'] . '_rate'}; ?>

答案 1 :(得分:0)

代码

  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
相关问题