我希望<imageview>浮动在<button>的右侧

时间:2015-05-20 10:09:42

标签: javafx javafx-2

我试图添加图片,但是它添加了按钮文本而不是极右位​​置。即我想将图像漂浮在右侧。

final ImageView imageView = new ImageView(
  new Image("Button.png")
);
final Button button = new Button("button", imageView);
button.setStyle("-fx-background-color: white; -fx-border-color: grey; -fx-border-radius: 5;");
button.setContentDisplay(ContentDisplay.RIGHT);

更新:这是我需要的图片链接以及我当前需要改进的按钮设计。 http://imgur.com/jrO0LGD,jGRcxl5

1 个答案:

答案 0 :(得分:2)

您需要在此处将自定义布局设置为按钮的图形。使用StackPane是我选择浮动布局(如css):

@Override
public void start( final Stage primaryStage )
{
    Text text1 = new Text( "back to home" );
    ImageView imageView1 = new ImageView( new Image( "pkg/images1.jpg" ) );
    StackPane stackPane1 = new StackPane( text1, imageView1 );
    StackPane.setAlignment( text1, Pos.CENTER_LEFT );
    StackPane.setAlignment( imageView1, Pos.CENTER_RIGHT );
    final Button button1 = new Button( "", stackPane1 );
    button1.setStyle( "-fx-background-color: white; -fx-border-color: grey; -fx-border-radius: 5;" );
    button1.setMinWidth( 400 );

    Text text2 = new Text( "holiday" );
    ImageView imageView2 = new ImageView( new Image( "pkg/images2.jpg" ) );
    StackPane stackPane2 = new StackPane( text2, imageView2 );
    StackPane.setAlignment( text2, Pos.CENTER_LEFT );
    StackPane.setAlignment( imageView2, Pos.CENTER_RIGHT );
    final Button button2 = new Button( "", stackPane2 );
    button2.setStyle( "-fx-background-color: white; -fx-border-color: grey; -fx-border-radius: 5;" );
    button2.setMinWidth( 400 );

    final Scene scene = new Scene( new VBox( button1, button2 ) );
    primaryStage.setScene( scene );
    primaryStage.show();
}