无法单击 HBox 中的按钮
Can't click on buttons in HBox
我无法使用 buttonContainer
HBox
中的按钮,因为我根本无法点击它们,可以说它们是 "stuck"。这发生在多个 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
es 都填充了整个区域,对齐设置将按钮定位在其中。因此,虽然按钮实际上并没有重叠,但 HBox
es 却重叠了。
StackPane
(以及某些情况下的其他窗格)中节点的 z 顺序由 children
列表中的节点顺序决定。所以在你的代码中,buttonContainer
是 "behind" roomContainer
。这意味着鼠标点击要么指向 roomContainer
中的按钮,要么指向 roomContainer
本身。因此,buttonContainer
中的按钮永远不会收到鼠标点击。
解决此问题的方法是使用 "real" 布局容器来适当定位两个按钮容器。例如,您可以使用 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
中工作的概述
我无法使用 buttonContainer
HBox
中的按钮,因为我根本无法点击它们,可以说它们是 "stuck"。这发生在多个 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
es 都填充了整个区域,对齐设置将按钮定位在其中。因此,虽然按钮实际上并没有重叠,但 HBox
es 却重叠了。
StackPane
(以及某些情况下的其他窗格)中节点的 z 顺序由 children
列表中的节点顺序决定。所以在你的代码中,buttonContainer
是 "behind" roomContainer
。这意味着鼠标点击要么指向 roomContainer
中的按钮,要么指向 roomContainer
本身。因此,buttonContainer
中的按钮永远不会收到鼠标点击。
解决此问题的方法是使用 "real" 布局容器来适当定位两个按钮容器。例如,您可以使用 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
中工作的概述