gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:06:04 25 4
gpt4 key购买 nike

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

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

即使光标可能不会改变状态,当按下鼠标并且 JTextPane 不可编辑时,HyperlinkListeners 将看到一个 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其中光标悬停在超链接上时会发生变化。

enter image description here

关于java - 调用 JEditor.setEditable() 时更改光标悬停在超链接上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6803716/

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