gpt4 book ai didi

java - 奇怪的 HTMLEditorKit 问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:56 24 4
gpt4 key购买 nike

下面的代码片段存在问题,如果在包含小程序窗口的浏览器中按下重新加载按钮,它将无法工作。它在小程序第一次启动时起作用,但在重新加载时不起作用。同样的事情也发生在 AppletViewer 中。

原因是 Text.setText(...) 调用在 HTMLParser 深处发生 NullPointerException 崩溃。我已经尝试将 setText 调用放入 start() 中,但这没有帮助。

你知道有什么解决办法吗?感谢您的帮助。 RG

@Override
public void init()
{
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try
{
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("createGUI didn't successfully complete");
}
}

private void createGUI()
{
((JComponent)this.getContentPane()).setBorder(new CompoundBorder
(BorderFactory.createRaisedBevelBorder(),
new EmptyBorder(5,5,5,5)));

BorderLayout bl=new BorderLayout();
bl.setVgap(5);
setLayout(bl);

Input=new JTextField();
Input.setFont(new Font("arial",Font.PLAIN,14));
add("North",Input);
Input.addActionListener(this);

HTMLEditorKit kit=new HTMLEditorKit();
Text=new JTextPane();
Text.setFont(new Font("arial",Font.PLAIN,14));
Text.setEditorKit(kit);
Text.setText("<p>Test</p>");
Text.setEditable(false);
Text.setBackground(Color.white);
add("Center",new JScrollPane(Text));

}

最佳答案

不确定您从哪里复制该代码,但它看起来非常旧。

add("North",Input); 
add("Center",new JScrollPane(Text));

这不是在向容器添加组件时指定约束的首选方式。阅读 API 以获取推荐的方法。或者阅读 Swing 教程中的“如何使用边框布局”示例。

不确定您为什么要创建编辑器工具包。另外,您的文本不是正确的 HTML(不知道这是否会产生影响)。

我过去刚刚使用过如下代码:

String text = "<html><body>Some text><body></html>";
JEditorPane editor = new JEditorPane("text/html", text);

我还发现,如果您需要对文本进行样式化,那么使用 JTextPane 然后使用属性会更容易。

关于java - 奇怪的 HTMLEditorKit 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4127665/

24 4 0