【问题标题】:Adding buttons to Hbox向 Hbox 添加按钮
【发布时间】:2018-02-05 16:41:04
【问题描述】:

我第一次在 javaFX 中涉足,我在向 Hbox 添加按钮时遇到了一些问题。似乎该框不喜欢添加的类型按钮。不知道为什么,所以只是检查这是否发生在其他人身上。

Button knapp1 = new Button("Alphabetical");
Button knapp2 = new Button("Frequency");
HBox hbox = new HBox();
hbox.getChildren().addAll(knapp1, knapp2);

问题出在“addAll”方法,它给出了错误

List <Node> 类型中的方法addAll(int, Collection&lt;? extends Node&gt; 不适用于参数(Button, Buttons))

提前感谢您提出的愚蠢问题。

【问题讨论】:

  • 检查你有正确的进口。
  • 正如@James_D 指出的,你能检查一下你是否有这个:import javafx.scene.layout.HBox;
  • @Boris 肯定是 Button 导入,而不是 HBox,这可能是问题所在?
  • 你是对的。那么,可能是 java.awt.Button import 而不是 javafx.scene.control.Button 是错误的
  • 我不确定为什么根据编译时错误它认为knapp2Buttons 类型(注意s)...你确定一切都以正确的方式发布吗它出现在你的代码中?

标签: button javafx


【解决方案1】:

这是一个可运行的示例,如 cmets 检查您的导入中所述

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) {
        Button knapp1 = new Button("Alphabetical");
        Button knapp2 = new Button("Frequency");
        HBox hbox = new HBox();
        hbox.getChildren().addAll(knapp1, knapp2);
        Scene scene = new Scene(hbox);
        stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}

使用这些导入运行时

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.awt.*;

我收到与您删除 import java.awt.*; 并替换为 import javafx.scene.control.Button; 相同的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多