后台类是JavaFX的一部分。Background类设置区域的背景。每个背景都由几个填充或背景图像组成,但不能为空,但可以为空。Background类是不可变的,所以您可以在许多不同的区域上自由地重用相同的背景。
null
类的构造函数:
- 背景(背景填充…f) :创建具有指定填充的新背景对象。
- 背景(BackgroundFill[]填充,BackgroundImage[]图像) :创建具有指定填充和背景图像的新背景对象。
- 背景(背景图片…i) :使用指定的背景图像创建新的背景对象。
- 背景(列表填充、列表图像) :创建具有指定填充和背景图像列表的新背景对象。
常用方法:
方法 | 解释 |
---|---|
getFills() | 返回背景的所有填充的列表。 |
getImages() | 返回背景的所有背景图像的列表。 |
getOutsets() | 返回此背景之外的内容。 |
isEmpty() | 返回背景是否为空。 |
isFillPercentageBased() | 返回此背景的填充是否基于百分比。 |
下面的程序说明了后台类的使用:
- 设置容器背景填充的Java程序: 在这个程序中,我们将创建一个名为 出身背景 使用指定的背景填充,并将其添加到背景中。我们将创建一个名为 hbox ,一个名为 标签 ,文本字段名为 文本框 还有一个名为 按钮 .现在将标签、文本字段和按钮添加到HBox。我们将使用 挫折 作用现在将HBox的校准设置为 位置中心 还可以使用 设置间距() 方法我们将创建一个名为 场景 并将HBox添加到场景中。场景将使用 setScene() 作用我们会打电话给 show() 函数来显示结果。
// Java program to set a fill for the background
// of a container
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.geometry.*;
import
javafx.scene.Group;
import
javafx.scene.paint.*;
public
class
Background_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating Background"
);
// create a label
Label label =
new
Label(
"Name : "
);
// create a text field
TextField textfield =
new
TextField();
// set preferred column count
textfield.setPrefColumnCount(
10
);
// create a button
Button button =
new
Button(
"OK"
);
// add the label, text field and button
HBox hbox =
new
HBox(label, textfield, button);
// set spacing
hbox.setSpacing(
10
);
// set alignment for the HBox
hbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(hbox,
280
,
280
);
// create a background fill
BackgroundFill background_fill =
new
BackgroundFill(Color.PINK,
CornerRadii.EMPTY, Insets.EMPTY);
// create Background
Background background =
new
Background(background_fill);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- 将图像添加到容器背景的Java程序: 在这个程序中,我们将创建一个名为 出身背景 使用指定的BackgroundImage并将此图像添加到容器的背景中。使用FileInputStream导入图像,然后将文件转换为图像对象使用此图像对象创建背景图像。我们将创建一个名为 hbox ,一个名为 标签 ,文本字段名为 文本框 还有一个名为 按钮 .现在将标签、文本字段和按钮添加到HBox。设置背景 hbox 使用 挫折 作用将HBox的校准设置为 位置中心 还可以使用 设置间距() 方法我们将创建一个名为 场景 并将HBox添加到场景中。场景将使用 setScene() 作用最后打电话给 show() 方法来显示结果。
// Java program to add an image to
// the background of a container
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.geometry.*;
import
javafx.scene.Group;
public
class
Background_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating Background"
);
// create a label
Label label =
new
Label(
"Name : "
);
// create a text field
TextField textfield =
new
TextField();
// set preferred column count
textfield.setPrefColumnCount(
10
);
// create a button
Button button =
new
Button(
"OK"
);
// add the label, text field and button
HBox hbox =
new
HBox(label, textfield, button);
// set spacing
hbox.setSpacing(
10
);
// set alignment for the HBox
hbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(hbox,
280
,
280
);
// create a input stream
FileInputStream input =
new
FileInputStream(
"f:\gfg.png"
);
// create a image
Image image =
new
Image(input);
// create a background image
BackgroundImage backgroundimage =
new
BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
// create Background
Background background =
new
Background(backgroundimage);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Background.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END