Skip to content

Commit fe214b7

Browse files
committed
switching between scenes code added
1 parent 1aa5d37 commit fe214b7

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

muratagenc/MyJavaFXStudies/src/b_innerclass_lambda/InnerClass.java renamed to muratagenc/MyJavaFXStudies/src/b_innerclass/InnerClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package b_innerclass_lambda;
1+
package b_innerclass;
22

33
import javafx.application.Application;
44
import javafx.event.ActionEvent;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
creating multiple scenes
3+
*/
4+
5+
package d_setup_multiple_scenes;
6+
7+
8+
import javafx.application.*;
9+
import javafx.scene.Scene;
10+
import javafx.scene.control.Button;
11+
import javafx.scene.control.Label;
12+
import javafx.scene.layout.StackPane;
13+
import javafx.scene.layout.VBox;
14+
import javafx.stage.Stage;
15+
16+
//always extend Application for JavaFX
17+
public class Main extends Application {
18+
19+
//data members
20+
Stage window;
21+
Scene scene1, scene2;
22+
23+
//constructors
24+
25+
//methods
26+
27+
public static void main(String[] args) {
28+
launch(args); //launch the GUI
29+
}
30+
31+
//launch calls start(), so implement that
32+
@Override
33+
public void start(Stage primaryStage) throws Exception{
34+
window = primaryStage;
35+
36+
Label label1 = new Label("Welcome to the first scene!");
37+
Button button1 = new Button("Go to scene 2");
38+
39+
//add action to the button, which will switch the
40+
//view to scene2. default is scene 1.
41+
//! with lambdas, you can run methods without using inner classes
42+
button1.setOnAction(e -> window.setScene(scene2));
43+
44+
//layout 1 for scene 1: children are laid out in vertical
45+
//column. on top of eachother. 20 pixels
46+
VBox layout1 = new VBox(20);
47+
//add stuff to the layout
48+
layout1.getChildren().addAll(label1, button1);
49+
50+
//and add layout to the scene
51+
scene1 = new Scene(layout1, 200, 200);
52+
53+
/*
54+
we have a stage, and a scene which uses a certain layout.
55+
*/
56+
57+
//now, we will create the second scene
58+
//button and label for scene2
59+
Label label2 = new Label("Welcome to the SECOND scene!");
60+
Button button2 = new Button("Go back to scene 1");
61+
button2.setOnAction(e -> window.setScene(scene1));
62+
63+
//this time we will use a diffrent layout
64+
StackPane layout2 = new StackPane();
65+
layout2.getChildren().addAll(label2, button2);
66+
//put everything to the scene2
67+
scene2 = new Scene(layout2, 600, 300);
68+
69+
//we need to tell the window which scene should be
70+
//visible by default
71+
window.setScene(scene1);
72+
window.setTitle("title here");
73+
window.show();
74+
75+
}
76+
}

0 commit comments

Comments
 (0)