2

我正在尝试将我的 JFrame 窗口的形状设置为椭圆,但它却抛出了以下错误:

java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at Splash.setShape(Splash.java:48)
    at Splash.<init>(Splash.java:25)
    at BackOffice.init(BackOffice.java:40)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

问题是我正在发送 2 个参数,而该方法只接受 2 个参数,所以我看不到我从哪里得到这个错误?错误指向的行是mSetWindowShape.invoke(this, shape);这里所说的行是相关方法:

private void setShape() {
    Class<?> awtUtilitiesClass;
    try {
        awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
        Method mSetWindowShape = awtUtilitiesClass.getMethod("setWindowShape", Window.class, Shape.class);
        Shape shape = (Shape) new Ellipse2D.Double(0, 0, getWidth(), getHeight());
        mSetWindowShape.invoke(this, shape);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

}

编辑:我取消了一个参数并得到了同样的错误(参数数量错误)。然后我输入 3 个参数(窗口、形状、0)并得到“参数类型不匹配”。然后我尝试了一个布尔值和一个字符串作为第三个参数,但它们也给出了“参数类型不匹配”。我不明白这一点,因为在教程中它只显示了 2 个参数。现在显然有三个?

4

2 回答 2

3

您的:

mSetWindowShape.invoke(this, shape);

应该:

mSetWindowShape.invoke(null, this, shape);

Method.invoke()方法将调用该方法的对象作为第一个参数。由于 AWTUtilities.setWindowShape() 是静态方法,因此第一个参数应该为 null。

此外,如果您可以针对 Java 7,请改用Frame.setShape(),因为它现在正式成为 API 的一部分。com.sun.* 类将来可能会消失。

于 2011-12-22T21:56:38.647 回答
1

我希望,这是您的代码要求的正确方法,frameObject.setShape(shape); 希望对您有所帮助。问候。

于 2011-12-22T21:48:47.380 回答