我想在我的程序中有一个按钮或可点击的ImageView。单击时,我希望边框显示在imageview的形状中。此图像没有背景,但我找不到指定边框形状的方法。例如:
此图像没有背景,边框必须仅在图像周围,因此没有矩形或圆形。这可能吗?
答案 0 :(得分:5)
您可能更喜欢使用DropShadow
效果来显示边框:
@Override
public void start( final Stage primaryStage )
{
DropShadow ds = new DropShadow( 20, Color.AQUA );
ImageView imageView = new ImageView( "http://vignette3.wikia.nocookie.net/forgeofempires/images/b/b8/Castel_del_Monte.png" );
imageView.setOnMouseClicked( ( MouseEvent event ) ->
{
imageView.requestFocus();
} );
imageView.focusedProperty().addListener(( ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue ) ->
{
if ( newValue )
{
imageView.setEffect( ds );
}
else
{
imageView.setEffect( null );
}
});
final Scene scene = new Scene(
new VBox( imageView,
new Button( "When you focus on me, the imageview looses its shadow effect" ) ),
500, 200 );
primaryStage.setScene( scene );
primaryStage.show();
}
当单击图像视图时,我们请求对其进行聚焦,这会触发focusProperty
更改侦听器并设置效果,并且当图像视图失去焦点时(通过按TAB或单击下面的按钮)效果清零。