Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Application;
- import javafx.collections.FXCollections;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- public class Main extends Application {
- @Override
- public void start(Stage primaryStage) throws Exception{
- //here you can set the title of your stage
- primaryStage.setTitle("Komponentensalat");
- //We chose a BorderPane as our primary layout.
- BorderPane borderPane = new BorderPane();
- //Here we just created a label with text to indicate the top part of the BorderPane.
- Label labelNorth = new Label("This is a label at the top of the BorderPane\n ");
- //An example of how to place a group of RadioButtons in a vertical layout.
- Label labelWest = new Label("This is a group of RadioButtons\npositioned by a VBox with spacing 10 ");
- //The ToggleGroup is necessary to ensure that only one RadioButton can be selected at any time.
- ToggleGroup group = new ToggleGroup();
- RadioButton rb1 = new RadioButton("Choice A");
- RadioButton rb2 = new RadioButton("Choice B");
- RadioButton rb3 = new RadioButton("Choice C");
- rb1.setToggleGroup(group);
- rb2.setToggleGroup(group);
- rb3.setToggleGroup(group);
- //VBox lets us position (GUI) components vertically. The (10) indicates the spacing between components.
- VBox vboxLeft = new VBox(10);
- //multiple components can be added to the VBox using addAll(...)
- vboxLeft.getChildren().addAll(labelWest,rb1,rb2,rb3);
- //An example of how a ChoiceBox can be used.
- //Note that the \n indicates a line break in a String.
- Label labelEast = new Label("Here is a Choice Box with languages.\nThe text underneath changes\ndepening on your language choice");
- //For the Spanish n with a ~ above, you have to place \u00F1 inside your string
- final String[] greetingsLanguage = {"Hello in English","Hallo auf deutsch","Hola en espa\u00F1ol"};
- Label greetings = new Label();
- ChoiceBox choiceBox = new ChoiceBox(FXCollections.observableArrayList("English", "Deutsch", "Espa\u00F1ol"));
- //Please note that in more complex scenarios it is usually a good idea to separate the view from the functions.
- //In this case you should register the controller (in the MVC pattern) as a listener to the ChoiceBox.
- //This is a lambda expression
- choiceBox.getSelectionModel().selectedIndexProperty().addListener((ov,value,newValue)->{greetings.setText(greetingsLanguage[newValue.intValue()]);});
- choiceBox.getSelectionModel().select(0);
- VBox vboxRight = new VBox(10);
- vboxRight.getChildren().addAll(labelEast,choiceBox,greetings);
- //This is an example of how images can be added to your JavaFX application.
- Label labelCenter = new Label("Here is a way to include images\nin your JavaFX Application");
- //Place your images in the resources package so that it can be loaded as follows.
- //It is also possible to specify a URL path here.
- Image img = new Image(getClass().getResource("/resources/javafx.png").toURI().toString());
- //add the image to your ImageView, where you can set the size etc.
- ImageView imView = new ImageView(img);
- imView.setFitHeight(150);
- imView.setFitWidth(300);
- VBox vboxCenter = new VBox(10);
- vboxCenter.getChildren().addAll(labelCenter,imView);
- //This is an example of a TextField, in which you can write something, and a Button. When clicking on the button
- //some action should be performed, in this case update the label to greet the user.
- Label labelSouth = new Label("Here you can enter your name into a TextField and press the OK Button");
- Label nameLabel = new Label();
- Label lName = new Label("Please enter your name: ");
- TextField tf = new TextField();
- Button button = new Button("OK");
- //Please note that also in this case it is good practise to implement functionality in the controller in more
- //complex examples.
- button.setOnAction(e->nameLabel.setText("Hello "+tf.getText()));
- HBox hbox = new HBox(15,lName,tf,button);
- VBox vboxBottom = new VBox(10,labelSouth,hbox,nameLabel);
- //with setTop etc you can add components to the BorderPane.
- borderPane.setTop(labelNorth);
- borderPane.setAlignment(labelNorth, Pos.CENTER);
- borderPane.setLeft(vboxLeft);
- borderPane.setBottom(vboxBottom);
- borderPane.setRight(vboxRight);
- borderPane.setCenter(vboxCenter);
- //Don't forget to add the borderPane to the scene, else you will not be able to see it.
- //Here you can also specify the size of the scene.
- primaryStage.setScene(new Scene(borderPane, 800, 300));
- //without stage.show() the stage will be invisible.
- primaryStage.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment