我们如何从单个服务更新多个控件。现在updateMessage()Service 中只有一个,它的值只能绑定到一个控件,因此只更新它。我们如何更新多个控件的值?
我的服务类实例:
//run a background thread
threadTimeChecker = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
while (!isDone) {
DataHelper.setCurrentDate(LocalDate.now());
if(!DataHelper.getOldDate().equals(DataHelper.getCurrentDate())) {
DataHelper.setIntIndex(DataHelper.getIntIndex()+1);
DataHelper.setOldDate(DataHelper.getCurrentDate());
DataHelper.saveData();
System.out.println("Saved!");
}
//Thread.currentThread().sleep(2000);
updateMessage(wordString.getValue());
}
return null;
}
};
}
};
threadTimeChecker.restart();
//bind string properties to labels
word.textProperty().bind(threadTimeChecker.messageProperty());
Run Code Online (Sandbox Code Playgroud)
这只会更新一条消息,即我只能绑定一个标签。有什么方法可以更新来自同一个线程的多条消息,以便我可以在我的 UI 中绑定多个标签?
已编辑- 根据评论提供更多信息
我的可运行是:
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception { …Run Code Online (Sandbox Code Playgroud) 如您所见,我的文本受labels约束。我如何做label调整大小使之正确配合文字,也以调整VBox和GridPane相应。我只想垂直调整大小。
我的 FXML:
<VBox fx:id="body"
alignment="TOP_CENTER"
prefHeight="150"
GridPane.vgrow="SOMETIMES"
prefWidth="300"
GridPane.columnIndex="0"
GridPane.rowIndex="1" spacing="5">
<Label alignment="CENTER" textAlignment="JUSTIFY" fx:id="word" maxWidth="268"/>
<Pane prefHeight="10" VBox.vgrow="NEVER"/>
<Label alignment="CENTER" textAlignment="JUSTIFY" wrapText="true" fx:id="meaning" maxWidth="268" />
<Label alignment="CENTER" textAlignment="JUSTIFY" wrapText="true" fx:id="sentence" maxWidth="268" />
</VBox>
Run Code Online (Sandbox Code Playgroud)
这VBox是里面的GridPane:
<GridPane fx:id="base"
alignment="CENTER"
prefHeight="210.0"
maxWidth="310.0"
stylesheets="@style.css"
vgap="0" xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
...
Run Code Online (Sandbox Code Playgroud)
@James_D 要求的最小、完整和可验证的示例
主要类:
public class Main extends Application {
private Stage stage;
private StackPane stackPane;
@Override
public void start(Stage primaryStage) …Run Code Online (Sandbox Code Playgroud) 在JsonOperation类中:
public void writeJson(String path, JSONObject passedJsonObj){
File file = new File(path);
try{
if (!file.exists()){
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.write(passedJsonObj.toJSONString());
writer.flush();
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主叫班上:
LocalDate todayDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateString = todayDate.format(formatter).toString();
JsonOperation jsonOp = new JsonOperation();
jsonOp.writeJson("srcsample/SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );
Run Code Online (Sandbox Code Playgroud)
运行此程序时,出现以下错误:
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at sample.JsonOperation.writeJson(JsonOperation.java:50)
at sample.Main.saveData(Main.java:58)
at sample.Main.start(Main.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/384953125.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at …Run Code Online (Sandbox Code Playgroud)