Trending September 2023 # Complete Guide To Javafx Background # Suggested October 2023 # Top 11 Popular | Nhahang12h.com

Trending September 2023 # Complete Guide To Javafx Background # Suggested October 2023 # Top 11 Popular

You are reading the article Complete Guide To Javafx Background updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Complete Guide To Javafx Background

Definition of JavaFX Background

Web development, programming languages, Software testing & others

Syntax:

Background bg = new Background(background_fill);

Here, bg is the object of background class.

Constructors

Let us see the constructors of the background class in JavaFX.

Background(BackgroundFill… f): A new background object will be created with fills mentioned.

Background(BackgroundFill[] f, BackgroundImage[] im): A new background object will be created with fills as well as the background images mentioned.

Background(BackgroundImage… i): A new background object will be created with the background image mentioned.

Background(List f, List im): A new background object will be created with a list of fills as well as a list of background images mentioned.

Methods

Following are the commonly used methods of background class in JavaFX.

getFills(): A list of all background fills is returned.

getImages(): A list of all background images is returned.

getOutsets(): A list of all background outset is returned.

isEmpty(): This method checks whether the background is empty or not and returns the same.

isFillPercentageBased(): This method checks whether the background fill is based on percentages or not and returns the same.

Examples of JavaFX Background

Now, it is time to see some sample programs on background class in JavaFX.

Example #1

Code:

import javafx.application.Application; import javafx.scene.Scene; 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.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.paint.*; public class BackgroundClassProgram extends Application { public void start(Stage st) { try { st.setTitle("Background creation. . .") ; Label lb = new Label("user name : ") ; TextField tf = new TextField(); tf.setPrefColumnCount(10); Button bt = new Button("OK"); HBox hb = new HBox(lb, tf, bt); hb.setSpacing(10); hb.setAlignment(Pos.CENTER); Scene sc = new Scene(hb, 280, 280); BackgroundFill bf = new BackgroundFill(Color.RED, CornerRadii.EMPTY , Insets.EMPTY); Background bg = new Background(bf); hb.setBackground(bg); st.setScene(sc); st.show(); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void main(String args[]) { launch(args); } }

In this program, a button, label, and text field are created. As a background, red color is given and on executing the code, it gets displayed as shown above.

Example #2

Code:

import javafx.application.Application; import javafx.scene.Scene; 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.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.paint.*; public class BackgroundClassProgram extends Application { public void start(Stage st) { try { st.setTitle("Background creation. . .") ; Label lb = new Label("user name : ") ; TextField tf = new TextField(); tf.setPrefColumnCount(10); Button bt = new Button("OK"); HBox hb = new HBox(lb, tf, bt); hb.setSpacing(10); hb.setAlignment(Pos.CENTER); Scene sc = new Scene(hb, 280, 280); FileInputStream inp = new FileInputStream("d:\eduCBA-logo1.png"); Image im = new Image(inp); BackgroundImage bi = new BackgroundImage(im, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT); Background bg = new Background(bi); hb.setBackground(bg); st.setScene(sc); st.show(); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void main(String args[]) { launch(args); } }

Output:

Example #3

Code:

import javafx.application.Application; import javafx.scene.Scene; 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.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.scene.paint.*; import javafx.scene.shape.Circle; public class BackgroundClassProgram extends Application { public void start(Stage st) { try { st.setTitle("Background creation. . .") ; Label lb = new Label("user name : ") ; Circle c = new Circle(); c.setCenterX(310.0f); c.setCenterY(125.0f); c.setRadius(110.0f); HBox hb = new HBox(c); hb.setSpacing(10); hb.setAlignment(Pos.CENTER); Scene sc = new Scene(hb, 280, 280); BackgroundFill bf = new BackgroundFill(Color.RED, CornerRadii.EMPTY , Insets.EMPTY); Background bg = new Background(bf); hb.setBackground(bg); st.setScene(sc); st.show(); } catch (Exception ec) { System.out.println(ec.getMessage()); } } public static void main(String args[]) { launch(args); } } Output:

Unlike the above programs, a circle gets displayed on executing the code with a red background color.

Conclusion

The background is a class that helps in setting the background of a selected region. In this article, different aspects such as syntax, constructors, methods, and examples of JavaFX background class is explained in detail.

Recommended Articles

This is a guide to JavaFX Background. Here we also discuss the Definition, Constructors, methods, and along with different examples. You may also have a look at the following articles to learn more –

You're reading Complete Guide To Javafx Background

Update the detailed information about Complete Guide To Javafx Background on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!