Контроллер javafx ничего не делает для события, даже если присутствуют обработчики событий - PullRequest
0 голосов
/ 24 февраля 2020

Кажется, что мой контроллер, main.f xml или main не "связаны" друг с другом. Нет ошибок компиляции, и мой код работает нормально, но когда я пытаюсь выполнить событие, например, нажатием кнопки, кажется, что ничего не происходит, даже если определенно есть обработчик события.

Класс главного контроллера:

package ivashkem;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Assign2Controller {

    @FXML private Button btnAdd;
    @FXML private Button btnSave;
    @FXML private Button btnOrders;
    @FXML private Button btnExit;

    @FXML private TextField txtID;
    @FXML private TextField txtName;
    @FXML private TextField txtQOH;
    @FXML private TextField txtROP;
    @FXML private TextField txtPrice;

    @FXML private TextArea txtDisplay;

    @FXML private void initialize() {
        btnAdd.setOnAction(new addHandler());
        btnExit.setOnAction(e -> onExitClicked());
    }

    public void onAddClicked(){
        System.out.println("Add was clicked");
        txtID.clear();
        txtName.clear();
        txtQOH.clear();
        txtROP.clear();
        txtPrice.clear();
        txtPrice.setText("Test");
    }

    public void onExitClicked(){
        System.out.println("test");
    }

    private class addHandler implements EventHandler<ActionEvent> {
        public void handle(ActionEvent e) {
            onAddClicked();   // Call a method in the outer class
        }
    }
}

Основной класс:

package ivashkem;

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


public class Assign2Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane root = (GridPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
            Scene scene = new Scene(root,446,612);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

F XML:

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

<?import javafx.geometry.Insets?>
<?import javafx.geometry.Point3D?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="612.0" prefWidth="446.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="292.0" minWidth="0.0" prefWidth="0.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="452.0" minWidth="10.0" prefWidth="452.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="205.0" minWidth="0.0" prefWidth="0.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints maxHeight="395.0" minHeight="0.0" prefHeight="75.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="395.0" minHeight="0.0" prefHeight="63.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="395.0" minHeight="10.0" prefHeight="69.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="395.0" minHeight="10.0" prefHeight="75.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="395.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="395.0" minHeight="10.0" prefHeight="77.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="472.0" minHeight="10.0" prefHeight="262.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="472.0" minHeight="10.0" prefHeight="52.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
   <children>
      <HBox alignment="CENTER" prefHeight="58.0" prefWidth="466.0" GridPane.columnIndex="1">
         <children>
            <Label fx:id="lblTitle" alignment="CENTER" prefHeight="24.0" prefWidth="418.0" text="Inventory Tracker" textFill="LIME">
               <font>
                  <Font name="Courier New" size="20.0" />
               </font>
            </Label>
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="351.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <children>
            <Label fx:id="lblID" prefHeight="21.0" prefWidth="80.0" text="Item ID:" textFill="#3c2ae4">
               <HBox.margin>
                  <Insets />
               </HBox.margin>
               <padding>
                  <Insets left="25.0" top="3.0" />
               </padding>
            </Label>
            <TextField fx:id="txtID" prefHeight="31.0" prefWidth="120.0" promptText="Item ID" />
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <children>
            <Label fx:id="lblName" prefHeight="21.0" prefWidth="80.0" text="Item Name:" textFill="#3c2ae4">
               <padding>
                  <Insets top="3.0" />
               </padding>
            </Label>
            <TextField fx:id="txtName" prefHeight="31.0" prefWidth="305.0" promptText="Item Name" />
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="3">
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <children>
            <Label fx:id="lblQOH" prefHeight="21.0" prefWidth="80.0" text="Q-O-H:" textFill="#3c2ae4">
               <padding>
                  <Insets left="25.0" top="3.0" />
               </padding>
            </Label>
            <TextField fx:id="txtQOH" prefHeight="31.0" prefWidth="120.0" promptText="Qty On Hand" />
            <Label fx:id="lblROP" text="R-O-P:" textFill="#3c2ae4">
               <padding>
                  <Insets top="3.0" />
               </padding>
            </Label>
            <TextField fx:id="txtROP" prefHeight="31.0" prefWidth="120.0" promptText="Re-Order Point" />
         </children>
      </HBox>
      <HBox prefHeight="100.0" prefWidth="409.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <children>
            <Label fx:id="lblPrice" prefHeight="21.0" prefWidth="80.0" text="Sell Price:" textFill="#3c2ae4">
               <padding>
                  <Insets left="10.0" top="3.0" />
               </padding>
            </Label>
            <TextField fx:id="txtPrice" prefHeight="31.0" prefWidth="120.0" promptText="Sell Price" />
         </children>
      </HBox>
      <HBox prefHeight="83.0" prefWidth="394.0" spacing="30.0" GridPane.columnIndex="1" GridPane.rowIndex="5">
         <padding>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
         </padding>
         <children>
            <Button fx:id="btnAdd" text="_Add">
               <HBox.margin>
                  <Insets left="35.0" />
               </HBox.margin>
            </Button>
            <Button fx:id="btnSave" text="_Save" />
            <Button fx:id="btnOrders" text="_Orders" />
            <Button fx:id="btnExit" text="_Exit" />
         </children>
      </HBox>
      <TextArea fx:id="txtDisplay" editable="false" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
      <Label text="Message" GridPane.columnIndex="1" GridPane.rowIndex="7" />
   </children>
   <rotationAxis>
      <Point3D />
   </rotationAxis>
</GridPane>

1 Ответ

3 голосов
/ 24 февраля 2020

Вам не хватает атрибута fx:controller в элементе root файла F XML, который должен указывать полное имя класса, используемого для создания экземпляра контроллера:

<GridPane fx:controller= "ivashkem.Assign2Controller"  maxHeight=Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="612.0" prefWidth="446.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">

...

</GridPane>
...