如何使用 JavaFX 中的 Scene Builder 将背景图像添加到 AnchorPane?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38565178/
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-08-11 20:25:20  来源:igfitidea点击:

How to add background-image to AnchorPane by using Scene Builder in JavaFX?

javajavafxfxml

提问by pen1993

How add background image to an AnchorPaneby using Scene Builder?

如何AnchorPane使用 Scene Builder添加背景图像?

I've tried it as:

我试过了:

-fx-background-image url('C:/Users/Documents/page_background.gif')

How I set this in Scene Builder.

我如何在Scene Builder 中设置它。

And the generated FXML:

和生成的 FXML:

<AnchorPane id="LoginAnchorPane" fx:id="LoginAnchorPane" prefHeight="400.0" prefWidth="600.0" style="-fx-background-image: url('C:/Users/Documents/page_background.gif');" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafx_lsdu.LoginController">

采纳答案by DVarga

You can try to set it directly in Scene Builder as:

您可以尝试直接在 Scene Builder 中将其设置为:

-fx-background-image: url('file:C:/Users/Documents/page_background.gif')

It requires the scheme/protocol to be specified.

它需要指定方案/协议。

But the suggested way, to separate CSS styling in a CSS file. For instance you can create a CSS style-class in your CSS file (let's call it "application.css"):

但是建议的方法是在 CSS 文件中分离 CSS 样式。例如,您可以在您的 CSS 文件中创建一个 CSS 样式类(我们称之为“application.css”):

application.css

应用程序.css

.anchor {
    -fx-background-image:url('file:/C:/Users/Documents/page_background.gif');
}

Then in the FXML file you add this stylesheet to the root and you add the anchorstyle-class to the AnchorPane:

然后在 FXML 文件中,将此样式表添加到根目录,并将anchor样式类添加到AnchorPane

<AnchorPane prefHeight="545.0" prefWidth="712.0" styleClass="anchor" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60">
  <stylesheets>
    <URL value="@application.css" />
  </stylesheets>
</AnchorPane>

Note: Stylesheets should be added to the root node (in the example the AnchorPaneis the root).

注意:样式表应该添加到根节点(在示例中AnchorPane是根)。

回答by dirk

I'm new to JavaFX, but I added a background image to my AnchorPane without any coding whatsoever. Simply drag and drop the image. That is what Scene Builder was designed for, not so? I think it's the best thing since sliced bread.

我是 JavaFX 的新手,但我在 AnchorPane 中添加了一个背景图像,而没有进行任何编码。只需拖放图像。这就是 Scene Builder 的设计目的,不是这样吗?我认为这是自切片面包以来最好的东西。

1) I edited my background picture with Photoshop, to get the size the same as my AnchorPane, 800 x 600 pixels. I also created a new blank file in Photoshop, same size. File, New, Blank File. Then I copied my background picture and pasted it on top of the blank file, to enable me to set Opacity 50% (transparency). I like that, it makes my background picture “soft”.

1) 我用 Photoshop 编辑了我的背景图片,以获得与我的 AnchorPane 相同的大小,800 x 600 像素。我还在 Photoshop 中创建了一个新的空白文件,大小相同。文件,新建,空白文件。然后我复制了我的背景图片并将其粘贴到空白文件的顶部,以使我能够将不透明度设置为 50%(透明度)。我喜欢这样,它使我的背景图片“柔和”。

2) I copied my 50% Opacity (50% transparent) background picture into the src (source) folder of my NetBeans project. The src folder is an ordinary Windows Explorer folder.

2) 我将我的 50% 不透明度(50% 透明)背景图片复制到我的 NetBeans 项目的 src(源)文件夹中。src 文件夹是一个普通的 Windows 资源管理器文件夹。

3) Drag and drop the background jpg image from the src folder into Scene Builder as an ImageView, onto the AnchorPane icon (node) which is in Document, Hierarchy. Left hand side of Scene Builder. If the ImageView drops somewhere else, drag it up to where it belongs, you want it in the AnchorPane, that's the background.

3) 将背景 jpg 图像从 src 文件夹中作为 ImageView 拖放到 Scene Builder 中,拖放到 Document、Hierarchy 中的 AnchorPane 图标(节点)上。场景生成器的左侧。如果 ImageView 放在其他地方,请将其拖到它所属的位置,您希望它位于 AnchorPane 中,这就是背景。

4) With your background image ImageView selected (highlighted) fix the settings on the right hand panel of Scene Builder, Inspector, Layout:ImageView. Set the Anchor Pane Constraints (the spider web thing) left and top, both to 0. And fix the Size, Fit Width 800, Fit Height 600.

4) 选择(突出显示)背景图像 ImageView 后,修复 Scene Builder、Inspector、Layout:ImageView 右侧面板上的设置。将 Anchor Pane Constraints (the Spider web thing) left 和 top 都设置为 0。 并修复 Size、Fit Width 800、Fit Height 600。

As easy as pie. There is no need for coding, Scene Builder automatically writes the code for you. There is also no need for a css file. What a pleasure, you can see what you're doing. Wysiwyg, what you see is what you get.

像馅饼一样简单。无需编码,Scene Builder 会自动为您编写代码。也不需要 css 文件。多么高兴,你可以看到你在做什么。所见即所得,所见即所得。

回答by Abdullah Riaz

You need to make a new CSS file and put code given below in it write your image name in url.

您需要创建一个新的 CSS 文件并将下面给出的代码放入其中,在 url 中写入您的图像名称。

.bodybg{
     -fx-background-image: url('**your image name**.jpg');
     -fx-background-size: 100% 100%;    
  }

after this you have to select your anchor pane go to their properties and select CSS Style sheet and select Stle class. After this your background will change.

在此之后,您必须选择您的锚点窗格,转到其属性并选择 CSS 样式表并选择 Stle 类。在此之后,您的背景将发生变化。

I have found a complete tutorial explaining all point in detail Change your Application Background in Scene Builder

我找到了一个完整的教程,详细解释了所有要点在场景生成器中更改您的应用程序背景