Java 编辑控制器内的文本区域
我不熟悉Java和JavaFX。我已经使用SceneBuilder/FXML创建了一个JavaFX项目,我正在尝试在程序开始时添加一个实时时钟,它可以在屏幕顶部的整个程序中工作。我创建了一个文本区域,并尝试根据我们得到的代码添加时钟函数,但每次启动程序时,它总是显示为空白。即使尝试手动使用.setText(“string”)函数也不起作用,所以我认为我把代码放错地方了。如果可能的话,有人能告诉我这个代码应该去哪里,或者给我指出正确的方向吗 以下是我的主要观点:Java 编辑控制器内的文本区域,java,javafx,textarea,fxml,scenebuilder,Java,Javafx,Textarea,Fxml,Scenebuilder,我不熟悉Java和JavaFX。我已经使用SceneBuilder/FXML创建了一个JavaFX项目,我正在尝试在程序开始时添加一个实时时钟,它可以在屏幕顶部的整个程序中工作。我创建了一个文本区域,并尝试根据我们得到的代码添加时钟函数,但每次启动程序时,它总是显示为空白。即使尝试手动使用.setText(“string”)函数也不起作用,所以我认为我把代码放错地方了。如果可能的话,有人能告诉我这个代码应该去哪里,或者给我指出正确的方向吗 以下是我的主要观点: package applicati
package application;
import java.lang.ModuleLayer.Controller;
import java.util.Date;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
public class Main extends Application {
public static Stage stage = null;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/Ui.fxml"));
Scene scene = new Scene(root);
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
this.stage = stage;
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我的控制器代码:
package application;
import java.util.Date;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class UiController {
@FXML
private TextArea clockTextArea;
@FXML
private TextArea transactionLog;
@FXML
private TextField recipentField;
@FXML
private Button payButton;
@FXML
private Button requestButton;
@FXML
private TextField commentField;
private void refreshClock()
{
Thread refreshClock = new Thread()
{
public void run()
{
while (true)
{
Date dte = new Date();
String topMenuStr = " " + dte.toString();
clockTextArea.setText(topMenuStr);
try
{
sleep(3000L);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} // end while ( true )
} // end run thread
};
refreshClock.start();
}
public void initialize() {
TextArea clockTextArea = new TextArea();
refreshClock();
}
}
需要注意的是,无论是在Swing还是JavaFX中,当更新控件时,都需要在事件调度线程中执行,,否则它将无法工作或在FX应用程序线程上抛出异常
;currentThread=*
此时,您只需将更新控制代码的代码放入事件分派线程中即可完成。在JavaFX中使用以下代码
Platform.runLater(new Runnable() {
@Override
public void run() {
//Update the control code of JavaFX and put it here
}
});
因此,请更改您的代码:
Date dte = new Date();
String topMenuStr = " " + dte.toString();
Platform.runLater(() -> clockTextArea.setText(topMenuStr));
检查它是否工作。一种方法是使用时间线来处理时钟。它将独立于您的其他代码 Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("primary.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1080, 720);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
public static void main(String[] args) {
launch();
}
}
PrimaryController
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.util.Duration;
public class PrimaryController {
@FXML Label lblClock;
Timeline clockHandler;
@FXML
void initialize()
{
Locale locale_en_US = Locale.US ;
DateTimeFormatter formatterUS = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale_en_US ) ;
clockHandler = new Timeline(
new KeyFrame(Duration.seconds(1), (ActionEvent event) -> {
LocalTime currentTime = LocalTime.now();
String outputUs = currentTime.format(formatterUS);
lblClock.setText(outputUs);
}));
clockHandler.setCycleCount(Timeline.INDEFINITE);
clockHandler.play();
}
}
主FXML
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="TOP_CENTER" prefHeight="720.0" prefWidth="1080.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sed.home.javafxrealtimeclock.PrimaryController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<HBox>
<children>
<Label maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS" />
<Label fx:id="lblClock" text="00:00">
<HBox.margin>
<Insets />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="5.0" right="5.0" />
</VBox.margin>
</HBox>
<AnchorPane prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<Label layoutX="451.0" layoutY="331.0" text="The other parts of the program here" />
</children>
</AnchorPane>
</children>
</VBox>
您无法从后台线程更新UI。嗯。。。这看起来非常类似(包括我评论的错误)-您是否创建了一个新帐户来重新发布?如果是这样的话,就不要——而是编辑上一篇文章,让它有责任感。至少,修复已经发现的错误。。顺便说一句:创建一个本地文本字段并且不以任何方式使用它(在初始化中)是。。没用。