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

JavaFXطريقة وضع أكثر من شيء في AnchorPane

المثال التالي يعلمك طريقة وضع أكثر من شيء في AnchorPane.

ملاحظة: هنا قمنا بتحديد حجم الفراغ بين كل Button و بين الـ AnchorPane من جميع الجوانب بالإضافة إلى تحديد حجمهم المفضل كالتالي:

  • Button1 يتأثر بعرض الـ AnchorPane فقط.
  • Button2 يتأثر بعرض و طول الـ AnchorPane.
  • Button3 يتأثر بطول الـ AnchorPane فقط.

مثال

Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
@Override
public void start(Stage stage) {
// في النافذة Root Node و الذي ننوي جعله الـ AnchorPane هنا قمنا بإنشاء كائن من الكلاس
AnchorPane root = new AnchorPane();
// root هنا قمنا بإنشاء جميع الأشياء التي سنضيفها في الكائن
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
// الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليسار و اليمين بين الكائن
AnchorPane.setTopAnchor(button1, 30.0);
AnchorPane.setRightAnchor(button1, 30.0);
AnchorPane.setLeftAnchor(button1, 30.0);
// ثابت و يساوي 30 بيكسل فقط كحد أقصى إذا كان هناك مساحة متوفرة في النافذة button1 هنا جعلنا طول الكائن
button1.setPrefHeight(30);
// الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليسار, اليمين و الأسفل بين الكائن
AnchorPane.setTopAnchor(button2, 70.0);
AnchorPane.setRightAnchor(button2, 130.0);
AnchorPane.setLeftAnchor(button2, 30.0);
AnchorPane.setBottomAnchor(button2, 30.0);
// الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليمين و الأسفل بين الكائن
AnchorPane.setTopAnchor(button3, 70.0);
AnchorPane.setRightAnchor(button3, 30.0);
AnchorPane.setBottomAnchor(button3, 30.0);
// ثابت و يساوي 80 بيكسل فقط كحد أقصى إذا كان هناك مساحة متوفرة في النافذة button3 هنا جعلنا عرض الكائن
button3.setPrefWidth(80);
// root في الكائن button هنا قمنا بإضافة الكائن
root.getChildren().addAll(button1, button2, button3);
// فيها و تحديد حجمها Node كأول root هنا قمنا بإنشاء محتوى النافذة مع تعيين الكائن
Scene scene = new Scene(root, 350, 250);
// هنا وضعنا عنوان للنافذة
stage.setTitle("JavaFX AnchorPane");
// أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .stage في كائن الـ scene هنا وضعنا كائن الـ
stage.setScene(scene);
// هنا قمنا بإظهار النافذة
stage.show();
}
// هنا قمنا بتشغيل التطبيق
public static void main(String[] args) {
launch(args);
}
}
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; import javafx.scene.layout.AnchorPane; public class Main extends Application { @Override public void start(Stage stage) { // في النافذة Root Node و الذي ننوي جعله الـ AnchorPane هنا قمنا بإنشاء كائن من الكلاس AnchorPane root = new AnchorPane(); // root هنا قمنا بإنشاء جميع الأشياء التي سنضيفها في الكائن Button button1 = new Button("Button 1"); Button button2 = new Button("Button 2"); Button button3 = new Button("Button 3"); // الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليسار و اليمين بين الكائن AnchorPane.setTopAnchor(button1, 30.0); AnchorPane.setRightAnchor(button1, 30.0); AnchorPane.setLeftAnchor(button1, 30.0); // ثابت و يساوي 30 بيكسل فقط كحد أقصى إذا كان هناك مساحة متوفرة في النافذة button1 هنا جعلنا طول الكائن button1.setPrefHeight(30); // الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليسار, اليمين و الأسفل بين الكائن AnchorPane.setTopAnchor(button2, 70.0); AnchorPane.setRightAnchor(button2, 130.0); AnchorPane.setLeftAnchor(button2, 30.0); AnchorPane.setBottomAnchor(button2, 30.0); // الذي سيوضع بداخله AnchorPane و كائن الـ button هنا قمنا بتحديد حجم الفراغ من الأعلى, اليمين و الأسفل بين الكائن AnchorPane.setTopAnchor(button3, 70.0); AnchorPane.setRightAnchor(button3, 30.0); AnchorPane.setBottomAnchor(button3, 30.0); // ثابت و يساوي 80 بيكسل فقط كحد أقصى إذا كان هناك مساحة متوفرة في النافذة button3 هنا جعلنا عرض الكائن button3.setPrefWidth(80); // root في الكائن button هنا قمنا بإضافة الكائن root.getChildren().addAll(button1, button2, button3); // فيها و تحديد حجمها Node كأول root هنا قمنا بإنشاء محتوى النافذة مع تعيين الكائن Scene scene = new Scene(root, 350, 250); // هنا وضعنا عنوان للنافذة stage.setTitle("JavaFX AnchorPane"); // أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .stage في كائن الـ scene هنا وضعنا كائن الـ stage.setScene(scene); // هنا قمنا بإظهار النافذة stage.show(); } // هنا قمنا بتشغيل التطبيق public static void main(String[] args) { launch(args); } }

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

طريقة وضع أكثر من شيء في AnchorPane في JavaFX

ببطئ قم بتصغير و تكبير حجم النافذة لتفهم كيف ستتغير أحجام الأشياء الموضوعة في الكائن AnchorPane نسبة لحجمه.