尝试设置没有运气的 textField 文本

Trying to set text of a textField with no luck

我正在尝试从数据库中读取数据,并在单击按钮时将其设置为文本字段中的文本。我一辈子都弄不明白为什么这段代码不起作用。任何帮助表示赞赏。标签有效,而文本字段无效。他们在同一个锚窗格中。

这是我的 FXMLcontroller.java 文件中的代码。我使用 SceneBuilder 创建了 UI.

package winfin_test;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 Sam
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    private TextField textField1 = new TextField();


    @FXML
    private void handleButtonData(ActionEvent event) {

            try {
            //Connect to the database
            String host = "jdbc:mysql://localhost:3306/my_database";
            String uName = "root";
            String uPass = "data";
            Connection con = DriverManager.getConnection(host, uName, uPass);

            //Execute some SQL and load the records into the resultset
            Statement stmt = con.createStatement();
            String SQL = "Select * FROM data_test";
            ResultSet rs = stmt.executeQuery(SQL);

            //Move the cursor to the first record and get data
            rs.next();
            int id_col = rs.getInt("Auto_ID");
            String id = Integer.toString(id_col);
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dob = rs.getString("Birthday");
            String phone = rs.getString("Phone");

            //Display the first record in the text fields
              label.setText(first);
              textField1.setText(last);
        }
        catch (SQLException err) {
            System.out.println(err.getMessage());
        }
        System.out.println("You clicked me!");
        //label.setText("Well Done!");
    }



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

    }    

}

问题是您从未将 textField 添加到您的场景中,您的 Label 有 @FXML 标签,但是您尝试动态创建的 textField 却从不显示。相反,在您的 .fxml 文档中定义文本字段,然后将您的代码编辑为以下内容:

package winfin_test;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 Sam
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    @FXML
    private TextField textField1;


    @FXML
    private void handleButtonData(ActionEvent event) {

            try {
            //Connect to the database
            String host = "jdbc:mysql://localhost:3306/my_database";
            String uName = "root";
            String uPass = "data";
            Connection con = DriverManager.getConnection(host, uName, uPass);

            //Execute some SQL and load the records into the resultset
            Statement stmt = con.createStatement();
            String SQL = "Select * FROM data_test";
            ResultSet rs = stmt.executeQuery(SQL);

            //Move the cursor to the first record and get data
            rs.next();
            int id_col = rs.getInt("Auto_ID");
            String id = Integer.toString(id_col);
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dob = rs.getString("Birthday");
            String phone = rs.getString("Phone");

            //Display the first record in the text fields
              label.setText(first);
              textField1.setText(last);
        }
        catch (SQLException err) {
            System.out.println(err.getMessage());
        }
        System.out.println("You clicked me!");
        //label.setText("Well Done!");
    }



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

    }    

}

我知道在链接到 fx:id 的每个变量声明之前都需要写 @FXML 似乎很愚蠢,但事实就是如此。如果你有多个相同类型的变量,(例如:一组标签)你只需要放一次然后用逗号分隔,就像这样:

@FXML
Label label1, label2, label3, label4;

这为您节省了一些代码。