英文:
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("sample.fxml"));
primaryStage.setTitle("Hello World");
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 {
}
<?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>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论