【发布时间】: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