2015-02-17 73 views
1

我有一个应用程序,有很多阶段,做各种不同的事情。我想知道是否可以更改整个应用程序的Cursor,而不必为所有场景进行更改。JavaFX更改所有阶段的光标

例如,如果用户做了长时间运行的任务,我想光标变为等待光标用于所有场景。当这个任务完成后,我希望光标变回正常光标。

我明白,要改变光标为特定的场景,你可以做

scene.setCursor(Cursor.WAIT); 

我宁可不要通过所有在我的应用程序的各个阶段的迭代,并改变光标的每一个。

我想知道,如果你可以在应用程序级别更改光标,而不是现场级。我没有发现任何网络上的任何暗示你可以。

回答

1

有在应用层面做到这一点(我知道的),没有直接的方法。但是,游标是一个属性,因此您可以将所有场景的游标绑定到单个值。

因此,像:

public class MyApp extends Application { 

    private final ObjectProperty<Cursor> cursor = new SimpleObjectProperty<>(Cursor.DEFAULT); 

    @Override 
    public void start(Stage primaryStage) { 
     Parent root = ... ; 
     // ... 

     someButton.setOnAction(event -> { 
      Parent stageRoot = ... ; 
      Stage anotherStage = new Stage(); 
      anotherStage.setScene(createScene(stageRoot, ..., ...)); 
      anotherStage.show(); 
     }); 

     primaryStage.setScene(createScene(root, width, height)); 
     primaryStage.show(); 

    } 

    private static Scene createScene(Parent root, double width, double height) { 
     Scene scene = new Scene(root, width, height); 
     scene.cursorProperty().bind(cursor); 
     return scene ; 
    } 
} 

现在,任何时候你做

cursor.set(Cursor.WAIT); 

通过createScene(...)方法将改变其光标创建任何场景。

显然光标属性和实用方法没有在应用程序的子类来定义;你可以把它们放在你的应用程序结构方便的地方。