Darano

Few Components

Dec 27th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Application;
  4. import javafx.collections.FXCollections;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.*;
  8. import javafx.scene.image.Image;
  9. import javafx.scene.image.ImageView;
  10. import javafx.scene.layout.BorderPane;
  11. import javafx.scene.layout.HBox;
  12. import javafx.scene.layout.VBox;
  13. import javafx.stage.Stage;
  14.  
  15. public class Main extends Application {
  16.  
  17.     @Override
  18.     public void start(Stage primaryStage) throws Exception{
  19.         //here you can set the title of your stage
  20.         primaryStage.setTitle("Komponentensalat");
  21.  
  22.         //We chose a BorderPane as our primary layout.
  23.         BorderPane borderPane = new BorderPane();
  24.  
  25.  
  26.         //Here we just created a label with text to indicate the top part of the BorderPane.
  27.         Label labelNorth = new Label("This is a label at the top of the BorderPane\n ");
  28.  
  29.  
  30.         //An example of how to place a group of RadioButtons in a vertical layout.
  31.         Label labelWest = new Label("This is a group of RadioButtons\npositioned by a VBox with spacing 10   ");
  32.         //The ToggleGroup is necessary to ensure that only one RadioButton can be selected at any time.
  33.         ToggleGroup group = new ToggleGroup();
  34.         RadioButton rb1 = new RadioButton("Choice A");
  35.         RadioButton rb2 = new RadioButton("Choice B");
  36.         RadioButton rb3 = new RadioButton("Choice C");
  37.         rb1.setToggleGroup(group);
  38.         rb2.setToggleGroup(group);
  39.         rb3.setToggleGroup(group);
  40.         //VBox lets us position (GUI) components vertically. The (10) indicates the spacing between components.
  41.         VBox vboxLeft = new VBox(10);
  42.         //multiple components can be added to the VBox using addAll(...)
  43.         vboxLeft.getChildren().addAll(labelWest,rb1,rb2,rb3);
  44.  
  45.  
  46.         //An example of how a ChoiceBox can be used.
  47.         //Note that the \n indicates a line break in a String.
  48.         Label labelEast = new Label("Here is a Choice Box with languages.\nThe text underneath changes\ndepening on your language choice");
  49.         //For the Spanish n with a ~ above, you have to place \u00F1 inside your string
  50.         final String[] greetingsLanguage = {"Hello in English","Hallo auf deutsch","Hola en espa\u00F1ol"};
  51.         Label greetings = new Label();
  52.         ChoiceBox choiceBox = new ChoiceBox(FXCollections.observableArrayList("English", "Deutsch", "Espa\u00F1ol"));
  53.         //Please note that in more complex scenarios it is usually a good idea to separate the view from the functions.
  54.         //In this case you should register the controller (in the MVC pattern) as a listener to the ChoiceBox.
  55.         //This is a lambda expression
  56.         choiceBox.getSelectionModel().selectedIndexProperty().addListener((ov,value,newValue)->{greetings.setText(greetingsLanguage[newValue.intValue()]);});
  57.         choiceBox.getSelectionModel().select(0);
  58.         VBox vboxRight = new VBox(10);
  59.         vboxRight.getChildren().addAll(labelEast,choiceBox,greetings);
  60.  
  61.  
  62.         //This is an example of how images can be added to your JavaFX application.
  63.         Label labelCenter = new Label("Here is a way to include images\nin your JavaFX Application");
  64.         //Place your images in the resources package so that it can be loaded as follows.
  65.         //It is also possible to specify a URL path here.
  66.         Image img = new Image(getClass().getResource("/resources/javafx.png").toURI().toString());
  67.         //add the image to your ImageView, where you can set the size etc.
  68.         ImageView imView = new ImageView(img);
  69.         imView.setFitHeight(150);
  70.         imView.setFitWidth(300);
  71.         VBox vboxCenter = new VBox(10);
  72.         vboxCenter.getChildren().addAll(labelCenter,imView);
  73.  
  74.  
  75.         //This is an example of a TextField, in which you can write something, and a Button. When clicking on the button
  76.         //some action should be performed, in this case update the label to greet the user.
  77.         Label labelSouth = new Label("Here you can enter your name into a TextField and press the OK Button");
  78.         Label nameLabel = new Label();
  79.         Label lName = new Label("Please enter your name: ");
  80.         TextField tf = new TextField();
  81.         Button button = new Button("OK");
  82.         //Please note that also in this case it is good practise to implement functionality in the controller in more
  83.         //complex examples.
  84.         button.setOnAction(e->nameLabel.setText("Hello "+tf.getText()));
  85.         HBox hbox = new HBox(15,lName,tf,button);
  86.         VBox vboxBottom = new VBox(10,labelSouth,hbox,nameLabel);
  87.  
  88.         //with setTop etc you can add components to the BorderPane.
  89.         borderPane.setTop(labelNorth);
  90.         borderPane.setAlignment(labelNorth, Pos.CENTER);
  91.         borderPane.setLeft(vboxLeft);
  92.         borderPane.setBottom(vboxBottom);
  93.         borderPane.setRight(vboxRight);
  94.         borderPane.setCenter(vboxCenter);
  95.  
  96.         //Don't forget to add the borderPane to the scene, else you will not be able to see it.
  97.         //Here you can also specify the size of the scene.
  98.         primaryStage.setScene(new Scene(borderPane, 800, 300));
  99.         //without stage.show() the stage will be invisible.
  100.         primaryStage.show();
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment