JavaFX — метка ниже влияет на метки выше

Я пытаюсь добавить метку под 3 метки, которые у меня сейчас есть в другой строке, однако похоже, что ширина четвертой метки, которая ниже трех верхних, фактически отталкивает 2 метки в строке выше, хотя я установил эта метка должна быть в другой строке.

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

Вот изображение трех первых ярлыков с закомментированным статусомLabel - https://puu.sh/FSdZd/a2b4585b69.png — и здесь я снова добавляю свою метку, и они вытесняются — https://puu.sh/FSesI/b0c524fedd.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);

person Ben Whitely    schedule 03.06.2020    source источник


Ответы (1)


Панель сетки работает, имея индексированные строки и столбцы, которые занимают переменную высоту и ширину соответственно. Когда вы указываете индексы столбцов и строк в mainGrid.add(node, columnIndex, rowIndex), это не значения пикселей, а индексы столбцов и строк соответственно (которые отсчитываются от нуля).

So

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

приводит к сетке со 106 столбцами и 81 строкой. Почти все они пусты, поэтому не занимают места. Исключение составляют строка 25, содержащая три метки, и строка 80, содержащая 1 метку, и столбцы 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(), который указывает диапазон столбцов и диапазон строк в дополнение к индексам столбцов и строк.

person James_D    schedule 03.06.2020
comment
Отлично, этот перегруженный метод определенно сработал для меня. Ценю вашу помощь - person Ben Whitely; 03.06.2020