البرمجة

بناء حاسبة باستخدام JavaFX

To resolve the error you’re encountering, you need to understand that the setCenter method of BorderPane expects a single Node as an argument, not a 2D array of Button objects. To achieve your goal of using an array of buttons as a single object to place in the center of the pane, you can use a GridPane to layout your buttons in a grid-like manner. Here’s how you can modify your code:

java
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class Calculator extends Application { @Override public void start(Stage primaryStage) { BorderPane pane = new BorderPane(); TextField display = new TextField(); pane.setTop(display); int rows = 4; // Number of rows for the grid int cols = 4; // Number of columns for the grid GridPane grid = new GridPane(); // Create buttons and add them to the grid for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Button button = new Button("Button " + (i * cols + j)); grid.add(button, j, i); // Add button to grid at column j, row i } } pane.setCenter(grid); Scene scene = new Scene(pane, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }

In this code, we use a GridPane to lay out the buttons in a grid. You can adjust the rows and cols variables to change the number of rows and columns in the grid. Each button is added to the grid at a specific column and row using the add method of GridPane. Finally, we set the GridPane as the center of the BorderPane.

المزيد من المعلومات

بالطبع! في تطبيق الحاسبة القياسي الذي تقوم بتطويره، يمكنك استخدام الكود الذي تم توضيحه لإنشاء وعرض شاشة الحاسبة (TextField display) في الجزء العلوي من النافذة باستخدام BorderPane، وإنشاء شبكة من الأزرار باستخدام GridPane وعرضها في الجزء الأوسط من النافذة. يمكنك تعديل الأساليب والمنظمة لتلبية متطلبات تصميم الحاسبة الخاصة بك.

إذا كنت تحتاج إلى مزيد من المساعدة أو إذا كان لديك أي استفسارات أخرى حول تطوير الحاسبة، فلا تتردد في طرحها!

زر الذهاب إلى الأعلى