gpt4 book ai didi

java - 从文本字段获取文本java fxml

转载 作者:行者123 更新时间:2023-11-30 02:57:44 24 4
gpt4 key购买 nike

这是我的 fxml 代码和 Java Controller 文件代码。我正在尝试使用“String s = tf.getText().toString();”从句柄事件中的 TEXFIELD tf 获取文本但它没有被执行。

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id = "aPane" prefHeight="268.0" prefWidth="379.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.searchController">
<children>
<VBox layoutX="20.0" layoutY="36.0" prefHeight="232.0" prefWidth="333.0">
<children>
<HBox prefHeight="36.0" prefWidth="333.0">
<children>
<Label fx:id= "kw" text="Key Word : ">
<padding>
<Insets right="10.0" />
</padding>
</Label>
<TextField fx:id="tf" prefHeight="25.0" prefWidth="120.0" />
<Button fx:id="srch" mnemonicParsing="false" onAction="#handle" text="Search" >
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
</children>
<padding>
<Insets left="6.0" top="6.0" />
</padding>
<VBox.margin>
<Insets />
</VBox.margin>
<opaqueInsets>
<Insets />
</opaqueInsets>
</HBox>
<TextArea fx:id="ta" prefHeight="174.0" prefWidth="282.0" />
</children>
</VBox>
</children>
</AnchorPane>

JAVA Controller 代码:

public class searchController implements Initializable,EventHandler<ActionEvent> {

AnchorPane aPane = new AnchorPane();
Label kw = new Label();
public TextField tf;
Button srch = new Button();
TextArea ta = new TextArea();
//Text t;
String s = "priyam";

@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
tf = new TextField();
}

@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
s=tf.getText().toString();
System.out.println(s);
}
}

最佳答案

方法initialize()实际上覆盖了tf的值。它使用新对象初始化您的 TextField 对象 tf

@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
tf = new TextField();
}

关于java - 从文本字段获取文本java fxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36784527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com