JavaFX

JavaFX ist ein Framework für plattformübergreifende Rich Client Anwendungen. Das gilt sowohl für Web-Anwendungen als auch für Desktop Anwendungen. Seit der Java Version 8 ist JavaFX das offiziell empfohlene UI-Toolkit für Java.

class HelloController (MVC)


package de.snowbits.fx;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class HelloController extends Application implements Initializable {

	private static final String DEFAULT_MESSAGE = "Sag, ";

	@FXML
	private Label messageLB;
	@FXML
	private Button sayHelloBTN;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("hello.fxml"));
		Scene scene = new Scene(root);
		stage.setTitle("Hallo Welt!");
		stage.setScene(scene);
		stage.show();
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		messageLB.setText(DEFAULT_MESSAGE);
	}

	@FXML
	public void sayHello(ActionEvent event) {
		messageLB.setText(DEFAULT_MESSAGE + " Hallo JavaFX");
	}
}
	

FXML hello.fxml


<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<VBox prefHeight="180.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" 
 xmlns:fx="http://javafx.com/fxml/1" stylesheets="@hello.css" fx:controller="de.snowbits.fx.HelloController">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="292.0" prefWidth="490.0" VBox.vgrow="ALWAYS">
      <children>
      	<Label fx:id="messageLB" layoutX="45.0" layoutY="14.0" text="" styleClass="text-label" />
        <Button fx:id="sayHelloBTN" layoutX="39.0" layoutY="46.0" mnemonicParsing="false" 
         onAction="#sayHello" text="Sag Hallo" styleClass="button" />
      </children>
    </AnchorPane>
  </children>
</VBox>
	

CSS hello.css


VBox {
	-fx-background-color: white;
}
.button {
    -fx-background-radius: 12;
    -fx-padding: 4 10 4 10;
    -fx-shadow-highlight-color: f0f0f0;
    -fx-body-color: linear-gradient(from 0em 0em to 0em 2.083333em, white, #D7D7D7, white);
    -fx-cursor: hand;
}
.text-label {
	-fx-font-weight: bold;	
}
	

Hello Welt


class HelloWorld


package de.snowbits.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HelloWorld extends Application {
	
	private static final String DEFAULT_MESSAGE = "Sag, ";
	
	private Label messageLB = new Label(DEFAULT_MESSAGE);
	private Button sayHelloBTN = new Button("Sag Hallo");
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("Hallo Welt!");
		messageLB.getStyleClass().add("text-label");
		sayHelloBTN.setText("Hallo JavaFX");
		sayHelloBTN.getStyleClass().add("button");
		sayHelloBTN.setOnAction(e -> messageLB.setText(DEFAULT_MESSAGE + " Hallo JavaFX"));
		VBox root = new VBox(2);
		Scene scene = new Scene(root, 300, 180);
		scene.getStylesheets().add(getClass().getResource("hello.css").toExternalForm());
		root.setPadding(new Insets(10, 50, 50, 50));
		root.setSpacing(10);
		root.getChildren().addAll(messageLB, sayHelloBTN);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
	

Top


Example
Sitemap Kontakt Impressum