如何在JavaFX Scene Builder的文本字段中获取用户输入?

huangapple go评论159阅读模式
英文:

How to get user input in JavaFX Scene Builder Text field?

问题

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class Controller {
    @FXML
    private TextField Textfield;

    // You can add event handling methods here
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
            prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Label layoutX="72.0" prefHeight="20.0" prefWidth="457.0" text="GUESSING GAME">
            <font>
                <Font name="Arial Black" size="48.0"/>
            </font>
        </Label>
        <Label layoutX="72.0" layoutY="187.0" text="Your guess:">
            <font>
                <Font name="Arial" size="22.0"/>
            </font>
        </Label>
        <Label layoutX="395.0" layoutY="187.0" text="Attempts:">
            <font>
                <Font name="Arial" size="22.0"/>
            </font>
        </Label>
        <TextField fx:id="Textfield" layoutX="197.0" layoutY="188.0" prefHeight="25.0" prefWidth="66.0"/>
        <Label layoutX="496.0" layoutY="188.0" text="0">
            <font>
                <Font name="Arial" size="22.0"/>
            </font>
        </Label>
    </children>
</AnchorPane>

Note: I've provided the translated code segments as requested, but it's important to point out that your XML snippet and the provided Java classes are incomplete. You will need to fill in the missing parts, particularly in the Controller class where you can add event handling methods to interact with the TextField.

英文:

I'm new with JavaFX and made a Textfield in the Scene Builder. But I don't find a tutorial where someone explains how to get the input from the Textfield. My code I called my Textfield Texfield(fx:id) and left the On Action empty can you tell me what I have to fill in those two boxes and than in my code?

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));
        primaryStage.setTitle(&quot;Hello World&quot;);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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



package sample;

import com.sun.javafx.scene.control.IntegerField;
import javafx.fxml.FXML;

import java.awt.*;
import java.awt.event.ActionEvent;

public class Controller {
    
}



&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.Label?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.text.Font?&gt;

&lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
   &lt;children&gt;
      &lt;Label layoutX=&quot;72.0&quot; prefHeight=&quot;20.0&quot; prefWidth=&quot;457.0&quot; text=&quot;GUESSING GAME&quot;&gt;
         &lt;font&gt;
            &lt;Font name=&quot;Arial Black&quot; size=&quot;48.0&quot; /&gt;
         &lt;/font&gt;
      &lt;/Label&gt;
      &lt;Label layoutX=&quot;72.0&quot; layoutY=&quot;187.0&quot; text=&quot;Your guess:&quot;&gt;
         &lt;font&gt;
            &lt;Font name=&quot;Arial&quot; size=&quot;22.0&quot; /&gt;
         &lt;/font&gt;
      &lt;/Label&gt;
      &lt;Label layoutX=&quot;395.0&quot; layoutY=&quot;187.0&quot; text=&quot;Attempts:&quot;&gt;
         &lt;font&gt;
            &lt;Font name=&quot;Arial&quot; size=&quot;22.0&quot; /&gt;
         &lt;/font&gt;
      &lt;/Label&gt;
      &lt;TextField fx:id=&quot;Textfield&quot; layoutX=&quot;197.0&quot; layoutY=&quot;188.0&quot; prefHeight=&quot;25.0&quot; prefWidth=&quot;66.0&quot; /&gt;
      &lt;Label layoutX=&quot;496.0&quot; layoutY=&quot;188.0&quot; text=&quot;0&quot;&gt;
         &lt;font&gt;
            &lt;Font name=&quot;Arial&quot; size=&quot;22.0&quot; /&gt;
         &lt;/font&gt;
      &lt;/Label&gt;
   &lt;/children&gt;
&lt;/AnchorPane&gt;

答案1

得分: 1

你需要在你的 fxml 文件中给元素添加一个 id,然后在控制器中使用 @FXML 将其链接起来,变量名必须与你设置的 id 相同。

然后你就可以访问文本框或任何 UI 元素。

英文:

you need to add an id to the element in your fxml and then link it in the controller using @FXML the variable name must be the same name you set as the id.

And then you can access the text field or any UI element

huangapple
  • 本文由 发表于 2020年1月30日 18:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定