- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 Internet 检查器类,它将检查与某个 url 的连接并相应地更新状态属性。为了避免 ui 卡住,我想使用一个线程和一个计时器在一定时间间隔后重新检查。问题是时间轴关键帧中的 CHECK 方法调用仍然是从 FX 线程调用的。我如何在线程中使用时间轴?
代码:
public class InternetChecker {
private String baseUrl;
/***Properties***/
private ObjectProperty<Status> status = new SimpleObjectProperty<>(Status.ACTIVE);
/****************************************************************
********** CONSTRUCTORS ************
****************************************************************/
public InternetChecker(String baseUrl) {
this(baseUrl, 1000);
}
public InternetChecker(String baseUrl, int millisCheckInterval) {
this.baseUrl = baseUrl;
new Thread(() -> {
Timeline timelineCheck = new Timeline();
timelineCheck.getKeyFrames().add(
new KeyFrame(Duration.millis(millisCheckInterval), e -> {
check();
}));
timelineCheck.setCycleCount(Animation.INDEFINITE);
timelineCheck.play();
}).start();
}
/*******************************
* Will check if there is an internet connection present
* and update the status accordingly
*******************************/
public void check() {
// Check if base internet connection
// is working, if it is we continue
// to see if domain connection is working
try {
if ("127.0.0.1".equals(InetAddress.getLocalHost().getHostAddress())) {
setStatus(Status.INTERNET_DISCONNECTED);
return;
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
// Check if base domain connection is working
try {
final URL url = new URL(baseUrl);
final URLConnection conn = url.openConnection();
conn.connect();
conn.getInputStream().close();
setStatus(Status.ACTIVE);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
setStatus(Status.BASE_URL_UNREACHABLE);
}
}
/****************************************************************
********** ACCESSORS ************
****************************************************************/
public Status getStatus() {
return status.get();
}
public ObjectProperty<Status> statusProperty() {
return status;
}
private void setStatus(Status status) {
this.status.set(status);
}
/*******************************
* ACTIVE (Base url reachable)
* BASE_URL_UNREACHABLE (Internet available, but base url is unreachable)
* INTERNET_DISCONNECTED (Internet is not available)
********************************/
public enum Status {
ACTIVE,
BASE_URL_UNREACHABLE,
INTERNET_DISCONNECTED;
}
}
最佳答案
由于您需要定期执行与JavaFX 应用程序线程 通信的后台 任务,因此最好使用ScheduledService
。 .此类执行(新)Task
定期使用可由开发人员定义的 Executor
。请注意,ScheduledService
扩展了 javafx.concurrent.Service
。
以下是您需要执行的操作的框架示例:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
public class ConnectionStatusService extends ScheduledService<Status> {
// Property allows you to change the "baseUrl" between executions
private final StringProperty baseUrl = new SimpleStringProperty(this, "baseUrl");
// property getter and setters omitted...
@Override
protected Task<Status> createTask() {
// creates a new Task and gives the current "baseUrl"
// as an argument. This is called every cycle
return new ConnectionStatusTask(getBaseUrl());
}
private static class ConnectionStatusTask extends Task<Status> {
// A Task is a one-shot thing and its initial state should be
// immutable (or at least encapsulated from external modification).
private final String baseUrl;
private ConnectionStatusTask(String baseUrl) {
this.baseUrl = baseUrl;
}
@Override
protected Status call() throws Exception {
// Do what you need to determine connection status
return computedStatus;
}
}
}
然后你会监听/绑定(bind)到 lastValue
属性(property)。
public void initService() {
ConnectionStatusService service = new ConnectionStatusService();
service.setBaseUrl(/* your URL */);
service.setPeriod(Duration.seconds(1)); // run every 1 seconds
service.lastValueProperty().addListener(/* your listener */); // or bind to this property
// you may also want to add EventHandlers/Listeners to handle when the
// service fails and such.
service.start();
}
观察 lastValue
属性而不是 value
属性很重要。 lastValue
的Javadoc中给出了原因:
The last successfully computed value. During each iteration, the "value" of the ScheduledService will be reset to null, as with any other Service. The "lastValue" however will be set to the most recently successfully computed value, even across iterations. It is reset however whenever you manually call reset or restart.
我建议阅读 Task
、Service
和 ScheduledService
的 Javadoc 以获得更多信息。所有这三个类都实现了 javafx.concurrent.Worker
接口(interface)。
关于java - 在线程中使用 Javafx TimeLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51109833/
我正在尝试实现一个时间轴,但是当我点击其中一个不同的状态时我无法更改颜色 :( 有谁知道我的问题是什么或者我该怎么做? 本来的想法是给不同的状态分配不同的颜色,但是没有成功:( 在图片中我留下了一个我
我在我的应用程序中使用时间轴 JS 来根据日期显示两种类型的报告。它工作正常,但是当报告数据数量较多时,很难找到哪个报告属于哪个类别。所以我想在timemarker中使用不同的文本颜色来区分两种类型。
我有一个由客户端软件调用的 WPF 应用程序。它第一次工作正常。当我从客户端软件关闭 WPF 应用程序并再次加载 WPF 应用程序(中间没有关闭客户端软件))时,它抛出一个异常,因为“属性元数据已经为
我需要可视化时间线内的大量数据。我发现'verite Timeline '看起来很有希望,但我没有任何经验,使用了很多数据集。有人可以告诉我这个工具的性能或告诉我更好的解决方案吗?谢谢! 最佳答案 正
我有一个数据框,其中包含不同团队执行的任务的信息。 我想使用 R.Blue box = team 绘制类似的图,如下所示。任务已完成 = 绿框。执行任务=灰框。我正在考虑使用 ggplot2 geom
我对任何可以执行以下操作的库感兴趣: 我有一个时间范围:假设 1995 年到 2010 年,我可以以某种方式对其进行分类,假设为“互联网时代”。 然后我希望能够将其进一步分解,将 2001-2010
我有一个谷歌时间轴,如下面的代码所示,我想更改任何标题为“黑色”的条目的颜色。我似乎无法让它工作,我尝试了几种不同的解决方案。这是我的代码: http://jsfiddle.net/5j0umkcx/
我正在尝试创建一个 Internet 检查器类,它将检查与某个 url 的连接并相应地更新状态属性。为了避免 ui 卡住,我想使用一个线程和一个计时器在一定时间间隔后重新检查。问题是时间轴关键帧中的
我目前正在构建一个基于网络的迷你社交网络。与 Facebook 一样,每个用户都有一个发布最近事件的时间表。这些事件包括状态更新、在页面上发帖、添加新 friend 和喜欢页面。 它们在数据库中使用这
我创建了一个时间表,有时连续有两个时间表。这不是问题,但我想在一行中重叠显示它们。 请看这个例子: 因此,较小的行应显示为较大行的一部分。 Google 的可视化 API 很棒,是一件非常好的东西,但
我有一个包含很多行的时间表,每行中都有几个时间表。 我想为每个时间表固定一个确切的颜色,这样一种类型的时间表在每一行中都会有相同的颜色。 我正在使用这段代码: var options = {
我将如何存储用户“帖子”?这样我就可以有效地从数据库中获取它们,然后在请求特定用户页面时处理它们以按时间顺序显示? 我是否像这样将所有用户的所有帖子存储在一个表中: Post ID | Poster
我需要将 Pane 的背景颜色设置一秒钟,然后将其切换为透明。我已将其设置为更改背景颜色,使用持续时间为 1000 毫秒的时间线暂停它,然后切换为透明。 时间轴没有暂停,程序飞过它并且过快地将背景设置
编辑:Java/FX 版本:13.0.2 集成环境:Eclipse 4.13.0 Tracker 和 changePerFrame 值在类级别上声明为私有(private) double 变量。 目标
我正在尝试找到合适的 css 类来为 vis timeline 的组标签分配新的背景颜色。这是我的 jsfiddle 示例。 I want to give the Label in front of
我正在 Timeline lite 中对多个补间进行排序,但我希望其中几个补间同时发生在不同的对象上。如果没有 onComplete 函数,有没有办法做到这一点。我当前的补间序列是: tl.to($s
我正在构建一个视差滚动网站(我们不都是这样),除其他外,它会在用户滚动时显示图像。 我通过将图像放在背景中并在顶部放置一个实心填充的 div 来完成“显示”。然后我根据滚动位置将此 div 从 100
我的问题是关于 Chrome DevTools,具体来说,我有关于时间轴选项卡的问题。因此,正如我读过无数次,我的浏览器必须以 60fps 的速度渲染我的像素。有时虽然它有一些繁重的 JS 执行并阻止
说明: 在pg中,当我们进行了基于时间点的还原(PITR)后,数据库会启用新的时间线并继续进行操作。 但是,当我们进行基于时间点的还原后如果发现又出现错误,想要继续还原数据库该如何操作呢?如何还
我在 Vis JS Timeline 中使用以下选项在时间线顶部生成带有时间标签的水平轴: orientation: { axis: 'both' }, 水平轴如下所示: 我的时间线有很多行,
我是一名优秀的程序员,十分优秀!