在JavaFX 8中的时间轴外部设置和使用变量 [英] Set and use variables outside timeline in javafx 8

查看:59
本文介绍了在JavaFX 8中的时间轴外部设置和使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Javafx应用程序以查看系统剪贴板:

I am trying to writing a javafx app to watch the system clipboard:

package sample;

import javafx.event.ActionEvent;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.util.Duration;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.show();

        final Clipboard clipboard = Clipboard.getSystemClipboard();
        final String oldString = "";
        final String newString = "";
        Timeline repeatTask = new Timeline(new KeyFrame(Duration.millis(200), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                if (clipboard.hasString()) {
                    newString = clipboard.getString();
                    if(oldString != newString) {
                        System.out.printf("String changed in clipboard: " + newString);
                        oldString = newString;
                    }
                    else {
                        System.out.println("String not changed.");
                    }
                }
            }
        }));
        repeatTask.setCycleCount(Timeline.INDEFINITE);
        repeatTask.play();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

问题是我需要将剪贴板的旧内容存储在时间轴对象之外,并且需要对其进行更改,但是一旦将它们设置为final(如java所要求的那样),就无法再进行修改了. /p>

The problem is I need to store the old content of the clipboard outside the timeline object, and I need to make changes to them, but once they set as final -- as java requires -- modification is no longer possible.

推荐答案

您可以将oldString作为Application类的字段:

You can have oldString as a field of your Application class:

private String oldString = "";

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.show();

    final Clipboard clipboard = Clipboard.getSystemClipboard();
    Timeline repeatTask = new Timeline(new KeyFrame(Duration.millis(200), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            if (clipboard.hasString()) {
                String newString = clipboard.getString();
                if(!oldString.equals(newString)) {
                    System.out.printf("String changed in clipboard: " + newString);
                    oldString = newString;
                }
                else {
                    System.out.println("String not changed.");
                }
            }
        }
    }));
    repeatTask.setCycleCount(Timeline.INDEFINITE);
    repeatTask.play();
}

之所以可行,是因为当您访问oldString时,实际上是在访问this.oldString,其中this是最终的.

This works because when you access oldString, you are actually accessing this.oldString, where this is final.

还请注意,要比较字符串,应使用oldString.equals(newString)而不是比较对象标识(==/!=).

Also note that to compare strings, you should use oldString.equals(newString) instead of comparing object identities (==/!=).

此外,如果您使用的是Java 8,则可以使用ReactFX摆脱Timeline样板:

Also, if you are using Java 8, you can get rid of the Timeline boilerplate using ReactFX:

private String oldString = "";

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.show();

    final Clipboard clipboard = Clipboard.getSystemClipboard();
    EventStreams.ticks(Duration.ofMillis(200)).subscribe(tick -> {
        if (clipboard.hasString()) {
            String newString = clipboard.getString();
            if(!oldString.equals(newString)) {
                System.out.printf("String changed in clipboard: " + newString);
                oldString = newString;
            }
            else {
                System.out.println("String not changed.");
            }
        }
    });
}

有关使用ReactFX中的计时器的更多信息,请参阅我的博客文章

For more information about using the timer from ReactFX, see my blog post http://tomasmikula.github.io/blog/2014/06/04/timers-in-javafx-and-reactfx.html

这篇关于在JavaFX 8中的时间轴外部设置和使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆