英文:
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 < 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();
}
}
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论