gpt4 book ai didi

JavaFx 文本字段未在 GUI 上更新

转载 作者:行者123 更新时间:2023-12-01 22:41:43 25 4
gpt4 key购买 nike

我正在尝试编写一个内存可视化工具,它将使用当前内存统计信息更新图表,并在其旁边有一个数字表示。我有一个在线程中运行的更新方法,应该每秒更新一次。

问题是它在 GUI 上都没有更新,事实上我相信代码在一次迭代后就会停止。我相信这与一些 setText() 调用有关,因为如果我将它们注释掉,那么至少图表会更新。

这是我的初始化方法

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
assert memUsage_lineChart != null : "fx:id=\"memUsage_lineChart\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_totalMem != null : "fx:id=\"text_totalMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_usedMem != null : "fx:id=\"text_usedMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_availableMem != null : "fx:id=\"text_availableMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_percentageUsed != null : "fx:id=\"text_percentageUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_totalRuntime != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_threadsUsed != null : "fx:id=\"text_threadsUsed\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_version != null : "fx:id=\"text_version\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_timeAtMaxMem != null : "fx:id=\"text_timeAtMaxMem\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_maxMemReached != null : "fx:id=\"text_maxMemReached\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_timeOfLastAssociation != null : "fx:id=\"text_timeOfLastAssociation\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert text_something != null : "fx:id=\"text_totalRuntime\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert lineChart_yAxis != null : "fx:id=\"lineChart_yAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";
assert lineChart_xAxis != null : "fx:id=\"lineChart_xAxis\" was not injected: check your FXML file 'MemoryVisualization.fxml'.";

runtime = 0L;


//Initializing the ArrayLists<> that will contain the series of data for each possible graph
memData = new ArrayList<XYChart.Series<Number,Number>>();

//Setting the Y-axis of the chart to change according to incoming data.
memUsage_lineChart.getYAxis().setAutoRanging(true);

//Initializes the data arrays and series for the charts
XYChart.Series<Number, Number> series_sec = new XYChart.Series<Number,Number>();
memUsage_lineChart.getData().add(series_sec);
memData.add(series_sec);

//Creates a thread that continuously runs and adds data points to the displays
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
runtime += 1000;
updateData();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 0, 500, TimeUnit.MILLISECONDS);

}/*END OF INSTANTIATION*/

这是我的 updateData() 方法

public void updateData()
{
//Gathers the runtime memory data from the system
long maxFreeMemory = Runtime.getRuntime().maxMemory();
long usableFreeMemory= Runtime.getRuntime().maxMemory()
-Runtime.getRuntime().totalMemory()
+Runtime.getRuntime().freeMemory();
double usedPercent=(double)(Runtime.getRuntime().totalMemory()
-Runtime.getRuntime().freeMemory())/Runtime.getRuntime().maxMemory();
long usedMemory = (long)(usedPercent*maxFreeMemory);
maxFreeMemory /= 1000000;
usableFreeMemory /= 1000000;
usedMemory /= 1000000;
usedPercent *= 100;
DecimalFormat df = new DecimalFormat("0.00");

//Set the text values for the runtime statistics
text_totalMem.textProperty().set(String.valueOf(maxFreeMemory));
text_usedMem.textProperty().set(String.valueOf(usedMemory));
text_availableMem.textProperty().set(String.valueOf(usableFreeMemory));
text_percentageUsed.textProperty().set(df.format(usedPercent)+" %");
text_totalRuntime.textProperty().set((runtime/3600000)+":"+String.format("%02d",((runtime/60000)%60))+":"+String.format("%02d",((runtime%60000)/1000)));
text_threadsUsed.textProperty().set(String.valueOf(ManagementFactory.getThreadMXBean().getThreadCount()));

//Updates the axes of the real-time graphs/charts
if(runtime >= 120000 && (runtime % 12000) == 0)
{
lineChart_xAxis.setLowerBound(lineChart_xAxis.getLowerBound()+12);
lineChart_xAxis.setUpperBound(lineChart_xAxis.getUpperBound()+12);

for(int i=0; i<12; i++)
memData.get(0).getData().remove(0);
}

//Adds the data to the series to be placed on to the graphs/charts
memData.get(0).getData().add(new XYChart.Data<Number,Number>(runtime/1000,usedMemory));
}

运行时我的 GUI 卡住并且无法更新文本字段/图形是否有原因?我知道线程至少运行一次迭代,因为弹出 GUI 时文本字段不为 0(初始化时)。

最佳答案

使用 Platform.runLater() 在 FX 应用程序线程上执行 updateData() 方法。 (此外,我认为 Thread.sleep(...) 是多余的,因为您使用的是预定执行程序。)

    ses.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
runtime += 1000;
Platform.runLater(() -> updateData());
}
}, 0, 500, TimeUnit.MILLISECONDS);

关于JavaFx 文本字段未在 GUI 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com