[英]Load html file in jframe
我正在完成一个项目,我有一个简单的html,其中包含一些图像和锚点链接,我想在用户单击帮助菜单按钮时将其显示为帮助文件。 我不知道怎么做。 我尝试了下面的代码,它返回我唯一没有任何滚动的框架,链接不起作用。
pane = new JEditorPane(getClass().getResource("help.html"));//add the html to be shown
pane.setEditable(false);
这有什么简单的方法吗? 请保持简单,因为我是Java的新手。 谢谢!
从路径获取文件getClass().getResource("help.html")
请阅读JAR文件中的打包程序
好吧,因为我无法以其他方式做到这一点,我发现使用默认用户浏览器查看文件更有用。 所以这是我的解决方案:
helpItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported())
{
try
{
desktop = Desktop.getDesktop();
String thehelpfile= String.valueOf(getClass()
.getResource("help.html"));
File fileToOpen=new File(thehelpfile.substring(5));
desktop.open(fileToOpen);
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null,
ex, "Λάθος", JOptionPane.PLAIN_MESSAGE);
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.