Java:JEditorPane 包裹在 JScrollPane 内的奇怪行为

发布于 2024-10-19 10:46:41 字数 2784 浏览 1 评论 0原文

我正在尝试为我的软件构建一个简单的帮助系统。
由 JEditorPane(加载 HTML 文件)构建的帮助系统包装在 JScrollPane 内,同一窗口内有一个 JLabel。
当用户将鼠标移动到 JEditorPane 上的特定单词上时,JLabel 中会显示更多说明。

我成功做到了,但问题是,由于某种原因它只在文本的开头工作。(HTML 文件很长,必须滚动...)
当我向下滚动页面并将鼠标悬停在一个单词上后,它向我抛出 BadLocationException

在下面的代码中,有一个 JEditorPane 包裹在 JScrollPane 中。
当用户移动鼠标时,它会打印鼠标指向的当前字母。(在帮助系统上,我通过该位置找到单词的值,并根据它向 JLabel 打印解释)< br> 但是,正如我所说,它仅适用于文本的开头。
为什么?



import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;

public class JEditorPaneTestApp extends JFrame {

    private JEditorPane editorPan;
    private JScrollPane scrollPan;

    public JEditorPaneTestApp() {
        super();
        try {
            editorPan = new javax.swing.JEditorPane("file:///path/toHTML/file/helpFile.html");
        } 
        catch (IOException e) {e.printStackTrace();}

        scrollPan = new JScrollPane(editorPan);

        this.add(scrollPan);

        editorPan.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                    public void mouseMoved(java.awt.event.MouseEvent evt) {
                        Point p = new Point(evt.getX(), evt.getY());
                        int pos = editorPan.viewToModel(p);
                        try {
                        System.out.println(editorPan.getText(pos--, pos).charAt(0)); 
                        }
                        catch (BadLocationException e1) {
                            System.out.println("Invalid location");/* e1.printStackTrace();*/
                        }
                    }
                });
        scrollPan.setViewportView(editorPan);
        this.add(scrollPan);

        //
        this.getContentPane().setLayout(new LayoutManager() {
            @Override public Dimension preferredLayoutSize(Container arg0) {return null;} 
            @Override public Dimension minimumLayoutSize(Container arg0) {return null;}
            @Override public void removeLayoutComponent(Component arg0) {}
            @Override public void addLayoutComponent(String arg0, Component arg1) {}
            @Override public void layoutContainer(Container conter) {
                scrollPan.setBounds(0, 0, conter.getWidth(),  conter.getHeight());
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        JEditorPaneTestApp test = new JEditorPaneTestApp();
    }
}


谢谢

I'm trying to build a simple help system to my software.
The help system built from JEditorPane(Loaded with HTML file) wrapped inside of JScrollPane, inside of the same window there is a JLabel.
When the user move the mouse over the JEditorPane on a specific word - more explanations appear in the JLabel.

I succeed doing it, but the problem is, that for some reason it work just on the beginning of the text.(the HTML file is long and must be scrolled...)
After i scroll down the page and hover over a word, it throw me BadLocationException.

On the code below there is a JEditorPane wrapped inside JScrollPane.
When the user move the mouse it print the current letter which the mouse point on.(on the help system i find the value of the word by this position and print explanations to the JLabel according to it)
But, as i said it work just on the beginning of the text.
Why ?



import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;

public class JEditorPaneTestApp extends JFrame {

    private JEditorPane editorPan;
    private JScrollPane scrollPan;

    public JEditorPaneTestApp() {
        super();
        try {
            editorPan = new javax.swing.JEditorPane("file:///path/toHTML/file/helpFile.html");
        } 
        catch (IOException e) {e.printStackTrace();}

        scrollPan = new JScrollPane(editorPan);

        this.add(scrollPan);

        editorPan.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                    public void mouseMoved(java.awt.event.MouseEvent evt) {
                        Point p = new Point(evt.getX(), evt.getY());
                        int pos = editorPan.viewToModel(p);
                        try {
                        System.out.println(editorPan.getText(pos--, pos).charAt(0)); 
                        }
                        catch (BadLocationException e1) {
                            System.out.println("Invalid location");/* e1.printStackTrace();*/
                        }
                    }
                });
        scrollPan.setViewportView(editorPan);
        this.add(scrollPan);

        //
        this.getContentPane().setLayout(new LayoutManager() {
            @Override public Dimension preferredLayoutSize(Container arg0) {return null;} 
            @Override public Dimension minimumLayoutSize(Container arg0) {return null;}
            @Override public void removeLayoutComponent(Component arg0) {}
            @Override public void addLayoutComponent(String arg0, Component arg1) {}
            @Override public void layoutContainer(Container conter) {
                scrollPan.setBounds(0, 0, conter.getWidth(),  conter.getHeight());
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        JEditorPaneTestApp test = new JEditorPaneTestApp();
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-10-26 10:46:41
System.out.println(editorPan.getText(pos--, pos).charAt(0));

应该是:

System.out.println(editorPan.getText(pos--, 1).charAt(0));
System.out.println(editorPan.getText(pos--, pos).charAt(0));

should be:

System.out.println(editorPan.getText(pos--, 1).charAt(0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文