为什么在尝试将按钮添加到GridPane时我收到了一个错误?

huangapple go评论67阅读模式
英文:

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

问题

我在grid.add(button, c, r);这一行遇到了错误,错误信息是“类型GridPane中的方法add(node, int, int)不适用于参数(Button, int, int)”。这个问题看起来很琐碎,但我无法弄清楚出了什么问题。我知道它在说我试图把一个Button放在一个应该是node的位置,但是我查阅了将按钮添加到gridpane的示例,它们使用了完全相同的语法,没有出现任何问题。

抱歉如果之前有人问过/回答过这个问题,我找不到与这个问题相同的任何问题。

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

    @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();
    }
}
英文:

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 &lt; 10; r++) {
		    for (int c = 0; c &lt; 10; c++) {
			    int number = 10 * r + c;
			    Button button = new Button();
			
			    grid.add(button, c, r); 
		    }
	    }

	    stage.show();
    }
}

答案1

得分: 1

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

import javafx.geometry.Insets;
import javafx.scene.control.Button;
英文:

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

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

huangapple
  • 本文由 发表于 2020年9月24日 13:11:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64039917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定