Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created May 3, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvareztech/4cbbf5aca181e45bb02cde6429428d10 to your computer and use it in GitHub Desktop.
Save alvareztech/4cbbf5aca181e45bb02cde6429428d10 to your computer and use it in GitHub Desktop.
JavaFX: Ejemplo usando FXML
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class Controller {
@FXML
private TextField numero1TextField;
@FXML
private TextField numero2TextField;
@FXML
private Label resultadoLabel;
public void sumar(ActionEvent actionEvent) {
System.out.println("sumar");
int numero1 = Integer.parseInt(numero1TextField.getText());
int numero2 = Integer.parseInt(numero2TextField.getText());
int resultado = numero1 + numero2;
resultadoLabel.setText(resultado + "");
}
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Una suma");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="200.0" prefWidth="300.0" spacing="20">
<padding>
<Insets bottom="20" left="20" right="20" top="20" />
</padding>
<children>
<TextField fx:id="numero1TextField" promptText="Número 1" />
<TextField fx:id="numero2TextField" promptText="Número 2" />
<Button mnemonicParsing="false" onAction="#sumar" text="Sumar" />
<Label text="Resultado" fx:id="resultadoLabel" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</VBox>
</children>
</GridPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment