下面的代码片段有一个问题,即如果在包含小程序窗口的浏览器中按下重新加载按钮,它将无法工作。它在小程序的第一次启动时起作用,但在重新加载时不起作用。同样的事情也发生在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));
}发布于 2010-11-09 04:30:10
不知道你从哪里复制的代码,但它看起来太旧了。
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,然后再使用属性要容易得多。
https://stackoverflow.com/questions/4127665
复制相似问题