这可能是因为您忘记将Button对象添加到布局对象中。您可以使用以下代码示例创建一个HBox对象并将其中两个Button对象添加到其中:
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 primaryStage) throws Exception{
// 创建两个Button对象
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
// 创建一个HBox对象,将button1和button2添加到其中
HBox hbox = new HBox(button1, button2);
// 创建一个Scene对象并将HBox对象添加到其中
Scene scene = new Scene(hbox, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
请注意,HBox类具有几个构造函数,但是没有接受Button对象的构造函数。相反,您可以在HBox对象中使用add()方法手动将Button对象添加到其中。