【发布时间】:2016-08-08 13:25:49
【问题描述】:
我正在使用 GUI 制作基于文本的游戏。我正在使用 Javafx 我正在尝试使用网格窗格排列元素。我正在尝试做这样的事情: example.
这是我的代码以及目前的样子
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
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();
Label healthLabel = new Label("Health:");
Label manaLabel = new Label("Mana:");
Label healthDisplay = new Label("100/100");
Label manaDisplay = new Label("100/100");
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(450);
descArea.setWrapText(true);
GridPane.setConstraints(descArea, 0, 0);
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.setPrefHeight(150);
GridPane.setValignment(listView, VPos.BASELINE);
GridPane.setConstraints(listView, 0, 2);
grid.getChildren().addAll(descArea, healthLabel, healthDisplay, manaLabel, manaDisplay, listView);
Scene scene = new Scene(grid, 950, 750);
window.setScene(scene);
window.show();
}
它会像这样显示 example2 我试图在健康标签下移动法力并使列表框更小。网格是正确的方法还是有更好的方法?
谢谢
【问题讨论】: