【问题标题】:Access object's methods when pushing button FXML按下按钮 FXML 时访问对象的方法
【发布时间】:2015-08-10 05:44:20
【问题描述】:

我一直在使用 Python 和 tkinter。我现在正在学习 JavaFXML。 我在通过控制器访问对象时遇到问题。 我有一个名为 User 的类。我在我的主 Java 类中创建了一个用户,该类扩展了 Application。用户有一个位置 X 和一个位置 Y。当我按下按钮时,我想更改我的用户的位置。当使用没有 FXML 的 JavaFX 时,我很容易做到这一点,因为所有内容都在同一个类中声明。但是当我使用 FXML 时,我正在处理一个不知道 User 类的控制器类,因为我在我的主类中调用了它。我试图在控制器中启动 User 类,它看起来很傻而且没有用。我还尝试寻找一种将用户对象传递给控制器​​的方法,但找不到有关如何做的页面。代码如下:

主类:

package simx;

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

public class SimX extends Application {
    User user;
    World world;
    Human human;

    public SimX() {
    }

    @Override
    public void start(Stage stage) throws Exception {

        user = new User(10, 10);

        Parent root = FXMLLoader.load(getClass().getResource("UIFXML.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();

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

}

我的 FXML:

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="480.0" prefWidth="640.0"     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="simx.UIFXMLController">
   <children>
      <GridPane layoutX="6.0" prefHeight="480.0" prefWidth="640.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <GridPane prefHeight="121.0" prefWidth="193.0" GridPane.columnSpan="2" GridPane.rowSpan="2">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <Button fx:id="moveEastButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveEastButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move East" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                  <Button fx:id="moveSouthButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveSouthButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move South" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <Button fx:id="moveWestButton" layoutX="10.0" layoutY="38.0" onAction="#handleMoveWestButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move West" GridPane.rowIndex="1" />
                  <Label alignment="CENTER" prefHeight="17.0" prefWidth="169.0" text="(X,Y)" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <font>
                    <Font size="24.0" />
                     </font>
                  </Label>
                    <Button fx:id="moveNorthButton" onAction="#handleMoveNorthButtonAction" prefHeight="25.0" prefWidth="500.0" text="Move North" GridPane.columnIndex="1" />
               </children>
            </GridPane>
         </children>
      </GridPane>
    </children>
</AnchorPane>

和我的控制器:/*

package simx;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class UIFXMLController implements Initializable {

@FXML
private Button moveNorthButton;

@FXML
private void handleMoveNorthButtonAction(ActionEvent event) {
    System.out.println("Move North");
    user.moveNorth();
}

@FXML
private Button moveSouthButton;

@FXML
private void handleMoveSouthButtonAction(ActionEvent event) {
    System.out.println("Move South");
}

@FXML
private Button moveEastButton;

@FXML
private void handleMoveEastButtonAction(ActionEvent event) {
    System.out.println("Move East");
}

@FXML
private Button moveWestButton;

@FXML
private void handleMoveWestButtonAction(ActionEvent event) {
    System.out.println("Move West");
}


@Override
public void initialize(URL url, ResourceBundle rb) {
}    

}

我认为我不需要显示 User 类。

【问题讨论】:

    标签: java user-interface javafx javafx-8 fxml


    【解决方案1】:

    您可以将 User 实例交给 Controller 来管理它。首先要做到这一点

    1) 获取控制器实例 uiFXMLController 就像在 Accessing FXML controller class 中一样。
    2) uiFXMLController.setUser(user);
    3) 更新用户的字段

    @FXML
    private void handleMoveEastButtonAction(ActionEvent event) {
        System.out.println("Move East");
        user.setLocationX( user.getLocationX() + 10 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多