javafx:如何处理项目(文本字段、标签等)?

javafx: how to handle items(textfields, labels, etc)?

我使用场景生成器生成布局,在导出我导入到 TextPad 的 fxml 后,布局成功导入,但是我无法通过 id 处理项目(如果它是这样工作的)。我的问题是如何处理我添加的项目。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;

public class test2fx extends Application{


        public static void main(String[] args){

            Application.launch(args);
        }
        public void init(){
        }

        @Override
        public void start(Stage stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("fxlayout.fxml"));
        Scene scene = new Scene(root, 300, 275);
        stage.setTitle("FXML Welcome");
        stage.setScene(scene);

        stage.show();

        }
        public void stop(){
            System.exit(0);
    }

fxml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="284.0" prefWidth="314.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button layoutX="31.0" layoutY="252.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="50.0" text="Jogar" />
      <Button layoutX="124.0" layoutY="252.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="50.0" text="Novo" />
      <Button layoutX="219.0" layoutY="252.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="50.0" text="Sair" />
      <TextField id="tf1" disable="true" layoutX="150.0" layoutY="60.0" />
      <TextField id="tf1" disable="true" layoutX="150.0" layoutY="100.0" />
      <TextField id="tf3" layoutX="150.0" layoutY="140.0" />
      <TextField id="tf4" disable="true" layoutX="150.0" layoutY="180.0" />
      <Label id="lb1" layoutX="38.0" layoutY="60.0" prefHeight="22.0" prefWidth="59.0" text="Inicio" />
      <Label id="lb2" layoutX="38.0" layoutY="100.0" prefHeight="22.0" prefWidth="59.0" text="Fim" />
      <Label id="lb3" layoutX="38.0" layoutY="139.0" prefHeight="22.0" prefWidth="59.0" text="Palpite" />
      <Label id="lb4" layoutX="38.0" layoutY="180.0" prefHeight="22.0" prefWidth="59.0" text="Inicio" />
   </children>
</Pane>

正如@James_D所说,您很可能想要:

  1. 对您希望访问的 FXMl 中的元素的引用。
  2. 用于您的 FXML 的控制器 class。

示例:

/*
 * Dean2191 Whosebug example
 */
package javafxapplication6;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 *
 * @author dean2191
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private TextField tf1; // value will be injected by the FXMLLoader

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

}

检查 oracle 示例 here 并注意 netbeans IDE 生成默认控制器 class 如上所示,但是当在场景构建器中添加新元素时,您应该添加您希望访问的变量和场景的每个 @FXML 注释 builder/netbeans 都具有同步功能,可以查看您的代码引用了哪些元素。

Synchronizing With the Controller Source Code The NetBeans IDE's Make Controller feature allows you to synchronize the modifications you make in the FXML file that is currently opened in Scene Builder and the controller source code opened in NetBeans IDE. To illustrate this feature, do the following:

In Scene Builder, drag a Button control from the Library panel to the Control panel. In the Code panel, assign a new value for the new button's fx:id field and a new method name for the onAction method. Select File in the main menu and then Save. In NetBeans IDE 7.4 or later, right click the project node for the FXML file that you just edited and select Edit from the contextual menu. From the main menu, select Source and then Make Controller. The @FXML private variable and the new onAction method for the button you just added in Scene Builder are created in the controller source file. Use the Make Controller command if you delete an element in the Control panel or update an fx:id value or a method name in Scene Builder.

Source

另请注意,您应该检查此行的 FXMl:

fx:controller="yourNamespace.fxlayout"

因为您的控制器 class 必须在 FXML 中的某处指定。然后,您的控制器将在您的示例代码中被称为 fxlayout.java 之类的名称。这也可以是 set/modified 使用场景生成器: