Skip to content

Instantly share code, notes, and snippets.

@PabloSanchezMartinez
Last active April 27, 2017 02:19
Show Gist options
  • Save PabloSanchezMartinez/bed45253b311dcc220ee1d2018b46fcc to your computer and use it in GitHub Desktop.
Save PabloSanchezMartinez/bed45253b311dcc220ee1d2018b46fcc to your computer and use it in GitHub Desktop.
JavaFX
package com.company;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
//Creando lo que se usara
Label nombre = new Label("Nombre");
Label apellido = new Label("Apellido");
Label color = new Label("Color Favorito");
Label sexo = new Label("Sexo");
TextField tf1 = new TextField();
tf1.setPromptText("Introduzca Nombre");//Texto dentro de la caja TextField
TextField tf2 = new TextField();
tf2.setPromptText("Introduzca Apellido");//Texto dentro de la caja TextField
ColorPicker col = new ColorPicker();
RadioButton r1 = new RadioButton("Masculino");
RadioButton r2 = new RadioButton("Femenino");
//Evitar que se seleccionen ambos
ToggleGroup tg1 = new ToggleGroup();
r1.setToggleGroup(tg1);
r2.setToggleGroup(tg1);
Button b1 = new Button("Adicionar");
//Creando Vista Derecha
ListView<String> list = new ListView<String>();
//Dancole accion al Boton b1 = Adicionar
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String l1 = tf1.getText();
tf1.setText(" ");
String l2 = tf2.getText();
tf2.setText(" ");
String l3 = col.getValue().toString();
String l4 = obtenerSexo(r1, r2);
list.getItems().add(l1);
list.getItems().add(l2);
list.getItems().add(l3);
list.getItems().add(l4);
}
});
VBox vbox1 = new VBox();//Creando caja Vertical
//Añadiendo datos a la caja Vertical VBox
vbox1.getChildren().add(nombre);
vbox1.getChildren().add(tf1);
vbox1.getChildren().add(apellido);
vbox1.getChildren().add(tf2);
vbox1.getChildren().add(color);
vbox1.getChildren().add(col);
vbox1.getChildren().add(sexo);
vbox1.getChildren().add(r1);
vbox1.getChildren().add(r2);
vbox1.getChildren().add(b1);
vbox1.setSpacing(10);
HBox hbox1 = new HBox();
hbox1.getChildren().add(vbox1);
hbox1.getChildren().add(list);
Scene pantalla = new Scene(hbox1, 400, 500);
primaryStage.setScene(pantalla);
primaryStage.setTitle("Formulario");
primaryStage.show();
}
private String obtenerSexo(RadioButton r1, RadioButton r2) {
if (r1.isSelected()) {
return "Masculino";
} else
return "Femenino";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment