简体   繁体   English

无法单击HBox中的按钮

[英]Can't click on buttons in HBox

I can't get the buttons in the buttonContainer HBox to work, as in I can't click on them at all, they are "stuck" so to speak. 我无法使buttonContainer HBox的按钮正常工作,因为我根本无法单击它们,可以这么说,它们“卡住了”。 This happens in multiple IDEs so it can't be that. 这发生在多个IDE中,所以事实并非如此。 I don't understand why they are stuck without the ability to click on them, but I'm sure I'm making some kind of basic mistake. 我不明白为什么无法单击它们会卡住它们,但是我确定我犯了一些基本错误。

Here's my simple code: 这是我的简单代码:

public class Game extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    Button attack, run, drinkPotion, nextRoom;
    StackPane root;
    HBox buttonContainer, roomContainer;
    Scene scene;

    public void createNode() {
        root = new StackPane();
        scene = new Scene(root, 860, 640);

        attack = new Button("Attack");
        run = new Button("Run!");
        drinkPotion = new Button("Drink Potion!");
        nextRoom = new Button("Go to next room...");

        buttonContainer = new HBox(12);
        buttonContainer.getChildren().addAll(attack, run, drinkPotion);
        buttonContainer.setAlignment(Pos.BOTTOM_LEFT);

        roomContainer = new HBox();
        roomContainer.getChildren().addAll(nextRoom);
        roomContainer.setAlignment(Pos.BOTTOM_RIGHT);

        root.getChildren().addAll(buttonContainer, roomContainer);
    }

    public void start(Stage stage) {
        createNode();
        stage.setScene(scene);
        stage.show();
    }
} 

In a StackPane , the HBox es are going to be resized to fill the StackPane . StackPane ,将调整HBox大小以填充StackPane So both HBox es here are filling the whole area, with the alignment settings positioning the buttons inside them. 因此,此处的两个HBox都填充了整个区域,对齐设置将按钮放置在其中。 Thus while the buttons don't actually overlap, the HBox es do. 因此,虽然按钮实际上并没有重叠,但是HBox却有重叠。

The z-order of nodes in a StackPane (and other panes, in some circumstances) is determined by the order of the nodes in the children list. StackPane (在某些情况下,以及其他窗格)中节点的z顺序由children列表中节点的顺序确定。 So in your code, buttonContainer is "behind" roomContainer . 因此,在您的代码中, buttonContainer位于roomContainer “后面”。 This means mouse clicks are targeted either to the buttons in roomContainer , or to roomContainer itself. 这意味着鼠标单击既可以指向roomContainer的按钮,也可以roomContainer本身。 Consequently, the buttons in buttonContainer never receive a mouse click. 因此, buttonContainer的按钮将永远不会获得鼠标单击。

A solution to this is to use a "real" layout container to position the two button containers appropriately. 一种解决方案是使用“实际”布局容器来适当地放置两个按钮容器。 For example, you could use an AnchorPane : 例如,您可以使用AnchorPane

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Game extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    Button attack, run, drinkPotion, nextRoom;
    AnchorPane root;
    HBox buttonContainer, roomContainer;
    Scene scene;

    public void createNode() {
        root = new AnchorPane();
        scene = new Scene(root, 860, 640);

        attack = new Button("Attack");
        run = new Button("Run!");
        drinkPotion = new Button("Drink Potion!");
        nextRoom = new Button("Go to next room...");

        buttonContainer = new HBox(12);
        buttonContainer.getChildren().addAll(attack, run, drinkPotion);

        roomContainer = new HBox();
        roomContainer.getChildren().addAll(nextRoom);

        AnchorPane.setBottomAnchor(buttonContainer, 0.0);
        AnchorPane.setLeftAnchor(buttonContainer, 0.0);

        AnchorPane.setBottomAnchor(roomContainer, 0.0);
        AnchorPane.setRightAnchor(roomContainer, 0.0);

        root.getChildren().addAll(roomContainer, buttonContainer);
    }

    @Override
    public void start(Stage stage) {
        createNode();
        stage.setScene(scene);
        stage.show();
    }
}

or possibly a BorderPane : 或可能是BorderPane

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

public class Game extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    Button attack, run, drinkPotion, nextRoom;
    BorderPane root;
    HBox buttonContainer, roomContainer;
    Scene scene;

    public void createNode() {
        root = new BorderPane();
        scene = new Scene(root, 860, 640);

        attack = new Button("Attack");
        run = new Button("Run!");
        drinkPotion = new Button("Drink Potion!");
        nextRoom = new Button("Go to next room...");

        buttonContainer = new HBox(12);
        buttonContainer.getChildren().addAll(attack, run, drinkPotion);
        buttonContainer.setAlignment(Pos.BOTTOM_CENTER);


        roomContainer = new HBox();
        roomContainer.getChildren().addAll(nextRoom);
        roomContainer.setAlignment(Pos.BOTTOM_CENTER);


        root.setLeft(buttonContainer);
        root.setRight(roomContainer);
    }

    @Override
    public void start(Stage stage) {
        createNode();
        stage.setScene(scene);
        stage.show();
    }
}

You can read an overview of how all the layout panes work in the tutorial 您可以在本教程中阅读有关所有布局窗格如何工作的概述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM