我正在开发一个带GUI的小游戏,我的布局有问题。
当我运行它时,我得到了这个:
ListView
正在涵盖TextArea
,我试图让它紧挨着它。就像在这个例子中一样:
这是我到目前为止的代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class test extends Application {
Stage window;
ListView<String> listView;
TextArea descArea = new TextArea();
TextArea actionDescArea = new TextArea();
Label healthLabel = new Label("Health:");
Label manaLabel = new Label("Mana:");
Label healthDisplay = new Label("100/100");
Label manaDisplay = new Label("100/100");
Button actionBtn = new Button("Action");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Game");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
descArea.setPrefWidth(750);
descArea.setPrefHeight(550);
descArea.setWrapText(true);
descArea.setEditable(false);
GridPane.setConstraints(descArea, 0, 0, 1, 2);
GridPane.setConstraints(healthLabel, 1, 0);
GridPane.setValignment(healthLabel, VPos.TOP);
GridPane.setConstraints(healthDisplay, 2, 0);
GridPane.setValignment(healthDisplay, VPos.TOP);
GridPane.setConstraints(manaLabel, 1, 1);
GridPane.setValignment(manaLabel, VPos.TOP);
GridPane.setConstraints(manaDisplay, 2, 1);
GridPane.setValignment(manaDisplay, VPos.TOP);
listView = new ListView<>();
listView.getItems().addAll("Action 1", "Action 2", "Action 3", "Action 4");
listView.setPrefWidth(260);
listView.setMaxWidth(260);
listView.setPrefHeight(150);
GridPane.setConstraints(listView, 0, 2, 1, 1);
actionDescArea.setPrefWidth(100);
actionDescArea.setPrefHeight(150);
actionDescArea.setMaxWidth(100);
actionDescArea.setWrapText(true);
actionDescArea.setEditable(false);
GridPane.setConstraints(actionDescArea, 0, 2, 2, 1);
actionBtn.setPrefWidth(260);
actionBtn.setPrefHeight(60);
GridPane.setConstraints(actionBtn, 0, 3);
grid.getChildren().addAll(descArea, healthLabel, healthDisplay, manaLabel, manaDisplay, listView, actionBtn, actionDescArea);
Scene scene = new Scene(grid, 950, 750);
window.setScene(scene);
window.show();
}
如果有人能指出我正确的方向,那就太好了!
谢谢:)
答案 0 :(得分:1)
很少有事情需要注意:
GridPane.setConstraints(descArea, 0, 0, 1, 2);
此处,TextArea descArea
的colspan为1,其columnIndex为0。
GridPane.setConstraints(listView, 0, 2, 1, 1);
这里,ListView listView
的colspan为1,其中columnIndex为0。
GridPane.setConstraints(actionDescArea, 0, 2, 2, 1);
此处,TextArea actionDescArea
的colspan为2,columnIndex为0。
如果我们要将actionDescArea
放在listView
旁边,那么它的columnIndex应该大于后者的columnIndex。另外,如果我们希望它增长并覆盖descArea
下方的整个宽度,我们需要将Hgrow设置为Priority.Always
并移除maxWidth
GridPane.setConstraints(actionDescArea, 1, 2, 1, 1);
GridPane.setHgrow(actionDescArea, Priority.ALWAYS);
此外,由于listView
和actionDescArea
都应位于descArea
之下,因此其colspan应为2。
GridPane.setConstraints(descArea, 0, 0, 2, 2);
解决方案:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class Test extends Application {
Stage window;
ListView<String> listView;
TextArea descArea = new TextArea();
TextArea actionDescArea = new TextArea();
Label healthLabel = new Label("Health:");
Label manaLabel = new Label("Mana:");
Label healthDisplay = new Label("100/100");
Label manaDisplay = new Label("100/100");
Button actionBtn = new Button("Action");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Game");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
descArea.setPrefWidth(750);
descArea.setPrefHeight(550);
descArea.setWrapText(true);
descArea.setEditable(false);
GridPane.setConstraints(descArea, 0, 0, 2, 2);
GridPane.setConstraints(healthLabel, 2, 0);
GridPane.setValignment(healthLabel, VPos.TOP);
GridPane.setConstraints(healthDisplay, 3, 0);
GridPane.setValignment(healthDisplay, VPos.TOP);
GridPane.setConstraints(manaLabel, 2, 1);
GridPane.setValignment(manaLabel, VPos.TOP);
GridPane.setConstraints(manaDisplay, 3, 1);
GridPane.setValignment(manaDisplay, VPos.TOP);
listView = new ListView<>();
listView.getItems().addAll("Action 1", "Action 2", "Action 3", "Action 4");
listView.setPrefWidth(260);
listView.setMaxWidth(260);
listView.setPrefHeight(150);
GridPane.setConstraints(listView, 0, 2, 1, 1);
actionDescArea.setPrefWidth(100);
actionDescArea.setPrefHeight(150);
actionDescArea.setWrapText(true);
actionDescArea.setEditable(false);
GridPane.setConstraints(actionDescArea, 1, 2, 1, 1);
GridPane.setHgrow(actionDescArea, Priority.ALWAYS);
actionBtn.setPrefWidth(260);
actionBtn.setPrefHeight(60);
GridPane.setConstraints(actionBtn, 0, 3);
grid.getChildren().addAll(descArea, healthLabel, healthDisplay, manaLabel, manaDisplay, listView, actionBtn, actionDescArea);
Scene scene = new Scene(grid, 950, 750);
window.setScene(scene);
window.show();
}
}
输出