有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么JavaFX文本字段侦听器不工作?

我想在我按下前一个窗口上的按钮时出现的窗口中的文本字段中添加一个侦听器。问题是听众根本不工作。它检测不到任何变化

public class WindowTwoController {

private Stage stage;

@FXML
private TextField imieTF = new TextField();


public void show() throws IOException {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
    Parent root = loader.load();

    stage = new Stage();
    stage.initModality(Modality.APPLICATION_MODAL);

    stage.setTitle("Sell Art");
    stage.setScene(new Scene(root, 500, 500));

    imieTF.textProperty().addListener((observable, oldValue, newValue) -> {
        System.out.println("textfield changed from " + oldValue + " to " + newValue);
    });

    stage.showAndWait();

}

我正在更改文本字段的值,但没有任何内容输出到控制台。当按下上一个窗口上的按钮时,将调用show方法。请帮帮我。这是我的胜利之二。fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<GridPane alignment="center" hgap="10" prefHeight="441.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="windowTwo.WindowTwoController">
<columnConstraints>
    <ColumnConstraints />
</columnConstraints>
<rowConstraints>
    <RowConstraints />
</rowConstraints>
<children>
    <VBox prefHeight="354.0" prefWidth="455.0">
        <children>
            <Label alignment="CENTER" prefHeight="45.0" prefWidth="457.0" text="Sprzedaż dzieła">
                <font>
                    <Font size="30.0" />
                </font>
            </Label>
            <Separator prefWidth="200.0" />
            <GridPane prefHeight="139.0" prefWidth="455.0">
                <columnConstraints>
                    <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>
                <children>
                    <Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Imię">
                        <font>
                            <Font size="28.0" />
                        </font>
                    </Label>
                    <Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Nazwisko" GridPane.rowIndex="1">
                        <font>
                            <Font size="28.0" />
                        </font>
                    </Label>
                    <TextField fx:id="imieTF" GridPane.columnIndex="1">
                        <GridPane.margin>
                            <Insets right="20.0" />
                        </GridPane.margin></TextField>
                    <TextField fx:id="nazwiskoTF" GridPane.columnIndex="1" GridPane.rowIndex="1">
                        <GridPane.margin>
                            <Insets right="20.0" />
                        </GridPane.margin>
                    </TextField>
                </children>
            </GridPane>
            <Separator prefWidth="200.0" />
            <Label alignment="CENTER" prefHeight="17.0" prefWidth="450.0" text="Klient powracający">
                <font>
                    <Font size="22.0" />
                </font>
            </Label>
            <Separator prefWidth="200.0" />
            <GridPane prefHeight="126.0" prefWidth="455.0">
                <columnConstraints>
                    <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>
                <children>
                    <Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Poziom Zaprzyjaźnienia" textFill="#00000080">
                        <font>
                            <Font size="19.0" />
                        </font>
                    </Label>
                    <ComboBox fx:id="cb" opacity="0.5" prefHeight="25.0" prefWidth="207.0" GridPane.columnIndex="1" />
                    <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Akceptuj" GridPane.rowIndex="1">
                        <GridPane.margin>
                            <Insets left="25.0" />
                        </GridPane.margin>
                    </Button>
                    <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Anuluj" GridPane.columnIndex="1" GridPane.rowIndex="1">
                        <GridPane.margin>
                            <Insets left="27.0" />
                        </GridPane.margin>
                    </Button>
                </children>
            </GridPane>
        </children>
    </VBox>
</children>
</GridPane>

共 (1) 个答案

  1. # 1 楼答案

    你应该像这样给私人会员imieTF添加一个地址

    public class WindowTwoController {
      @FXML
      private TextField imieTF;
    
      @FXML
      private void initialize() {
          imieTF.textProperty().addListener((observable, oldValue, newValue) -> 
           {
             System.out.println("textfield changed from " + oldValue + " to " + newValue);
           });
      }
    
      public static void show() {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
        Parent root = loader.load();
    
        stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
    
        stage.setTitle("Sell Art");
        stage.setScene(new Scene(root, 500, 500));
        stage.showAndWait();
      }
    }
    

    这会将TextField实例绑定到控制器。但是从我可以发现,当属于FXML文件的窗口被创建时,FXML将创建一个WindowTwoController的新实例

    另请参见https://www.callicoder.com/javafx-fxml-form-gui-tutorial/了解有关其工作原理的基本示例

    请注意,文本字段的所有操作都应该是JavaFX流的一部分,不能在WindowTwoController的手动实例中完成。请记住,JavaFX将在loader.load();操作期间创建自己的WindowTwoController实例