java 如何使用 JavaFX 8 在图像上重叠按钮/文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27912628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:36:33  来源:igfitidea点击:

How to Overlap buttons/Text over an image with JavaFX 8?

javajavafx

提问by Tacent

I am having trouble placing buttons/text over an image I am using with JavaFX 8.

我在使用 JavaFX 8 的图像上放置按钮/文本时遇到问题。

I used an ImageViewer to place the image but I am unable to actually get the rest ON TOP of the image.

我使用 ImageViewer 放置图像,但实际上无法将其余部分放在图像的顶部。

I am using setTranslateX,Y to move the button around but it never overlaps.

我正在使用 setTranslateX,Y 来移动按钮,但它从不重叠。

Please tell me how I can solve this issue.

请告诉我如何解决这个问题。

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.text.Text;


public class Main extends Application{


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


    public void start(Stage stage) throws Exception {

        int x = 450;
        int y = 450;

        Image image = new Image("space.jpg",x,y, false, false);
        ImageView iv1 = new ImageView();
        iv1.setImage(image);
        iv1.setPreserveRatio(true);
        iv1.setFitHeight(x);
        iv1.setFitWidth(y);


        Text text = new Text("hello");
        text.setFont(Font.font ("Arial", 27));

        Button button = new Button("Hello");
        button.setTranslateX(10);
        button.setTranslateY(10);
        button.setContentDisplay(ContentDisplay.TOP);


        HBox root = new HBox();


        root.getChildren().add(iv1);
        root.getChildren().add(text);
        root.getChildren().add(button);


        Scene scene = new Scene(root,x,y);

        stage.setTitle("Space Blaster (New Game)");
        stage.setScene(scene);
        stage.show();

    }
}

回答by James_D

You can use a StackPaneto overlay two nodes. For example:

您可以使用 aStackPane来覆盖两个节点。例如:

Button button = new Button("Hello");
Text text = new Text("hello");

HBox hbox = new HBox();
hbox.getChildren().addAll(button, text); // button will be left of text

Image image = new Image("space.jpg",x,y, false, false);
ImageView iv1 = new ImageView();

StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(iv1, hbox); // hbox with button and text on top of image view

HBox root = new HBox();
root.getChildren().add(stackPane);

// etc

Another approach is just to put the controls (button and text) in a pane of some kind (e.g. HBox), and then to use CSS to set a background imageon the pane.

另一种方法是将控件(按钮和文本)放在某种窗格(例如HBox)中,然后使用 CSS在窗格上设置背景图像