简体   繁体   中英

Why doesn't my fxml controller class move my button?

I have 3 classes with which I'm currently working: ButtonMove.java, MainController.java, and Main.fxml. I've been experimenting with SceneBuilder to create user interfaces and wanted to add motion to an object. I've tried getting this button to move, but to no avail. The stage launches and I see the button, but it remains motionless.

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

    }


}

The above class, ButtonMove.java, just loads the fxml file, Main.fxml, and launches the stage. Below is the Main.fxml controller file, which for some reason keeps telling me that javafx.fxml.FXML is never used.

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

}

And this is just the fxml file generated by SceneBuilder.

<?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>

which for some reason keeps telling me that javafx.fxml.FXML is never used.

It's because you don't use the @FXML annotation anywhere in the code.

To connect the fxml to the controller, you need to

  1. Make the field that should be injected visible to the FXMLLoader by annotating it:

     @FXML private Button button; 
  2. Specify the controller class to be used with the fxml by adding the fx:controller attribute to the root element:

     <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"> 

    or by specifying a controller instance before calling FXMLLoader.load

     FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml")); loader.setController(new MainController()); Parent root = loader.load(); 
  3. Specify the fx:id attribute for objects you want to inject to the controller:

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

add an FXML tag here:

 @FXML
 private Button button;

otherwise the button is not initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM