0% found this document useful (0 votes)
49 views2 pages

Exo Ihm

The document defines a JavaFX application class that creates a scene with 4 buttons labeled "a" through "d". When the corresponding key or enter is pressed on each button, its background color changes. When the key is released, the background resets to normal. The application window is displayed when launched.

Uploaded by

Amira Said
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views2 pages

Exo Ihm

The document defines a JavaFX application class that creates a scene with 4 buttons labeled "a" through "d". When the corresponding key or enter is pressed on each button, its background color changes. When the key is released, the background resets to normal. The application window is displayed when launched.

Uploaded by

Amira Said
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javafx.application.

Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class App extends Application {


@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root,600,600,Color.IVORY);

// button 1
Button btn = new Button();
root.getChildren().add(btn);
btn.setLayoutX(50);
btn.setLayoutY(50);
btn.setText("a");

btn.setOnKeyPressed(r -> {
if ((r.getCode() == KeyCode.A)||(r.getCode() == KeyCode.ENTER)) {
btn.setStyle("-fx-background-color:black;");
}
});
btn.setOnKeyReleased(r -> {
if ((r.getCode() == KeyCode.A)||(r.getCode() == KeyCode.ENTER)) {
btn.setStyle("none");
}
});

// button 2
Button btn2 = new Button();
root.getChildren().add(btn2);
btn2.setLayoutX(50);
btn2.setLayoutY(100);
btn2.setText("b");

btn2.setOnKeyPressed(e -> {
if ((e.getCode() == KeyCode.B)||(e.getCode() == KeyCode.ENTER)) {
btn2.setStyle("-fx-background-color:yellow;");
}
});
btn2.setOnKeyReleased(e -> {
if ((e.getCode() == KeyCode.B)||(e.getCode() == KeyCode.ENTER)) {
btn2.setStyle("none");
}
});

// button 3
Button btn3 = new Button();
root.getChildren().add(btn3);
btn3.setLayoutX(50);
btn3.setLayoutY(150);
btn3.setText("c");

btn3.setOnKeyPressed(g -> {
if ((g.getCode() == KeyCode.C)||(g.getCode() == KeyCode.ENTER)) {
btn3.setStyle("-fx-background-color:green;");
}
});
btn3.setOnKeyReleased(g -> {
if ((g.getCode() == KeyCode.C)||(g.getCode() == KeyCode.ENTER)) {
btn3.setStyle("none");
}
});

// button 4
Button btn4 = new Button();
root.getChildren().add(btn4);
btn4.setLayoutX(50);
btn4.setLayoutY(200);
btn4.setText("d");

btn4.setOnKeyPressed(f -> {
if ((f.getCode() == KeyCode.D)||(f.getCode() == KeyCode.ENTER)) {
btn4.setStyle("-fx-background-color:red;");
}
});
btn4.setOnKeyReleased(f -> {
if ((f.getCode() == KeyCode.D)||(f.getCode() == KeyCode.ENTER)) {
btn4.setStyle("none");
}
});

primaryStage.setScene(scene);
primaryStage.setTitle("I AM THE DANGER!!!!!");
primaryStage.show();
}

public static void main(String[] args) {


launch(args);
}
}

You might also like