HOWTO · Java
Texto de visualización de JavaFX
Este tutorial demuestra cómo mostrar texto en JavaFX.
En esta página
El texto se puede crear y mostrar utilizando la clase JavaFX.scene.text.Text. Este tutorial demuestra cómo mostrar texto de una sola línea y de varias líneas en JavaFX.
Texto de visualización de JavaFX
El JavaFX.scene,text.Text se utiliza para crear y mostrar texto en JavaFX. Se puede crear un nodo de texto instanciando la clase Text y mostrándolo en la escena.
Sintaxis:
Text text = new Text(text);
Donde el text como parámetro es el valor del texto. Para establecer el valor de la posición x e y del texto, utilizamos los siguientes métodos:
text.setX(30);
text.setY(30);
Los métodos anteriores establecerán la posición del texto de acuerdo con las posiciones x e y dadas en los métodos. Siga los pasos a continuación para crear y mostrar texto en JavaFX:
- Cree una clase extendiendo la clase
Applicatione implementando el métodostart(). - Cree el texto instanciando la clase
Text. Luego establezca la posiciónxeyutilizando los métodossetX()ysetY(). - Crear una clase
group. - Crear un objeto de escena, instanciar la clase
scene, y pasar el objetogroupa lascene. - Agregue un título al escenario con el método
setTitley agregue la escena al escenario con el métodosetScene(). - Muestre el escenario utilizando el método
show()e inicie la aplicación.
Implementemos un ejemplo basado en los pasos anteriores.
Código de ejemplo:
package delftstack;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class JavaFX_Display_Text extends Application {
@Override
public void start(Stage Demo_Stage) {
// Create a Text object
Text Demo_Text = new Text();
// Set the text to be added.
Demo_Text.setText("Hello, This is delftstack.com");
// set the position of the text
Demo_Text.setX(80);
Demo_Text.setY(80);
// Create a Group object
Group Group_Root = new Group(Demo_Text);
// Create a scene object
Scene Demo_Scene = new Scene(Group_Root, 600, 300);
// Set title to the Stage
Demo_Stage.setTitle("Text Display");
// Add scene to the stage
Demo_Stage.setScene(Demo_Scene);
// Display the contents of the stage
Demo_Stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
El código anterior creará y mostrará el Texto en la escena.
Producción:
Podemos utilizar un Label en lugar del Text para mostrar un texto de varias líneas. Crea una Label y pásale el Text.
Debemos envolver el Text en una Label para mostrarlo como un texto multilínea.
Código de ejemplo:
package delftstack;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFX_Display_Text extends Application {
@Override
public void start(Stage Demo_Stage) {
String Content = "DelftStack is a resource for everyone interested in programming, "
+ "embedded software, and electronics. It covers the programming languages "
+ "like Python, C/C++, C#, and so on in this website's first development stage. "
+ "Open-source hardware also falls in the website's scope, like Arduino, "
+ "Raspberry Pi, and BeagleBone. DelftStack aims to provide tutorials, "
+ "how-to's, and cheat sheets to different levels of developers and hobbyists..";
// Create a Label
Label Demo_Text = new Label(Content);
// wrap the label
Demo_Text.setWrapText(true);
// Set the maximum width of the label
Demo_Text.setMaxWidth(300);
// Set the position of the label
Demo_Text.setTranslateX(30);
Demo_Text.setTranslateY(30);
Group Text_Root = new Group();
Text_Root.getChildren().add(Demo_Text);
// Set the stage
Scene Text_Scene = new Scene(Text_Root, 595, 150, Color.BEIGE);
Demo_Stage.setTitle("Display Multiline Text");
Demo_Stage.setScene(Text_Scene);
Demo_Stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
El código anterior mostrará el texto envuelto en una etiqueta como multilínea.
Producción: