gpt4 book ai didi

java - awt 框架构造函数不接受 GraphicsConfiguration 类的命名

转载 作者:行者123 更新时间:2023-12-01 12:13:04 25 4
gpt4 key购买 nike

我刚刚开始我的 Java 事业,并且刚刚读完 Oracle 网站上的学习 Java 部分。所以我只是浏览几个包看看是什么。

所以我查看了 awt 包,我假设它是某种图形包?

无论如何,我尝试使用以下内容创建一个框架:

import java.awt.*;

class WindowTest{
public static void main(String[] args){
Frame f = new Frame(GraphicsConfiguration gc);
Rectangle bounds = gc.getBounds();
f.setLocation(10 + bounds.x, 10 + bounds.y);
}
}

当我尝试编译时收到编译错误,如下所示:

main.java:5: error: ')' expected
Frame f = new Frame(GraphicsConfiguration gc);
^
main.java:5: error: illegal start of expression
Frame f = new Frame(GraphicsConfiguration gc);
^
2 errors

我知道我无法实例化 GraphicsConfiguration,因为它是一个抽象类,并且我无法使用以下方式初始化它:

GraphicsConfiguration[] gc = GraphicsDevice.getConfiguration();

因为框架不接受 GraphicsConfiguration[] 作为构造函数。

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

当您调用方法或构造函数时,您传递参数 - 值 - 您不像声明方法或构造函数那样声明参数。

所以它应该是这样的:

GraphicsConfiguration gc = ...; // Whatever you need to get a value
Frame f = new Frame(gc);

请注意,这与 AWT 无关。这只是调用方法或构造函数的基本语法。例如:

public class Test {
public static void main(String[] args) {
someMethod(10); // Fine; uses an integer literal
int a = 10;
someMethod(a); // Fine; uses the value of a variable
someMethod(int b); // Invalid syntax
}

public static void someMethod(int x) {
System.out.println(x);
}
}

在这种特定情况下,除非您要指定的特定GraphicsConfiguration,否则只需调用无参数构造函数:

Frame f = new Frame();

关于java - awt 框架构造函数不接受 GraphicsConfiguration 类的命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27174110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com