JavaFXطريقة إظهار محتوى الـGridPane على كل المساحة المتوفرة
المثال التالي يعلمك طريقة جعل محتوى الـ GridPane يظهر على كل المساحة المتوفرة.
مثال
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
public class Main extends Application {
@Override
public void start(Stage stage) {
// في النافذة Root Node و الذي ننوي جعله الـ GridPane هنا قمنا بإنشاء كائن من الكلاس
GridPane root = new GridPane();
// root هنا قمنا بإنشاء جميع الأشياء التي سنضيفها في الكائن
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
Button button5 = new Button("Button 5");
Button button6 = new Button("Button 6");
// و هو كل المساحة المتوفرة بالطول و العرض button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا قمنا بجعل الحجم الأقصى للكائنات
button1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button3.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button4.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button5.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button6.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
// يكبر لأقصى حدود ممكنة button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا جعلنا طول الكائنات
GridPane.setVgrow(button1, Priority.ALWAYS);
GridPane.setVgrow(button2, Priority.ALWAYS);
GridPane.setVgrow(button3, Priority.ALWAYS);
GridPane.setVgrow(button4, Priority.ALWAYS);
GridPane.setVgrow(button5, Priority.ALWAYS);
GridPane.setVgrow(button6, Priority.ALWAYS);
// يكبر لأقصى حدود ممكنة button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا جعلنا عرض الكائنات
GridPane.setHgrow(button1, Priority.ALWAYS);
GridPane.setHgrow(button2, Priority.ALWAYS);
GridPane.setHgrow(button3, Priority.ALWAYS);
GridPane.setHgrow(button4, Priority.ALWAYS);
GridPane.setHgrow(button5, Priority.ALWAYS);
GridPane.setHgrow(button6, Priority.ALWAYS);
// root في أول سطر في الكائن button3 و button2 و button1 هنا قمنا بإضافة الكائنات
root.add(button1, 0, 0);
root.add(button2, 1, 0);
root.add(button3, 2, 0);
// root في ثاني سطر في الكائن button6 و button5 و button4 هنا قمنا بإضافة الكائنات
root.add(button4, 0, 1);
root.add(button5, 1, 1);
root.add(button6, 2, 1);
// فيها و تحديد حجمها Node كأول root هنا قمنا بإنشاء محتوى النافذة مع تعيين الكائن
Scene scene = new Scene(root, 350, 250);
// هنا وضعنا عنوان للنافذة
stage.setTitle("JavaFX GridPane");
// أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .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.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
public class Main extends Application {
@Override
public void start(Stage stage) {
// في النافذة Root Node و الذي ننوي جعله الـ GridPane هنا قمنا بإنشاء كائن من الكلاس
GridPane root = new GridPane();
// root هنا قمنا بإنشاء جميع الأشياء التي سنضيفها في الكائن
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
Button button5 = new Button("Button 5");
Button button6 = new Button("Button 6");
// و هو كل المساحة المتوفرة بالطول و العرض button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا قمنا بجعل الحجم الأقصى للكائنات
button1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button3.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button4.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button5.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button6.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
// يكبر لأقصى حدود ممكنة button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا جعلنا طول الكائنات
GridPane.setVgrow(button1, Priority.ALWAYS);
GridPane.setVgrow(button2, Priority.ALWAYS);
GridPane.setVgrow(button3, Priority.ALWAYS);
GridPane.setVgrow(button4, Priority.ALWAYS);
GridPane.setVgrow(button5, Priority.ALWAYS);
GridPane.setVgrow(button6, Priority.ALWAYS);
// يكبر لأقصى حدود ممكنة button6 و button5 ,button4 ,button3 ,button2 ,button1 هنا جعلنا عرض الكائنات
GridPane.setHgrow(button1, Priority.ALWAYS);
GridPane.setHgrow(button2, Priority.ALWAYS);
GridPane.setHgrow(button3, Priority.ALWAYS);
GridPane.setHgrow(button4, Priority.ALWAYS);
GridPane.setHgrow(button5, Priority.ALWAYS);
GridPane.setHgrow(button6, Priority.ALWAYS);
// root في أول سطر في الكائن button3 و button2 و button1 هنا قمنا بإضافة الكائنات
root.add(button1, 0, 0);
root.add(button2, 1, 0);
root.add(button3, 2, 0);
// root في ثاني سطر في الكائن button6 و button5 و button4 هنا قمنا بإضافة الكائنات
root.add(button4, 0, 1);
root.add(button5, 1, 1);
root.add(button6, 2, 1);
// فيها و تحديد حجمها Node كأول root هنا قمنا بإنشاء محتوى النافذة مع تعيين الكائن
Scene scene = new Scene(root, 350, 250);
// هنا وضعنا عنوان للنافذة
stage.setTitle("JavaFX GridPane");
// أي وضعنا محتوى النافذة الذي قمنا بإنشائه للنافذة .stage في كائن الـ scene هنا وضعنا كائن الـ
stage.setScene(scene);
// هنا قمنا بإظهار النافذة
stage.show();
}
// هنا قمنا بتشغيل التطبيق
public static void main(String[] args) {
launch(args);
}
}
ستظهر لك النافذة التالية عند التشغيل.