Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaFX Simple FXML-Controler Sample
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="100.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="FormController">
<children>
<HBox alignment="CENTER" prefHeight="32.0" prefWidth="320.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextField fx:id="field" prefWidth="200.0" />
<Button fx:id="button" onAction="#handleAction" text="Submit" />
</children>
</HBox>
<Label fx:id="label" minHeight="16.0" minWidth="69.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="40.0" />
</children>
<stylesheets>
<URL value="@formstyle.css" />
</stylesheets>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class FormController implements Initializable {
@FXML
private TextField field;
@FXML
private Label label;
@FXML
private void handleAction(ActionEvent event) {
// TextFiled から文字列を取得
String text = field.getText();
// TextFieldから取得した文字列をLabelにセット
label.setText(text);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FormSample extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Forrm Sample");
Parent root = FXMLLoader.load(getClass().getResource("Form.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
.text-field {
-fx-font-size: 16pt;
}
.button {
-fx-font-size: 16pt;
}
.label {
-fx-font-size: 36pt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.