Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

JavaFXطريقة تنفيذ أوامر عند النقر على الـButton

المثال التالي يعلمك طريقة تنفيذ أوامر عند النقر على الـ Button.
فعلياً, عند النقر على الـ Button سيتم إظهار نافذة جديدة.


مثال

Main.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
// يمثل الزر الذي نريد إضافته في النافذة Button هنا قمنا بإنشاء كائن من الكلاس
Button button = new Button("Open Window");
// في النافذة button هنا قمنا بتحديد مكان ظهور الكائن
button.setTranslateX(150);
button.setTranslateY(110);
// في النافذة button هنا قمنا بتحديد مكان ظهور الكائن
Stage stage2 = new Stage();
stage2.setTitle("Second Window");
stage2.setMaxWidth(400);
stage2.setMaxHeight(250);
// stage في النافذة Root Node لأننا ننوي جعله الـ Group هنا قمنا بإنشاء كائن من الكلاس
Group root = new Group();
// root في الكائن button هنا قمنا بإضافة الكائن
root.getChildren().add(button);
// فيها و تحديد حجمها Node كأول root مع تعيين الكائن stage هنا قمنا بإنشاء محتوى النافذة
Scene scene = new Scene(root, 400, 250);
// stage هنا وضعنا عنوان للنافذة
stage.setTitle("First Window");
// stage أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .stage في كائن الـ scene هنا وضعنا كائن الـ
stage.setScene(scene);
// stage هنا قمنا بإظهار النافذة
stage.show();
// button هنا قمنا بتحديد ماذا سيحدث عند النقر على الكائن
button.setOnAction((ActionEvent e) -> {
// عند النقر على الزر سيتم إظهار النافذة الثانية فقط
stage2.show();
});
}
// هنا قمنا بتشغيل التطبيق
public static void main(String[] args) {
launch(args);
}
}
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { // يمثل الزر الذي نريد إضافته في النافذة Button هنا قمنا بإنشاء كائن من الكلاس Button button = new Button("Open Window"); // في النافذة button هنا قمنا بتحديد مكان ظهور الكائن button.setTranslateX(150); button.setTranslateY(110); // في النافذة button هنا قمنا بتحديد مكان ظهور الكائن Stage stage2 = new Stage(); stage2.setTitle("Second Window"); stage2.setMaxWidth(400); stage2.setMaxHeight(250); // stage في النافذة Root Node لأننا ننوي جعله الـ Group هنا قمنا بإنشاء كائن من الكلاس Group root = new Group(); // root في الكائن button هنا قمنا بإضافة الكائن root.getChildren().add(button); // فيها و تحديد حجمها Node كأول root مع تعيين الكائن stage هنا قمنا بإنشاء محتوى النافذة Scene scene = new Scene(root, 400, 250); // stage هنا وضعنا عنوان للنافذة stage.setTitle("First Window"); // stage أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .stage في كائن الـ scene هنا وضعنا كائن الـ stage.setScene(scene); // stage هنا قمنا بإظهار النافذة stage.show(); // button هنا قمنا بتحديد ماذا سيحدث عند النقر على الكائن button.setOnAction((ActionEvent e) -> { // عند النقر على الزر سيتم إظهار النافذة الثانية فقط stage2.show(); }); } // هنا قمنا بتشغيل التطبيق public static void main(String[] args) { launch(args); } }

ستظهر لك النافذة التالية عند التشغيل.

طريقة إظهار نافذة جديدة عند النقر على الـ Button في javafx