JavaFX: Alerts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Alert; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Button boton1 = new Button("Alerta 1"); | |
Button boton2 = new Button("Alerta 2"); | |
Button boton3 = new Button("Alerta 3"); | |
boton1.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
Alert alert = new Alert(Alert.AlertType.INFORMATION); | |
alert.setTitle("Este es un díalogo de información"); | |
alert.setHeaderText("Este es el mensaje de cabecera"); | |
alert.setContentText("Aquí una descripción más larga"); | |
alert.showAndWait(); | |
} | |
}); | |
boton2.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
Alert alert = new Alert(Alert.AlertType.INFORMATION); | |
alert.setTitle("Este es un díalogo sin cabecera"); | |
alert.setHeaderText(null); | |
alert.setContentText("Aquí una descripción más larga"); | |
alert.showAndWait(); | |
} | |
}); | |
boton3.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
Alert alert = new Alert(Alert.AlertType.ERROR); | |
alert.setTitle("Este es un díalogo de error"); | |
alert.setHeaderText("Este es el mensaje de cabecera"); | |
alert.setContentText("Aquí una descripción más larga"); | |
alert.showAndWait(); | |
} | |
}); | |
VBox vBox = new VBox(); | |
vBox.getChildren().add(boton1); | |
vBox.getChildren().add(boton2); | |
vBox.getChildren().add(boton3); | |
vBox.setSpacing(20); | |
vBox.setPadding(new Insets(20, 20, 20, 20)); | |
Scene scene = new Scene(vBox, 400, 400); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment