简体   繁体   中英

Why am I receiving an error when trying to add a Button to a GridPane?

I'm getting an error for grid.add(button, c, r); saying "the method add(node, int, int) in the type GridPane is not applicable for the arguments (Button, int, int)". This is such a trivial thing but I cannot figure out what's wrong. I know it's saying I'm trying to put a Button where a node is supposed to be, but I've looked up examples of buttons being added to gridpanes and they have this exact syntax with no issues.

Sorry if this has been asked/answered previously, I couldn't find any question that was the same as this.

public class Demo extends Application {
    public static void main(String[] args) {
        // Nothing
    }

    @Override
    public void start(Stage stage) {
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(3));
        grid.setHgap(10);
        grid.setVgap(10);

        for (int r = 0; r < 10; r++) {
            for (int c = 0; c < 10; c++) {
                int number = 10 * r + c;
                Button button = new Button();
            
                grid.add(button, c, r); 
            }
        }

        stage.show();
    }
}

Maybe, check imports to be sure that you are not importing from java.awt library?

import javafx.geometry.Insets;
import javafx.scene.control.Button;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM