Javafx 如何在堆栈窗格中插入两个按钮?

Javafx 如何在堆栈窗格中插入两个按钮?,javafx,Javafx,我不熟悉JavaFx,我有一个非常简单的问题,但我尝试了所有方法,但是第二个按钮没有显示,请帮助我 public class Main extends Application implements EventHandler<ActionEvent> { Button button; Button button1; @Override public void start(Stage primaryStage) throws Exception{ button=new But

我不熟悉
JavaFx
,我有一个非常简单的问题,但我尝试了所有方法,但是第二个
按钮没有显示,请帮助我

public class Main extends Application implements EventHandler<ActionEvent> {

Button button;
Button button1;

@Override
public void start(Stage primaryStage) throws Exception{
    button=new Button();
    button.setText("Click Me");


    button.setOnAction(this);

    Button button1 = new Button();
    button1.setText("Don't click me");

    button1.setOnAction(this);

    StackPane layout=new StackPane();
    layout.getChildren().addAll(button,button1);


    Scene scene = new Scene(layout,1000,250);
    primaryStage.setScene(scene);
    primaryStage.show();



}


public static void main(String[] args) {

    launch(args);
}

@Override
public void handle(ActionEvent event) {
    if (event.getSource()==button){
        System.out.println("you clicked me;;;;;");
    }
    if (event.getSource()==button1){
        System.out.println("I asked not to click me;;;;");
    }
    }
}
public类主扩展应用程序实现EventHandler{
按钮;
按钮1;
@凌驾
public void start(Stage primaryStage)引发异常{
按钮=新按钮();
button.setText(“单击我”);
按钮。设置动作(此);
Button button1=新按钮();
按钮1.setText(“不要点击我”);
按钮1.设置动作(本);
StackPane布局=新建StackPane();
layout.getChildren().addAll(按钮,按钮1);
场景=新场景(布局,1000250);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效句柄(ActionEvent事件){
if(event.getSource()==按钮){
System.out.println(“您单击了我;”);
}
if(event.getSource()==button1){
System.out.println(“我要求不要单击我;;”);
}
}
}

我只看到一个
按钮(只有“button1”)。

评论是正确的,因为
堆栈窗格
只会在这里显示第二个按钮,如果它没有使用,而没有提供垂直和水平布局组件,例如
HBox
。下面是一个有助于理解这一点的示例

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonsApp extends Application {

    Button button1;
    Button button2;
    Button button3;
    Button button4;

    @Override
    public void start(Stage primaryStage) throws Exception {

        button1 = new Button("Button1");
        button2 = new Button("Button2");
        button3 = new Button("Button3");
        button4 = new Button("Button4");

        // these two buttons will be stacked one 
        // on top of the other because that is 
        // the purpose of a stackpane which means
        // you will only see the top one (button2)
        var topStackPane = new StackPane(
            button1,
            button2
        );

        // these two buttons are wrapped in a HBox
        // which fills in it's nodes horizontally,
        // side by side from left to right.
        var bottomStackPane = new StackPane(new HBox(
            button3,
            button4
        ));

        topStackPane.setPrefSize(200, 190);
        bottomStackPane.setPrefSize(200, 190);
        var vbox = new VBox(
            topStackPane,
            new Separator(Orientation.HORIZONTAL),
            bottomStackPane
        );

        primaryStage.setScene(new Scene(vbox, 200, 200));
        primaryStage.show();
    }

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

以下代码是您发布的代码的修改版本<代码>堆栈窗格
将其包含的组件堆叠在彼此的顶部。因此,
button1
出现在
按钮的顶部,因为
button1
是最后添加的。由于两个按钮的尺寸相同,因此只能看到
按钮1
。在下面的代码中,
按钮
的尺寸被更改,额外的文本被添加到
按钮1
,使其比
按钮
更宽,从而允许您看到
按钮1
出现在
按钮
的顶部,并且使用户可以轻松地在
按钮
按钮1
上单击鼠标

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Buttons2 extends Application implements EventHandler<ActionEvent> {
    Button button;
    Button button1;

    @Override
    public void start(Stage primaryStage) throws Exception {
        button = new Button();
        button.setText("Click Me");
        button.setMinSize(80.0d, 50.0d);
        button.setOnAction(this);

        button1 = new Button();
        button1.setText("Don't you dare click me");
        button1.setOnAction(this);

        StackPane layout = new StackPane();
        layout.getChildren().addAll(button, button1);

        Scene scene = new Scene(layout, 1000, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    @Override
    public void handle(ActionEvent event) {
        if (event.getSource() == button) {
            System.out.println("you clicked me;;;;;");
        }
        if (event.getSource() == button1) {
            System.out.println("I asked not to click me;;;;");
        }
    }
}
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;

公共类Buttons2扩展应用程序实现EventHandler

ButtonButton1=new Button()应该是
button1=新按钮()仍然只显示按钮1。当给出布局时,我需要给出两个单独的布局吗?我认为两个按钮位于同一位置,并且它们重叠。您使用的是
StackPane
,因此它是“堆叠”的,它的子项彼此重叠。除非您单独修改子级的布局参数,
StackPane
对所有子级使用相同的对齐方式。在这种情况下,所有子项都集中在
堆栈窗格中,因为最上面的子项是最大的子项,所以您只能看到其中一个。交换孩子们的顺序你应该看到两个。。。