JavaFX - 下面的标签会影响上面的标签

问题描述 投票:0回答:1

我试图在我目前在不同行的3个标签下面添加一个标签,但是看起来前3个标签下面的第4个标签的宽度实际上推开了上面一行的2个标签,尽管我已经将这个标签设置在不同行。

    mainGrid.add(scanLabel, 45, 25);
    mainGrid.add(imageLabel, 75, 25);
    mainGrid.add(patientLabel, 105, 25);
    mainGrid.add(statusLabel, 45, 30);

下面是上面3个标签的图片,状态标签被注释掉了--。https:/puu.shFSdZda2b4585b69.png。 - 在这里,我把我的标签添加回来,他们被推了出去 - 。https:/puu.shFSesIb0c524fedd.png。

我的代码的其余部分(为了不那么长,我省略了一些)。

public class ScanInterfaceStage {

Stage stage;
Scene scene;

BorderPane mainContent;
BorderPane buttonZone;
BorderPane topZone;
BorderPane leftZone;
BorderPane rightZone;
BorderPane bottomZone;

GridPane mainGrid;
GridPane voiceGrid;

Button backButton;
Button settingsButton;
Button exitButton;
Button voiceButton;
Button voiceConfirmButton;

Label statusLabel;
Label imageLabel;
Label patientLabel;
Label scanLabel;

Image exitImage;
Image backImage;
Image voiceImage;
Image settingsImage;

ImageView exitImageView;
ImageView backImageView;
ImageView voiceImageView;
ImageView settingsImageView;

Alert voicePopUp;

double offsetX;
double offsetY;

public ScanInterfaceStage (double sizeX, double sizeY, double positionX, double positionY) {

    stage = new Stage();

    // Instantiate panes
    mainContent = new BorderPane();
    buttonZone = new BorderPane();
    leftZone = new BorderPane();
    topZone = new BorderPane();
    bottomZone = new BorderPane();
    rightZone = new BorderPane();
    voiceGrid = new GridPane();
    mainGrid = new GridPane();

    // Instantiate Labels
    statusLabel = new Label("");
    imageLabel = new Label("");
    patientLabel = new Label("");
    scanLabel = new Label("");

    // Instantiate buttons
    backButton = new Button("");
    settingsButton = new Button("");
    exitButton = new Button("");
    voiceButton = new Button("");
    voiceConfirmButton = new Button("Done\n\nClick to view");

    // Set button sizes
    exitButton.setMinWidth(103);
    exitButton.setMaxWidth(103);
    exitButton.setPrefWidth(103);
    exitButton.setMinHeight(52);
    exitButton.setMaxHeight(52);
    exitButton.setPrefHeight(52);

    voiceButton.setMinWidth(103);
    voiceButton.setMaxWidth(103);
    voiceButton.setPrefWidth(103);
    voiceButton.setMinHeight(52);
    voiceButton.setMaxHeight(52);
    voiceButton.setPrefHeight(52);

    backButton.setMinWidth(103);
    backButton.setMaxWidth(103);
    backButton.setPrefWidth(103);
    backButton.setMinHeight(52);
    backButton.setMaxHeight(52);
    backButton.setPrefHeight(52);

    settingsButton.setMinWidth(103);
    settingsButton.setMaxWidth(103);
    settingsButton.setPrefWidth(103);
    settingsButton.setMinHeight(52);
    settingsButton.setMaxHeight(52);
    settingsButton.setPrefHeight(52);

    // Set Label sizes
    scanLabel.setMinWidth(250);
    scanLabel.setMaxWidth(250);
    scanLabel.setPrefWidth(250);
    scanLabel.setMinHeight(400);
    scanLabel.setMaxHeight(400);
    scanLabel.setPrefHeight(400);

    imageLabel.setMinWidth(500);
    imageLabel.setMaxWidth(500);
    imageLabel.setPrefWidth(500);
    imageLabel.setMinHeight(350);
    imageLabel.setMaxHeight(350);
    imageLabel.setPrefHeight(350);

    patientLabel.setMinWidth(250);
    patientLabel.setMaxWidth(250);
    patientLabel.setPrefWidth(250);
    patientLabel.setMinHeight(400);
    patientLabel.setMaxHeight(400);
    patientLabel.setPrefHeight(400);

    statusLabel.setMinWidth(800);
    statusLabel.setMaxWidth(800);
    statusLabel.setPrefWidth(800);
    statusLabel.setMinHeight(150);
    statusLabel.setMaxHeight(150);
    statusLabel.setPrefHeight(150);

    // Generate and apply drop shadow
    DropShadow ds = new DropShadow();
    ds.setOffsetY(3.0);
    ds.setOffsetX(3.0);
    ds.setColor(Color.GRAY);

    exitButton.setEffect(ds);
    settingsButton.setEffect(ds);
    backButton.setEffect(ds);
    voiceButton.setEffect(ds);
    topZone.setEffect(ds);

    scanLabel.setEffect(ds);
    imageLabel.setEffect(ds);
    patientLabel.setEffect(ds);
    statusLabel.setEffect(ds);

    // Define stage parameters
    stage.setX(positionX);
    stage.setY(positionY);

    // Set the scene to display mainContent at passed size
    scene = new Scene(mainContent, sizeX, sizeY);

    // Setting up voice alert
    voicePopUp = new Alert(AlertType.INFORMATION);
    voicePopUp.initStyle(StageStyle.UNDECORATED);
    voicePopUp.setGraphic(voiceImageView);
    voicePopUp.setHeaderText("We are now listening!");
    voicePopUp.setHeight(550);
    voicePopUp.setWidth(550);
    voicePopUp.setX(500);
    voicePopUp.setY(500);
    voicePopUp.getDialogPane().setStyle("-fx-background-color: c9f0ff; -fx-font: 25 Open_Sans;");
    voicePopUp.setContentText("AMMS is now listening to your voice! Please clearly state what you would like us to help with!");


    mainContent.setCenter(mainGrid);
    mainContent.setTop(topZone);
    mainContent.setLeft(leftZone);
    mainContent.setRight(rightZone);
    mainContent.setBottom(bottomZone);

    mainGrid.setPadding(new Insets(10, 10, 10, 10));
    mainGrid.setHgap(3);
    mainGrid.setVgap(3);
    Text menuText = new Text("AAMS - Scan Type: X-Ray");
    menuText.setStyle("-fx-font: 25 Open_Sans-Semi-Bold");
    topZone.setCenter(menuText);

    mainGrid.add(scanLabel, 45, 25);
    mainGrid.add(imageLabel, 75, 25);
    mainGrid.add(patientLabel, 105, 25);
    mainGrid.add(statusLabel, 45, 80);
java javafx label gridpane
1个回答
1
投票

网格窗格的工作原理是有索引的行和列,它们分别占用可变的高度和宽度。当你在表格中指定列和行的索引时,你可以在 mainGrid.add(node, columnIndex, rowIndex),这些不是像素值,而是分别是列和行的索引(是基于零的)。

所以

mainGrid.add(scanLabel, 45, 25);
mainGrid.add(imageLabel, 75, 25);
mainGrid.add(patientLabel, 105, 25);
mainGrid.add(statusLabel, 45, 80);

结果是一个有106列81行的网格。几乎所有的行都是空的,所以占用的空间为零。例外的是第25行,包含三个标签,第80行,包含一个标签,第75和105列,各包含一个标签,第45列,包含两个标签。

由于第45列包含两个标签,所以它的大小将使其足够宽,以包含这些标签中最宽的标签。第46-74列都是空的,宽度为零,都位于其右侧,第75列(宽度足以容纳两个标签)。imageLabel),然后是第76-104列(零宽度),最后是第105列(宽度足以容纳病人标签)。

这样创建的布局与

mainGrid.add(scanLabel, 0, 0);
mainGrid.add(imageLabel, 1, 0);
mainGrid.add(patientLabel, 2, 0);
mainGrid.add(statusLabel, 0, 1);

我想你希望状态标签能跨越整个网格的宽度;你可以通过设置它的 columnSpan. 你可能需要这样的东西。

mainGrid.add(scanLabel, 0, 0);
mainGrid.add(imageLabel, 1, 0);
mainGrid.add(patientLabel, 2, 0);
mainGrid.add(statusLabel, 0, 1, 3, 1);

最后一次调用使用重载的 add() 方法,该方法除了指定列和行的索引外,还指定了列跨度和行跨度。

© www.soinside.com 2019 - 2024. All rights reserved.