- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图在 JEditorPane 中显示可点击链接的列表。这是我的代码:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class GUI extends JFrame{
JEditorPane editorpane=new JEditorPane();
//this is the constructor
GUI(){
JFrame frame=new JFrame("Frame");
frame.add(editorpane);
JScrollPane scroll=new JScrollPane(editorpane);
editorpane.setContentType("text/html");
editorpane.setEditable(false);
editorpane.setText("<html><body>Test <a href='http://www.java.net'>"
+ "www.java.net</a></body></html>");
StyleSheet css = ((HTMLEditorKit)
editorpane.getEditorKit()).getStyleSheet();
Style style = css.getStyle("body");
editorpane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Open browser: " + e.getURL());
}
}
});
frame.setSize(512, 342);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scroll);
frame.show();
}
public void append(String s) {
try{
Document doc = editorpane.getDocument();
doc.insertString(doc.getLength(), "\n", null);
doc.insertString(doc.getLength(), s, null);
}
catch(BadLocationException exc){
}
}
//main method
public static void main(String args[]){
GUI gui=new GUI();
gui.append("<html><body>Test <a href='http://www.java.net'>"
+ "www.java.net</a></body></html>");
}
}
它显示了一个可点击的链接,当我在构造函数中使用 setText() 方法时,但是当我尝试使用 append() 显示附加链接时;它显示 html 标签和文本的方法,它不会使我的 url 成为超链接。任何想法,为什么它不能与 append 一起使用?
最佳答案
使用 HTMLEditorKit 的任一方法
public void insertHTML(HTMLDocument doc, int offset, String html,
int popDepth, int pushDepth,
HTML.Tag insertTag)
或者HTMLDocument的方法
public void insertAfterEnd(Element elem, String htmlText)
public void insertAfterStart(Element elem, String htmlText)
public void insertBeforeStart(Element elem, String htmlText
public void insertBeforeEnd(Element elem, String htmlText)
关于java - 如何在 JEditorPane 中显示可点击的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10495867/
ChatGUI 我使用 2 个 JEditorPane 将文本从一个 Pane 传输到另一个 Pane 。 一旦我传输了数据,我就会执行以下操作: JEditorPane.setText(null);
我试图在我的应用程序中实现聊天功能。我使用了 2 个 JEditorPane。一个用于保存聊天记录,另一个用于将聊天发送到前一个 JEditorPane。 JEditorPane 是 text/h
如何向 JEditorPane 添加建议列表(当用户键入某个单词的一部分以建议以该部分开头的所有标记的列表,例如 Eclipse 中的智能感知)?我已经实现了返回字符串 ArrayList 的方法,但
我已经在网上搜索了一段时间,但我没有找到任何关于它的真实信息......我想知道无论如何可以使用文件完整目录路径打开文件(如java文件) ? 我目前正在使用 FileReader reader =
我试图每 5 秒刷新一次 JEditorPane,如果新数据添加到数据库表中,它应该得到反射(reflect),但它不会重新绘制。 首先我有一个 JFrame,如下所示,在构造函数内调用 public
我想知道是否有一种方式或方法可以让我获取当前显示的 JEditorPane。例如,我有一个 JFrame,可以在其中创建多个选项卡。每当创建选项卡时,都会创建一个新的 JEditorPane 对象,并
我必须编写基于 JEditorPane 的 WebBrowser 应用程序 - 去学校。 我遇到了一点问题,超链接不起作用。 这是我的代码: public class BrowserFrame ext
在 JEditorPane 中,我想搜索一个字符串,我知道 scrollToReference 不起作用,所以我想知道如何迭代 Pane 并查找指定的字符串。 如何迭代 JEditorPane 来查找
我有一个程序将一些 URL 输出到 JEditorPane。我希望 URL 成为超链接。该程序基本上会将 URLS 输出到 JEditorPane,就好像它是日志一样。 我已经得到了一些工作,但它没有
我无法获得 JEditorPane 以将 HTML img 标记呈现为图像。显示的只是一个占位符图形。下面是我的代码。提前致谢。 我看到的: 我的代码: import java.awt.*; impo
public class EditorPane extends javax.swing.JPanel { /** * Creates new form EditorPane */ public E
谁知道如何更改 JEditorPane 的换行符属性? 我无法在我的 JTextPane 文本中找到换行符(它既不是 \n 也不是 \r),所以我可以' 正确计算此文本的行数。我想更改 \n 的换行符
我想知道我们是否可以使用 sethighlighter(new Highlighter()) 方法将两个荧光笔设置为 JEditorpane,其中一个是默认荧光笔,另一个是下划线荧光笔。 最佳答案 R
在下面的代码中,我想在 JEditorPane 上写入 0 到 10000 的数字。但是,JEditorPane 不显示任何内容,除非它完全完成(一次打印所有 0 到 10000)或者操作系统给它时间
我有这个代码: package web.test; import java.awt.Dimension; import java.io.IOException; import javax.swing.
我在使用 JEditorPane 时遇到问题。我想在同一行上左对齐和右对齐文本。 这是我的代码: INFO_AREA = new JEditorPane(); INFO_AREA.setBor
我正在尝试在 JEditorPane 中使用超链接对于某些单词,类似于单击单词以获取其定义的想法。当在编辑器 Pane 中输入单词时,程序会根据列表检查它们并用 some word 替换列出的单词。标
我正在编写一个使用 JEditorPane 制作简单编辑器的程序,它使用超链接允许用户使用简单的超链接监听器在不同页面之间跳转。 问题是我希望能够让用户选择一些文本并将其转换为链接。我发现有很多这样的
我正在用 Java 创建一个 Web 浏览器。在我使用的这个浏览器中 浏览器窗口的 JEditorPane。我正在使用“setPage(String url)”方法 显示页面。浏览器可以显示页面但是有
我正在尝试在 Java JEditorPane 中显示内联图像。下面的代码使用 HTML 内容,可以在 Firefox 中正确显示图像,但不能在 JEditorPane 中显示。任何想法为什么?谢谢。
我是一名优秀的程序员,十分优秀!