奇怪的 HTMLEditorKit 问题
下面的代码片段存在问题,如果在包含小程序窗口的浏览器中按下重新加载按钮,它将无法工作。它在小程序第一次启动时起作用,但在重新加载时不起作用。同样的事情也发生在 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));
}
The code snippet below has the problem, that it will not work, if the reload button is pressed in the browser containing the applet window. It works on the first start of the applet, but not on a reload. The same thing happens in the AppletViewer.
The reason is, that the Text.setText(...) call crashes with a NullPointerException deeply inside the HTMLParser. I already tried to place the setText call into start(), but that did not help.
Do you know any workaround? Thanks for your help. 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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您从哪里复制该代码,但它看起来非常旧。
这不是在向容器添加组件时指定约束的首选方式。阅读 API 以获取推荐的方法。或者阅读 Swing 教程中的“如何使用边框布局”的示例。
不确定您为什么要创建编辑器套件。另外,您的文本不是正确的 HTML(不知道这是否会产生影响)。
我过去只是使用过如下代码:
我还发现使用 JTextPane 然后使用属性(如果需要对文本进行样式化)要容易得多。
Not sure where you copied that code from but it looks awfully old.
That is not the preferred way to specify constraints when adding components to a container. Read the API for the recommended approach. Or read the Swing tutorial on "How to Use a Border Layout" for examples.
Not sure why you are creating an editor kit. Also your text isn't proper HTML (don't know if it makes a difference).
I've just used code like the following in the past:
I also find it much easier to use a JTextPane and then use attributes if you need to stylize the text.