【发布时间】:2020-05-15 23:10:26
【问题描述】:
我是 JavaFX 新手,在 Scene Builder 中创建了一个文本字段。但我找不到有人解释如何从文本字段获取输入的教程。我的代码我调用了我的 Textfield Texfield(fx:id) 并将 On Action 留空,你能告诉我我必须在这两个框中填写什么而不是在我的代码中填写什么吗?
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>
【问题讨论】:
-
是时候阅读和理解关于 fx 的基本教程了(javafx 标签 wiki 有参考).. 使用像 scenebuilder 这样的代码/fxml 生成工具是不够的,你需要理解 它在做什么 .. 还有:你的导入很奇怪,不要将 swing/awt 与 fx 混合使用!
-
这能回答你的问题吗? What are JavaFX, FXML and Scene Builder?
标签: java user-interface javafx input scenebuilder