AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-22357486

Meltryllis's questions

Martin Hope
Meltryllis
Asked: 2025-02-18 22:15:41 +0800 CST

JavaFX中添加新节点后,只能获取旧坐标

  • 6

这是一个显示我的问题的示例代码:

public class TestApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox box = new VBox();
        Button button = new Button("Add a Label at top.");
        button.setOnAction(event -> {
            box.getChildren().addFirst(new Label("Label"));
            // Here I want to compute something about coordinate
            Platform.runLater(() -> System.out.println(button.getBoundsInParent().getMinY()));
        });
        box.getChildren().add(button);
        box.setPrefSize(500, 500);
        Scene scene = new Scene(box);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

点击按钮后,窗口的布局如下:

在此处输入图片描述

然后我想要得到button.getBoundsInParent().getMinY()。

我认为这minY等于标签的高度(这也是我想要得到的)。

但是结果却是-1.399999976158142(不知道为什么不是0,不过和我这次的问题没关系)。

我想问的是:

为什么只能获取到添加标签前的坐标,如何才能获取到 位置的正确坐标System.out.println。

提前感谢您的帮助!🙏

更新:

我已阅读评论。我想要实现的功能是滚动以使节点可见。

button在我的程序中,和之间有很多层容器ScrollPane。所以我尝试编写一个静态方法来处理这个任务。

它如下所示:

public class TestApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox box = new VBox();
        Button button = new Button("Add a Label at top.");
        button.setOnAction(event -> {
            box.getChildren().addFirst(new Label("Label"));
            Platform.runLater(() -> scrollVerticalToVisible(button));
        });
        box.getChildren().add(button);
        box.setPrefSize(500, 500);
        Scene scene = new Scene(new ScrollPane(box));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void scrollVerticalToVisible(Node node) {
        Bounds bounds = node.getBoundsInParent();
        Node container = node;
        while (container != null && !(container instanceof ScrollPane)) {
            bounds = container.localToParent(bounds);
            container = container.getParent();
        }
        if (container == null) {
            return;
        }
        ScrollPane scrollPane = (ScrollPane) container;
        double height = scrollPane.getContent().getBoundsInLocal().getHeight();
        // Same Issue, I cannot get correct y
        double y = bounds.getMaxY() - scrollPane.getViewportBounds().getMinY();
        double viewHeight = scrollPane.getViewportBounds().getHeight();
        System.out.println(y + " " + viewHeight + " " + height);

        scrollPane.setVvalue((y - viewHeight) / (height - viewHeight));
    }

    public static void main(String[] args) {
        launch(args);
    }
}
javafx
  • 1 个回答
  • 56 Views
Martin Hope
Meltryllis
Asked: 2024-12-25 01:50:51 +0800 CST

java 生成的 UTF-16LE 编码的 Desktop.ini 在包含中文字符时不起作用

  • 7

我正在尝试通过 java 创建一个desk.ini文件,为 Windows11 文件夹提供一个备用名称(就像我的文档那样)。

下面是一个简单的代码示例(直接调用运行即可):

@Test
public void createDesktopIni() throws IOException {
    String folderPath = Write a folder path here;
    String iniPath = folderPath + "\\desktop.ini";
    File file = new File(iniPath);
    // delete and recreate if exist
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    // Write content
    String alternativeName = "Alternative Name";
    //alternativeName = "这是一个中文别名";
    OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_16LE);
    oStreamWriter.write("[.ShellClassInfo]\nLocalizedResourceName = " + alternativeName);
    oStreamWriter.flush();
    oStreamWriter.close();
    // Set system attribute
    Files.setAttribute(Paths.get(folderPath), "dos:system", true, LinkOption.NOFOLLOW_LINKS);
    Files.setAttribute(Paths.get(iniPath), "dos:system", true, LinkOption.NOFOLLOW_LINKS);
    Files.setAttribute(Paths.get(iniPath), "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
}

如果我设置一个英文替代名称,desktop.ini就可以正常工作了。(顺便说一下,此时这个文件可以通过 VSCode 正常打开)。

但是,如果我设置一个中文替代名称,例如评论:

alternativeName = "这是一个中文别名";

此时desktop.ini文件无法起作用。

如果我通过VSCode打开它,将显示以下内容: VS Code 错误消息

但是,如果我选择“仍然打开”->“使用 UTF-16 LE 编码重新打开”,然后通过 VS Code 将文件保存为 UTF-16 LE,它就可以再次正常工作。

以下是我尝试过的一些其他编码:

  1. UTF-8 可以工作,但是中文在 Explorer.exe 中会显示乱码。
  2. GBK 可以,而且不会乱码。但我更喜欢用 UTF-16,我看到我的文档文件夹中的desktop.ini也是用 UTF-16 LE。
  3. UTF-16 不起作用,文件将被编码为 UTF-16 BE。
java
  • 1 个回答
  • 50 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve