按下按钮 FXML 时访问对象的方法
Access object's methods when pushing button FXML
我一直在使用 Python 和 tkinter。我现在正在学习 JavaFXML。
我在通过控制器访问对象时遇到问题。
我有一个名为 class 的用户。我在主 Java class 中创建了一个用户,它扩展了应用程序。用户有位置 X 和位置 Y。当我按下按钮时,我想更改用户的位置。当使用不带 FXML 的 JavaFX 时,我很容易做到这一点,因为所有内容都在同一个 class 中声明。但是当我使用 FXML 时,我正在处理一个控制器 class,它不知道用户 class,因为我在我的主 class 中调用了它。我试图在控制器中启动 User class,这看起来很傻而且没有用。我还尝试寻找一种将用户对象传递给控制器的方法,但找不到有关如何操作的页面。这是代码:
主要class:
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) {
}
}
我认为我不需要向用户展示 class。
你可以将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 );
}
我一直在使用 Python 和 tkinter。我现在正在学习 JavaFXML。 我在通过控制器访问对象时遇到问题。 我有一个名为 class 的用户。我在主 Java class 中创建了一个用户,它扩展了应用程序。用户有位置 X 和位置 Y。当我按下按钮时,我想更改用户的位置。当使用不带 FXML 的 JavaFX 时,我很容易做到这一点,因为所有内容都在同一个 class 中声明。但是当我使用 FXML 时,我正在处理一个控制器 class,它不知道用户 class,因为我在我的主 class 中调用了它。我试图在控制器中启动 User class,这看起来很傻而且没有用。我还尝试寻找一种将用户对象传递给控制器的方法,但找不到有关如何操作的页面。这是代码:
主要class:
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) {
}
}
我认为我不需要向用户展示 class。
你可以将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 );
}