【问题标题】:Why doesn't my fxml controller class move my button?为什么我的 fxml 控制器类不移动我的按钮?
【发布时间】:2016-12-20 16:48:08
【问题描述】:

我目前正在使用 3 个类:ButtonMove.java、MainController.java 和 Main.fxml。我一直在尝试使用 SceneBuilder 来创建用户界面,并希望为对象添加运动。我试过让这个按钮移动,但无济于事。舞台启动,我看到了按钮,但它仍然一动不动。

package TestClasses;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ButtonMove extends Application
{

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);

    }


}

上面的类 ButtonMove.java 只是加载 fxml 文件 Main.fxml 并启动舞台。下面是 Main.fxml 控制器文件,由于某种原因,它一直告诉我 javafx.fxml.FXML 从未使用过。

package TestClasses;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.util.Duration;

public class MainController implements Initializable
{

    private Button button;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        TranslateTransition transition = new TranslateTransition();
        transition.setDuration(Duration.seconds(4));
        transition.setNode(button);
        transition.setToY(-200);
        transition.play();
    }

}

而这只是 SceneBuilder 生成的 fxml 文件。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button id="button" layoutX="274.0" layoutY="310.0" mnemonicParsing="false" text="CLICK" />
         </children>
      </AnchorPane>
   </children>
</StackPane>

【问题讨论】:

    标签: java user-interface javafx fxml scenebuilder


    【解决方案1】:

    由于某种原因一直告诉我 javafx.fxml.FXML 从未使用过。

    这是因为你没有在代码的任何地方使用@FXML注解。

    要将 fxml 连接到控制器,您需要

    1. 通过注释使应该注入的字段对FXMLLoader可见:

      @FXML
      private Button button;
      
    2. 通过将fx:controller 属性添加到根元素来指定要与fxml 一起使用的控制器类:

      <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestClasses.MainController">
      

      或者在调用FXMLLoader.load之前指定一个控制器实例

      FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
      loader.setController(new MainController());
      Parent root = loader.load();
      
    3. 为要注入控制器的对象指定fx:id 属性:

      <Button fx:id="button" layoutX="274.0" layoutY="310.0" mnemonicParsing="false" text="CLICK" />
      

    【讨论】:

    • 非常感谢。我让它工作了。我对 javaFX 很陌生,我仍在学习语法。您的解释清晰、简洁且条理清晰。
    【解决方案2】:

    在此处添加 FXML 标签:

     @FXML
     private Button button;
    

    否则按钮不会被初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多