Skip to content
Snippets Groups Projects
Commit 0f3bd788 authored by kaller01's avatar kaller01
Browse files

gridpane

parent 6a1d4fc7
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import javafx.scene.control.Label; ...@@ -9,6 +9,7 @@ import javafx.scene.control.Label;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
...@@ -24,26 +25,26 @@ public class Main extends Application { ...@@ -24,26 +25,26 @@ public class Main extends Application {
root.setMinWidth(600); root.setMinWidth(600);
root.setMinHeight(600); root.setMinHeight(600);
root.setSpacing(20); root.setSpacing(20);
root.setBackground(new Background(new BackgroundFill(Color.GREEN, null, null))); //Look for a green box root.setBackground(new Background(new BackgroundFill(Color.GREEN, null, null))); // Look for a green box
//These are nodes that can be put in a layout, read docs. // These are nodes that can be put in a layout, read docs.
TextField field = new TextField(); TextField field = new TextField();
Button button = new Button("Click me!"); Button button = new Button("Click me!");
//Canvas is special, because you can draw on it. // Canvas is special, because you can draw on it.
Canvas canvas = new Canvas(); Canvas canvas = new Canvas();
canvas.setHeight(200); canvas.setHeight(200);
canvas.setWidth(200); canvas.setWidth(200);
//Using the graphics context, you can draw. Canvas has x and y coordinates. // Using the graphics context, you can draw. Canvas has x and y coordinates.
GraphicsContext gc = canvas.getGraphicsContext2D(); GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK); gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
//Vertical box, nodes will stack on y axis. // Vertical box, nodes will stack on y axis.
VBox box = new VBox(); VBox box = new VBox();
box.setMinHeight(300); box.setMinHeight(300);
box.setMinWidth(300); box.setMinWidth(300);
box.setMaxHeight(400); box.setMaxHeight(400);
box.setBackground(new Background(new BackgroundFill(Color.PINK, null, null))); //Look for a pink box! box.setBackground(new Background(new BackgroundFill(Color.PINK, null, null))); // Look for a pink box!
// This is a listener, when you press the button this code will execute // This is a listener, when you press the button this code will execute
button.setOnAction(new EventHandler<ActionEvent>() { button.setOnAction(new EventHandler<ActionEvent>() {
...@@ -54,26 +55,49 @@ public class Main extends Application { ...@@ -54,26 +55,49 @@ public class Main extends Application {
box.getChildren().add(label); box.getChildren().add(label);
field.setText(""); field.setText("");
//Special case, try it // Special case, try it
if(text.equals("TDDC77")){ if (text.equals("TDDC77")) {
gc.setFill(Color.LIGHTBLUE); gc.setFill(Color.LIGHTBLUE);
gc.fillOval(50, 50, 100, 100); gc.fillOval(50, 50, 100, 100);
} }
} }
}); });
Scene scene = new Scene(root); // Create the scene with root element Scene scene = new Scene(root); // Create the scene with root element
primaryStage.setScene(scene); primaryStage.setScene(scene);
root.getChildren().add(field); //put field in green box root.getChildren().add(field); // put field in green box
root.getChildren().add(button); //put button in green box root.getChildren().add(button); // put button in green box
root.getChildren().add(box); //put pink box in green box root.getChildren().add(box); // put pink box in green box
root.getChildren().add(canvas); //put canvas in green box root.getChildren().add(canvas); // put canvas in green box
box.getChildren().add(new Label("Hello world")); //put label in pink box box.getChildren().add(new Label("Hello world")); // put label in pink box
// Grid
GridPane gridPane = new GridPane();
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
Button button5 = new Button("Button 5");
Button button6 = new Button("Button 6");
// gridPane.setBackground(new Background(new BackgroundFill(Color.PINK, null, null))); // Look for a pink box!
gridPane.add(button1, 0, 0, 2, 2);
gridPane.add(button2, 2, 0, 1, 1);
gridPane.add(button3, 2, 1, 1, 1);
gridPane.add(button4, 0, 2, 1, 1);
gridPane.add(button5, 1, 2, 1, 1);
gridPane.add(button6, 2, 2, 1, 1);
gridPane.setGridLinesVisible(true);
gridPane.setHgap(200);
gridPane.setVgap(200);
root.getChildren().add(gridPane);
//Last thing to do is show // Last thing to do is show
primaryStage.show(); primaryStage.show();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment