Skip to content

Commit 8596a87

Browse files
committed
Added more JavaFX tutorials.
1 parent aa42455 commit 8596a87

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

JavaFX/004_switchingScenes/Main.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import javafx.application.*;
2+
import javafx.stage.*;
3+
import javafx.scene.*;
4+
import javafx.scene.layout.*;
5+
import javafx.scene.control.*;
6+
7+
public class Main extends Application {
8+
9+
Stage window;
10+
Scene scene1, scene2;
11+
12+
public static void main(String[] args) {
13+
launch(args);
14+
}
15+
16+
@Override
17+
public void start(Stage primaryStage) {
18+
window = primaryStage;
19+
20+
//Button 1
21+
Label label1 = new Label("Welcome to the first scene!");
22+
Button button1 = new Button("Go to scene 2");
23+
button1.setOnAction(e -> window.setScene(scene2));
24+
25+
//Layout 1 - children laid out in vertical column
26+
VBox layout1 = new VBox(20);
27+
layout1.getChildren().addAll(label1, button1);
28+
scene1 = new Scene(layout1, 200, 200);
29+
30+
31+
//Button 2
32+
Button button2 = new Button("This sucks, go back to scene 1");
33+
button2.setOnAction(e -> window.setScene(scene1));
34+
35+
//Layout 2
36+
StackPane layout2 = new StackPane();
37+
layout2.getChildren().add(button2);
38+
scene2 = new Scene(layout2, 600, 300);
39+
40+
//Display scene 1 at first
41+
window.setScene(scene1);
42+
window.setTitle("Title Here");
43+
window.show();
44+
}
45+
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import javafx.stage.*;
2+
import javafx.scene.*;
3+
import javafx.scene.layout.*;
4+
import javafx.scene.control.*;
5+
import javafx.geometry.*;
6+
7+
public class AlertBox {
8+
9+
public static void display(String title, String message) {
10+
Stage window = new Stage();
11+
12+
//Block events to other windows
13+
window.initModality(Modality.APPLICATION_MODAL);
14+
window.setTitle(title);
15+
window.setMinWidth(250);
16+
17+
Label label = new Label();
18+
label.setText(message);
19+
Button closeButton = new Button("Close this window");
20+
closeButton.setOnAction(e -> window.close());
21+
22+
VBox layout = new VBox(10);
23+
layout.getChildren().addAll(label, closeButton);
24+
layout.setAlignment(Pos.CENTER);
25+
26+
//Display window and wait for it to be closed before returning
27+
Scene scene = new Scene(layout);
28+
window.setScene(scene);
29+
window.showAndWait();
30+
}
31+
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import javafx.application.Application;
2+
import javafx.scene.Scene;
3+
import javafx.scene.control.Button;
4+
import javafx.scene.layout.StackPane;
5+
import javafx.stage.Stage;
6+
7+
public class Main extends Application {
8+
9+
Stage window;
10+
Button button;
11+
12+
public static void main(String[] args) {
13+
launch(args);
14+
}
15+
16+
@Override
17+
public void start(Stage primaryStage) {
18+
window = primaryStage;
19+
window.setTitle("thenewboston");
20+
button = new Button("Click Me");
21+
22+
button.setOnAction(e -> AlertBox.display("Title of Window", "Wow this alert box is awesome!"));
23+
24+
StackPane layout = new StackPane();
25+
layout.getChildren().add(button);
26+
Scene scene = new Scene(layout, 300, 250);
27+
window.setScene(scene);
28+
window.show();
29+
}
30+
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import javafx.stage.*;
2+
import javafx.scene.*;
3+
import javafx.scene.layout.*;
4+
import javafx.scene.control.*;
5+
import javafx.geometry.*;
6+
7+
public class ConfirmBox {
8+
9+
//Create variable
10+
static boolean answer;
11+
12+
public static boolean display(String title, String message) {
13+
Stage window = new Stage();
14+
window.initModality(Modality.APPLICATION_MODAL);
15+
window.setTitle(title);
16+
window.setMinWidth(250);
17+
Label label = new Label();
18+
label.setText(message);
19+
20+
//Create two buttons
21+
Button yesButton = new Button("Yes");
22+
Button noButton = new Button("No");
23+
24+
//Clicking will set answer and close window
25+
yesButton.setOnAction(e -> {
26+
answer = true;
27+
window.close();
28+
});
29+
noButton.setOnAction(e -> {
30+
answer = false;
31+
window.close();
32+
});
33+
34+
VBox layout = new VBox(10);
35+
36+
//Add buttons
37+
layout.getChildren().addAll(label, yesButton, noButton);
38+
layout.setAlignment(Pos.CENTER);
39+
Scene scene = new Scene(layout);
40+
window.setScene(scene);
41+
window.showAndWait();
42+
43+
//Make sure to return answer
44+
return answer;
45+
}
46+
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import javafx.application.Application;
2+
import javafx.scene.Scene;
3+
import javafx.scene.control.Button;
4+
import javafx.scene.layout.StackPane;
5+
import javafx.stage.Stage;
6+
7+
public class Main extends Application {
8+
9+
Stage window;
10+
Button button;
11+
12+
public static void main(String[] args) {
13+
launch(args);
14+
}
15+
16+
@Override
17+
public void start(Stage primaryStage) {
18+
window = primaryStage;
19+
window.setTitle("JavaFX - thenewboston");
20+
button = new Button("Click Me");
21+
22+
button.setOnAction(e -> {
23+
boolean result = ConfirmBox.display("Title of Window", "Are you sure you want to send that pic?");
24+
System.out.println(result);
25+
});
26+
27+
StackPane layout = new StackPane();
28+
layout.getChildren().add(button);
29+
Scene scene = new Scene(layout, 300, 250);
30+
window.setScene(scene);
31+
window.show();
32+
}
33+
34+
}

0 commit comments

Comments
 (0)