Javafx 我想<;ImageView>;浮动在<;按钮>;

Javafx 我想<;ImageView>;浮动在<;按钮>;,javafx,javafx-2,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-bo

我曾尝试添加图像,但它添加到按钮文本的右侧,而不是最右侧的位置。也就是说,我想在右边浮动图像

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);

更新:这是我需要的图像链接和我当前需要改进的按钮设计

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


上述方法不适用于javafx按钮。我想将其用作“开”和“关”按钮。图像将设置为按钮最右侧的绿色椭圆形,反之亦然,用于“关”条件。@Aman,那么您使用的是旧版本的JavaFX,即版本2。在该版本中,需要将填充添加到setStyle()。请参见编辑。但是,建议升级到版本8。对于“开/关”按钮,您可能会更倾向于使用
ToggleButton
。我设置了按钮的填充,静止图像就在按钮text@Uluk的旁边。但是图像和右边框之间没有间隙,对吗?您可能想看看问题的答案。
@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();
}