gpt4 book ai didi

java - 如何在 GridPane 中对齐 ListView 基线

转载 作者:行者123 更新时间:2023-12-02 01:18:45 29 4
gpt4 key购买 nike

我目前正在编写一个 Java-FX 程序,其中包含 GridPane 中的 ListView。我想将 ListView 的第一个项目的基线与 ListView 左侧的标签对齐,但 ListView 最初是空的。它看起来没有正确对齐,但在填充并选择项目时跳转到正确的位置。

我认为,为了计算正确的基线偏移量, ListView 需要在显示之前知道其项目。那么,当它应该与基线对齐时,如何创建一个空 ListView 并动态填充它(通过用户操作)?

您可以使用以下 mcve (Java 8 u221) 重现效果:

import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class ListViewAlignmentTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}

@Override
public void start(Stage stage)
{
ListView<String> listview = new ListView<String>();
listview.setPrefSize(100.0, 100.0);

RowConstraints rc0 = new RowConstraints();
rc0.setValignment(VPos.BASELINE);

RowConstraints rc1 = new RowConstraints();
rc1.setValignment(VPos.BASELINE);

GridPane root = new GridPane();
root.getRowConstraints().add(0, rc0);
root.getRowConstraints().add(1, rc1);
root.setHgap(10);
root.setVgap(10);

root.add(new Label("Some text"), 0, 0);
root.add(new TextField(), 1, 0);
root.add(new Label("Other text"), 0, 1);
root.add(listview, 1, 1);

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

listview.getItems().addAll("String1", "String2");
}
}

这是 GUI 最初的样子:/image/pVWgb.png

选择第一个 ListView 项目后,它看起来是正确的:/image/x9432.png

我希望 ListView 将 BASELINE 与其旁边的标签对齐,但它首先出现在标签下方。

最佳答案

enter image description here

编辑:更新了完整代码。复制它,使其看起来如图所示,/image/x9432.png

下面的代码使其不可移动。

import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class ListViewAlignmentTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}

@Override
public void start(Stage stage)
{
ListView<String> listview = new ListView<String>();
listview.setPrefSize(100.0, 100.0);

RowConstraints rc0 = new RowConstraints();
rc0.setFillHeight(true);

RowConstraints rc1 = new RowConstraints();
rc1.setFillHeight(true);

GridPane root = new GridPane();
root.getRowConstraints().add(0, rc0);
root.getRowConstraints().add(1, rc1);
root.setHgap(10);
root.setVgap(10);

root.add(new Label("Some text"), 0, 0);
root.add(new TextField(), 1, 0);
root.add(new Label("Other text"), 0, 1);
root.add(listview, 1, 1);

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

listview.getItems().addAll("String1", "String2");
}
}

结果,它现在不动了。这应该有帮助。

关于java - 如何在 GridPane 中对齐 ListView 基线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57638859/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com