我正在用Java开发一个待办事项应用程序,其中有一个场景,其中包含两个文本字段和一个按钮,似乎可以在SceneBuilder的“预览”模式下使用(我可以键入这些字段并单击该按钮),但是当我运行程序时,无法在此场景中编辑文本字段。我之前已经成功构建了另外三个场景,我也使用了其中的文本字段,它们可以很好地工作,但是我不知道为什么这个不允许我编辑文本字段。
这是非工作状态的FXML:
<AnchorPane prefHeight="500.0" prefWidth="800.0" style="-fx-background-color: #4db6ac;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.newTaskController">
<children>
<AnchorPane fx:id="rootAnchorPane" layoutY="170.0" prefHeight="330.0" prefWidth="800.0" style="-fx-background-color: #fff8e1;">
<children>
<JFXTextField fx:id="taskText" focusColor="#4db6ac" layoutX="188.0" layoutY="55.0" maxWidth="424.0" minWidth="357.0" prefHeight="59.0" prefWidth="424.0" promptText="Task title">
<font>
<Font size="24.0" />
</font>
</JFXTextField>
<JFXTextField fx:id="descriptionText" focusColor="#4db6ac" layoutX="188.0" layoutY="136.0" maxWidth="424.0" minWidth="357.0" prefHeight="59.0" prefWidth="424.0" promptText="Description">
<font>
<Font size="24.0" />
</font>
</JFXTextField>
<JFXButton fx:id="saveTaskButton" layoutX="341.0" layoutY="217.0" prefHeight="52.0" prefWidth="119.0" style="-fx-background-color: #4db6ac;" text="Save" />
</children>
</AnchorPane>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="204.0" layoutY="34.0" text="To do List">
<font>
<Font name="Autumn in November" size="55.0" />
</font>
</Label>
</children>
</AnchorPane>
在SceneBuilder中,将文本字段设置为可编辑的,就像以前对其他节点所做的那样。
这是视图的控制器:
package sample.controller;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import sample.adatok.AdatHandler;
import sample.model.Task;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
public class newTaskController {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private AnchorPane rootAnchorPane;
@FXML
private JFXTextField taskText;
@FXML
private JFXTextField descriptionText;
@FXML
private JFXButton saveTaskButton;
@FXML
void initialize() {
saveTaskButton.setOnAction(event -> {
printMessage();
createTask();
Parent newUserParent = null;
try {
newUserParent = FXMLLoader.load(getClass().getResource("/sample/view/newitem.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene newUserScene = new Scene(newUserParent);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(newUserScene);
window.show();
});
}
private void printMessage() {
System.out.println("Current user ID: " + this.getId());
}
private void createTask() {
AdatHandler adatHandler = new AdatHandler();
String tasktext = taskText.getText();
String descriptiontext = descriptionText.getText();
LocalDateTime currentDateTime = LocalDateTime.now();
//Build formatter
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
//Format LocalDateTime
String formattedDateTime = currentDateTime.format(formatter);
Task task = new Task(tasktext, formattedDateTime, descriptiontext);
String currentid = this.getId();
adatHandler.addNewTask(task, currentid);
}
private String id;
public String getId() {
return id;
}
public void initDataT(String currentid) {
id = currentid;
}
}