Skip to content

Commit 4d3264f

Browse files
committed
added inner class and lambda examples
1 parent b08b08c commit 4d3264f

4 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎muratagenc/MyJavaFXStudies/nbproject/project.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ javadoc.splitindex=true
5454
javadoc.use=true
5555
javadoc.version=false
5656
javadoc.windowtitle=
57-
main.class=a_hello_world.HelloWorld
57+
main.class=a_stage_button_event.FirstApp
5858
manifest.file=manifest.mf
5959
meta.inf.dir=${src.dir}/META-INF
6060
mkdist.disabled=false

muratagenc/MyJavaFXStudies/src/a_hello_world/HelloWorld.java renamed to muratagenc/MyJavaFXStudies/src/a_stage_button_event/FirstApp.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package a_hello_world;
1+
package a_stage_button_event;
22

33
import javafx.application.*;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
46
import javafx.scene.*;
57
import javafx.scene.control.Button;
68
import javafx.scene.layout.StackPane;
79
import javafx.stage.*;
810

9-
public class HelloWorld extends Application {
11+
public class FirstApp extends Application implements EventHandler<ActionEvent> {
1012

1113
Button button;
1214

@@ -21,7 +23,9 @@ public void start (Stage primaryStage) throws Exception{
2123

2224
primaryStage.setTitle("Hello World");
2325

24-
button = new Button("Click me");
26+
button = new Button();
27+
button.setText("Click me");
28+
button.setOnAction(this);
2529

2630
StackPane layout = new StackPane();
2731
layout.getChildren().add(button);
@@ -31,4 +35,11 @@ public void start (Stage primaryStage) throws Exception{
3135
primaryStage.show();
3236

3337
}
38+
39+
@Override
40+
public void handle(ActionEvent event){
41+
if(event.getSource() == button){
42+
System.out.println("Hello World");
43+
}
44+
}
3445
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package b_innerclass_lambda;
2+
3+
import javafx.application.Application;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
6+
import javafx.scene.Scene;
7+
import javafx.scene.control.Button;
8+
import javafx.scene.layout.StackPane;
9+
import javafx.stage.Stage;
10+
11+
public class InnerClass extends Application {
12+
13+
Button button;
14+
15+
public static void main(String []args){
16+
launch(args);
17+
}
18+
19+
public void start(Stage primaryStage){
20+
primaryStage.setTitle("Inner Class and Lambdas");
21+
button = new Button();
22+
button.setText("Click Me");
23+
24+
//you don't have to check multiple resources
25+
//with anonymous classes, since they are already
26+
//implemented for a specific object
27+
//downside: needs to be implemented for each
28+
//object..
29+
button.setOnAction(new EventHandler<ActionEvent>(){
30+
31+
@Override
32+
public void handle(ActionEvent event){
33+
System.out.println("I am anonymous class");
34+
}
35+
});
36+
37+
StackPane layout = new StackPane();
38+
layout.getChildren().add(button);
39+
Scene scene = new Scene(layout, 300, 250);
40+
primaryStage.setScene(scene);
41+
primaryStage.show();
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package c_lambdas_intro;
2+
3+
import javafx.application.Application;
4+
import static javafx.application.Application.launch;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.Button;
7+
import javafx.scene.layout.StackPane;
8+
import javafx.stage.Stage;
9+
10+
public class LambdasIntro extends Application {
11+
12+
Button button;
13+
14+
public static void main(String []args){
15+
launch(args);
16+
}
17+
18+
public void start(Stage primaryStage){
19+
primaryStage.setTitle("Inner Class and Lambdas");
20+
button = new Button();
21+
button.setText("Click Me");
22+
23+
//Lambdas are available only with Java 8 and above..
24+
button.setOnAction(e -> System.out.println("First Lambda Expression"));
25+
26+
StackPane layout = new StackPane();
27+
layout.getChildren().add(button);
28+
Scene scene = new Scene(layout, 300, 250);
29+
primaryStage.setScene(scene);
30+
primaryStage.show();
31+
}
32+
}

0 commit comments

Comments
 (0)