Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX-覆盖边框窗格的居中背景图像_Java_Css_Javafx_Background Image_Borderpane - Fatal编程技术网

JavaFX-覆盖边框窗格的居中背景图像

JavaFX-覆盖边框窗格的居中背景图像,java,css,javafx,background-image,borderpane,Java,Css,Javafx,Background Image,Borderpane,我正在尝试在JavaFX中设置为()的背景。我希望它能盖上,并且总是放在中间。我想在CSS(用于web)中使用相同的方法 我已经试过: 使用CSS(FXCSS) 此代码使用外部CSS(您可以找到我的CSS代码),或者您可以将以下代码与内联CSS一起使用: package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import

我正在尝试在JavaFX中设置为()的背景。我希望它能盖上,并且总是放在中间。我想在CSS(用于web)中使用相同的方法

我已经试过:

  • 使用CSS(FXCSS)

    此代码使用外部CSS(您可以找到我的CSS代码),或者您可以将以下代码与内联CSS一起使用:

    package application;
    
    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    
    public class InlineCSS extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
    
                //BorderPane()
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
                BorderPane borderPane = new BorderPane();
    
    
                //Button(String text)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
                Button btn = new Button("Center");
    
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
                borderPane.setCenter(btn);
    
    
                Scene scene = new Scene(borderPane, 600, 600);
    
                URL path = getClass().getResource("resources/black_clock.png");
    
                String image = null;
                if(path != null) {
                    image = path.toExternalForm();
                } else {
                    image = "https://raw.githubusercontent.com/4nds/CenteredBackgroundImage/master/Background/src/application/resources/black_clock.png";
                }                   
    
                borderPane.setStyle("-fx-background-image: url('" + image + "'); " +
                           "-fx-background-position: center center; " +
                           "-fx-background-repeat: no-repeat;" +
                           "-fx-background-size: cover");
    
                primaryStage.setScene(scene);
                primaryStage.show();
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
  • 仅使用JavaFX:

    package application;
    
    
    import java.io.File;
    
    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.image.Image;
    import javafx.scene.layout.Background;
    import javafx.scene.layout.BackgroundImage;
    import javafx.scene.layout.BackgroundPosition;
    import javafx.scene.layout.BackgroundRepeat;
    import javafx.scene.layout.BackgroundSize;
    import javafx.scene.layout.BorderPane;
    
    
            public class OnlyJavaFX extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
    
                //BorderPane()
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
                BorderPane borderPane = new BorderPane();
    
    
                //File(String pathname)
                //https://docs.oracle.com/javase/8/docs/api/java/io/File.html
                File file = new File("./src/application/resourcesa/black_clock.png");
    
                Image img = null;
                if(file.exists()) {
                    //Image(InputStream is)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image(file.getAbsoluteFile().toURI().toString());
                } else {
                    //Image(String url)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image("https://raw.githubusercontent.com/4nds/CenteredBackgroundImage/master/Background/src/application/resources/black_clock.png");
                }
    
                //BackgroundSize(double width, double height, boolean widthAsPercentage, boolean heightAsPercentage, boolean contain, boolean cover)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BackgroundSize.html
                BackgroundSize bgSize = new BackgroundSize(0, 0, false, false, false, true);
    
                //public BackgroundImage(Image image, BackgroundRepeat repeatX, BackgroundRepeat repeatY, BackgroundPosition position, BackgroundSize size)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BackgroundImage.html
                BackgroundImage bgImg = new BackgroundImage(img,
                        BackgroundRepeat.NO_REPEAT,
                        BackgroundRepeat.NO_REPEAT,
                        BackgroundPosition.CENTER,
                        bgSize);
    
                //Background(BackgroundImage... images)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Background.html
                Background bg = new Background(bgImg);
    
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#setBackground-javafx.scene.layout.Background-
                borderPane.setBackground(bg);
    
    
                //Button(String text)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
                Button btn = new Button("Center");
    
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
                borderPane.setCenter(btn);
    
    
                Scene scene = new Scene(borderPane, 600, 600);
                primaryStage.setScene(scene);
                primaryStage.show();
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
  • 当前所有版本的代码(、和)都会产生如下结果:

    下面是同一示例的CSS和HTML代码。这就是我希望它看起来的样子:

    • 例如:

    • 例如:

    编辑1:

    我意识到我已经发布了我的代码的旧版本,所以我会更新它。 (代码中唯一的变化是,现在即使没有保存在计算机上的图像和CSS文件,它也可以工作,而程序将使其联机。)

    编辑2后:

    我已经测试了你的代码,但它不起作用。(我甚至添加了背景图像视图在Y轴方向上的平移,在您的回答中,它只是在X轴方向上,而且我还设置了borderPane的大小以匹配场景(应用程序窗口)的大小,但它仍然不起作用)。以下是完整的代码(与您的答案相同,但我已经提到了一些小的修复):

    问题是,此代码仅在启动程序时有效

    一旦你调整(应用程序)窗口的大小,它就不再居中了


    我可以通过不使用BackGroundImage来实现这一点,而只需在另一个区域中包含背景图像视图。然后平移背景图像视图,使其居中

    我调整了你的代码来做到这一点。我使用组作为场景的根节点。在这个根目录中,我放置了背景的ImageView和BorderPane

            //BorderPane()
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
            BorderPane borderPane = new BorderPane();
    
            //jot down the width and height of the scene
            double width = 600;
            double height = 600;
    
            //Image(InputStream is)
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
            Image img = new Image(file.getAbsoluteFile().toURI().toString());
            ImageView background = new ImageView(img);
            //..center the background
            double translateX = width/2 - img.getWidth()/2;
            background.setTranslateX(translateX);
    
            //Button(String text)
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
            Button btn = new Button("Center");
    
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
            borderPane.setCenter(btn);
    
            //make root with BorderPane and background image
            Group root = new Group (background, borderPane);
    
            Scene scene = new Scene(root, width, height);
    
    编辑:处理调整大小

    1) 将背景图像视图移动为对象字段。 2) 创建背景图像视图居中的方法。 3) 每当调整窗口大小时,创建回调以使背景图像视图居中

    public class Try1 extends Application {
    
        private ImageView background = new ImageView(img);
    
        private final ChangeListener<Number> windowWidthResized = (ObservableValue<? extends Number> ov, Number t, Number t1) -> {
        windowResized();
    };
    
        private final ChangeListener<Number> windowHeightResized = (ObservableValue<? extends Number> ov, Number t, Number t1) -> {
        windowResized();
    };
    
        private void windowResized(){
            double newHeight = scene.getHeight();
            double newWidth = scene.getWidth();
            centerBackgroundImage(newWidth, newHeight);
        }
    
        private void centerBackgroundImage(double width, double height) {
                double translateX = width/2 - img.getWidth()/2;
                System.out.println(translateX);
                background.setTranslateX(translateX);
    
                double translateY = height/2 - img.getHeight()/2;
                System.out.println(translateY);
                background.setTranslateY(translateY);
        }
    
        @Override
        public void start(Stage primaryStage) {
            try {
    
    
                //BorderPane()
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
                BorderPane borderPane = new BorderPane();
    
                //jot down the width and height of the scene
                double width = 600;
                double height = 600;
    
                //File(String pathname)
                //https://docs.oracle.com/javase/8/docs/api/java/io/File.html
                File file = new File("./src/application/resourcesa/black_clock.png");
    
                Image img = null;
                if(file.exists()) {
                    //Image(InputStream is)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image(file.getAbsoluteFile().toURI().toString());
                } else {
                    //Image(String url)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image("https://raw.githubusercontent.com/4nds/CenteredBackgroundImage/master/Background/src/application/resources/black_clock.png");
                }
    
    
                background.setImage(img);
    
                //..center the background
                centerBackgroundImage(width, height);
    
                //Button(String text)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
                Button btn = new Button("Center");
    
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
                borderPane.setCenter(btn);
    
                //make root with BorderPane and background image
                Group root = new Group (background, borderPane);
    
                Scene scene = new Scene(root, width, height);
    
                //add callbacks to handle window resize
                scene.heightProperty().addListener(windowResized);
                scene.widthProperty().addListener(windowWidthResized);        
    
                primaryStage.setScene(scene);
    
                //this lines set borderPane's dimensions to be the same as scene
                borderPane.prefHeightProperty().bind(scene.heightProperty());
                borderPane.prefWidthProperty().bind(scene.widthProperty());
    
                primaryStage.show();
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    public类Try1扩展应用程序{
    私有ImageView背景=新ImageView(img);
    
    私有最终ChangeListener windowWidthResized=(ObservalEvalue我已经尝试了你的代码,但它没有达到我想要的效果。请参阅我问题中的编辑。调整窗口大小时,你必须重新计算。将居中代码移到自己的方法中。然后在调整大小时添加屏幕回调。在该回调中,刷新背景图像视图上的翻译。这些行
     scene.heightProperty().addListener(windowHeightResized);
    scene.widthProperty().addListener(windowWidthResized);
    不起作用,它可能与函数
    windowResized()有关
    。我想自己修复它,但我不明白你想用那个部件做什么。哎呀,复制粘贴问题。添加了缺少的代码行。这仍然不起作用。问题是,当你调用Listener时,你只设置了
    新高度
    新宽度
    ,但你不使用它们。更新了我的答案以处理窗口大小泽。
            //BorderPane()
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
            BorderPane borderPane = new BorderPane();
    
            //jot down the width and height of the scene
            double width = 600;
            double height = 600;
    
            //Image(InputStream is)
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
            Image img = new Image(file.getAbsoluteFile().toURI().toString());
            ImageView background = new ImageView(img);
            //..center the background
            double translateX = width/2 - img.getWidth()/2;
            background.setTranslateX(translateX);
    
            //Button(String text)
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
            Button btn = new Button("Center");
    
            //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
            borderPane.setCenter(btn);
    
            //make root with BorderPane and background image
            Group root = new Group (background, borderPane);
    
            Scene scene = new Scene(root, width, height);
    
    public class Try1 extends Application {
    
        private ImageView background = new ImageView(img);
    
        private final ChangeListener<Number> windowWidthResized = (ObservableValue<? extends Number> ov, Number t, Number t1) -> {
        windowResized();
    };
    
        private final ChangeListener<Number> windowHeightResized = (ObservableValue<? extends Number> ov, Number t, Number t1) -> {
        windowResized();
    };
    
        private void windowResized(){
            double newHeight = scene.getHeight();
            double newWidth = scene.getWidth();
            centerBackgroundImage(newWidth, newHeight);
        }
    
        private void centerBackgroundImage(double width, double height) {
                double translateX = width/2 - img.getWidth()/2;
                System.out.println(translateX);
                background.setTranslateX(translateX);
    
                double translateY = height/2 - img.getHeight()/2;
                System.out.println(translateY);
                background.setTranslateY(translateY);
        }
    
        @Override
        public void start(Stage primaryStage) {
            try {
    
    
                //BorderPane()
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
                BorderPane borderPane = new BorderPane();
    
                //jot down the width and height of the scene
                double width = 600;
                double height = 600;
    
                //File(String pathname)
                //https://docs.oracle.com/javase/8/docs/api/java/io/File.html
                File file = new File("./src/application/resourcesa/black_clock.png");
    
                Image img = null;
                if(file.exists()) {
                    //Image(InputStream is)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image(file.getAbsoluteFile().toURI().toString());
                } else {
                    //Image(String url)
                    //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
                    img = new Image("https://raw.githubusercontent.com/4nds/CenteredBackgroundImage/master/Background/src/application/resources/black_clock.png");
                }
    
    
                background.setImage(img);
    
                //..center the background
                centerBackgroundImage(width, height);
    
                //Button(String text)
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html
                Button btn = new Button("Center");
    
                //https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html#setCenter-javafx.scene.Node-
                borderPane.setCenter(btn);
    
                //make root with BorderPane and background image
                Group root = new Group (background, borderPane);
    
                Scene scene = new Scene(root, width, height);
    
                //add callbacks to handle window resize
                scene.heightProperty().addListener(windowResized);
                scene.widthProperty().addListener(windowWidthResized);        
    
                primaryStage.setScene(scene);
    
                //this lines set borderPane's dimensions to be the same as scene
                borderPane.prefHeightProperty().bind(scene.heightProperty());
                borderPane.prefWidthProperty().bind(scene.widthProperty());
    
                primaryStage.show();
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }