gpt4 book ai didi

java - 无法点击 HBox 中的按钮

转载 作者:行者123 更新时间:2023-12-02 04:24:51 25 4
gpt4 key购买 nike

我无法让 buttonContainer HBox 中的按钮工作,因为我根本无法点击它们,它们被“卡住”了,所以说话。这种情况在多个 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();
}
}

最佳答案

StackPane 中,HBox 将调整大小以填充 StackPane。因此,这里的两个 HBox 都填充了整个区域,并通过对齐设置将按钮定位在其中。因此,虽然按钮实际上并不重叠,但 HBox 却重叠了。

StackPane(以及某些情况下的其他 Pane )中节点的 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 中阅读有关所有布局 Pane 如何工作的概述。

关于java - 无法点击 HBox 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com