java – 调用JEditor.setEditable()时更改光标以悬停在超链接上

我有一个包含超链接的JTextPane.当它是可编辑的时,超链接不可点击,并且当鼠标悬停在它们上时光标不会改变状态(例如,到手).如果它不可编辑,则可以单击超链接,并且当鼠标悬停在超链接上时,光标会改变状态.完善.

这是问题,如果光标在其可编辑性更改时已经悬停在JTextPane上,则光标不会更新.光标更新的唯一方法(我知道)是移动它.

即使光标可能不会改变状态,当按下鼠标并且JTextPane不可编辑时,HyperlinkListener将看到ACTIVATED事件.有没有办法强制光标重新评估它应该处于什么状态并在我更改其下面的面板状态时自行更新?

基本上,我希望光标图标始终对应于按下鼠标时会发生的情况.是的,这是一个边界情况,但它仍然很烦人.

以下是一些示例代码.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Every five seconds the editability of the JTextPane is changed. If the
 * editability changes to false when the mouse is hovering over the hyperlink,
 * the cursor will not change to a hand until it is moved off and then back
 * onto the hyperlink. If the editability changes to true when the mouse is
 * hovering over the hyperlink, the cursor will not change back to normal
 * until it is moved slightly.
 * 
 * I want the cursor to update without having to move the mouse when it's
 * already hovering over a hyperlink.
 */
@SuppressWarnings("serial")
public class HyperlinkCursorProblem extends JFrame {

  public HyperlinkCursorProblem(String title) {
    super(title);

    Container pane = getContentPane();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

    final JLabel editabilityLabel = new JLabel("Editable");
    pane.add(editabilityLabel);

    final JTextPane textPane = new JTextPane();
    textPane.setContentType("text/html");

    StringBuilder text = new StringBuilder();
    text.append("<html><body>");
    text.append("<a href=\"http://www.example.com/\">");
    text.append("Click here for fun examples.");
    text.append("</a>");
    text.append("</body></html>");
    textPane.setText(text.toString());
    textPane.addHyperlinkListener(new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          System.out.println("Going to " + e.getURL());
        }
      }
    });
    textPane.setPreferredSize(new Dimension(500, 250));
    pane.add(textPane);

    new Thread(new Runnable() {
      public void run() {
        while (true) {
          try {
            Thread.sleep(5000);

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    boolean editable = !textPane.isEditable();
                    textPane.setEditable(editable);
                    editabilityLabel.setText(editable ? "Editable" : "Not Editable");
                }
            });
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }).start();
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new HyperlinkCursorProblem("Large File Mover");

        // Display the window.
        frame.setVisible(true);
        frame.pack();
      }
    });
  }
}

解决方法:

阅读Processing hyperlinks in editable JEditorPane with HTMLEditorKit.

其中的代码将生成一个可编辑的JTextPane,当鼠标悬停在超链接上时光标会发生变化.

上一篇:python游戏开发:pygame事件与设备轮询


下一篇:linux – 鼠标点击无法在Ubuntu 16.04 LTS中运行