gpt4 book ai didi

java - 在 JeditorPane 中查找文本结尾的 X 和 Y 坐标

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

我已经在我正在处理的文本编辑器项目中创建了一个弹出列表作为自动完成功能,并且在 JeditorPane 组件中键入文本时设置此弹出列表的位置时遇到问题

这是我的代码(现在列表在框架底部打开):

    private void showPopUpWindow() {
autoSuggestionPopUpWindow.getContentPane().add(suggestionsPanel);
autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
autoSuggestionPopUpWindow.setSize(tW, tH);
autoSuggestionPopUpWindow.setVisible(true);

int windowX =0;
int windowY =0;

windowX = container.getX() + textField.getX() + 5;
if (suggestionsPanel.getHeight() > autoSuggestionPopUpWindow.getMinimumSize().height) {
windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getMinimumSize().height;
} else {
windowY = container.getY() + textField.getY() + textField.getHeight() + autoSuggestionPopUpWindow.getHeight();
}


autoSuggestionPopUpWindow.setLocation(windowX, windowY);
autoSuggestionPopUpWindow.setMinimumSize(new Dimension(textField.getWidth(), 30));
autoSuggestionPopUpWindow.revalidate();
autoSuggestionPopUpWindow.repaint();

}

对象“autoSuggestionPopUpWindow”是 JWindow 类的实例

我怎样才能找到这个点?

最佳答案

不要使用 JEditorPane。 JEdi​​torPane 最适合显示简单的 HTML。

相反,我建议您使用 JTextPane 来显示格式化文本,或使用 JTextArea 来显示纯文本。

您可以根据插入符号的位置显示弹出窗口:

int offset = textComponent.getCaretPostion();

然后您将获得代表文本组件中插入符号位置的矩形:

Rectangle location = textComponent.modelToView(offset);

然后就可以根据矩形来设置窗口的位置了:

window.setLocation(location.x, location.y + location.height);

关于java - 在 JeditorPane 中查找文本结尾的 X 和 Y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746624/

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