我有一个 JFrame 对象,我需要支持 Mac OS X 上的两个 JVM 1.5 和 Windows 上的 1.6。在 Windows 上,我需要使用 setIconImages 函数来设置应用程序的多个图标大小,但此函数在 1.5 上不可用。是否可以通过反射在 JFrame 中调用此函数?
Application extends JFrame{
.
.
.
void init(){
//check version
//call setIconImages
}
}
最佳答案
我会做类似以下的事情(不会编译,但应该让你开始):
创建一个名为 FrameUtils 的类并为其提供以下方法:
public static void setIconImages(final java.awt.Window window,
final List<? extends Image> icons)
{
try
{
Method setIconImagesMethod;
setIconImagesMethod = // use reflection to get the setIconImages method.
setIconImagesMethod.invoke(window, icons);
}
catch(final NoSuchMethodException ex)
{
// fall back to the single image method
window.setIconImage(icons.get(0));
}
}
This link shows you how to get the method and call it via reflection.
关于java - 根据 JVM 版本调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1851026/