无法点击HBox中的按钮

时间:2015-08-31 17:58:08

标签: java javafx hbox

我无法使buttonContainer HBox中的按钮正常工作,因为我根本无法点击它们,它们会被卡住&#34 ;可以这么说。这种情况发生在多个IDE中,因此无法实现。我不明白为什么他们被卡住而没有点击它们的能力,但我确定我犯了一些基本的错误。

这是我的简单代码:

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();
    }
} 

1 个答案:

答案 0 :(得分:2)

StackPane中,HBox es将调整大小以填充StackPane。因此,HBox这两个都填充整个区域,对齐设置将按钮定位在其中。因此,虽然按钮实际上没有重叠,但HBox可以。

StackPane(以及某些情况下的其他窗格)中节点的z顺序由children列表中节点的顺序决定。因此,在您的代码中,buttonContainer“落后”roomContainer。这意味着鼠标点击可以定位到roomContainer中的按钮,也可以定位到roomContainer本身。因此,buttonContainer中的按钮永远不会收到鼠标点击。

对此的解决方案是使用“真实”布局容器来适当地定位两个按钮容器。例如,您可以使用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();
    }
}

或可能是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();
    }
}

您可以阅读tutorial

中所有布局窗格的工作原理概述