Java awt框架构造函数不接受GraphicsConfiguration类的命名
我刚刚开始我的Java奋进,刚刚读完oracle网站上的Java学习部分。所以我只是浏览了一些包,看看是什么 所以我看了一下awt包,我假设它是某种图形包 无论如何,我尝试使用以下方法创建一个框架:Java awt框架构造函数不接受GraphicsConfiguration类的命名,java,awt,naming,Java,Awt,Naming,我刚刚开始我的Java奋进,刚刚读完oracle网站上的Java学习部分。所以我只是浏览了一些包,看看是什么 所以我看了一下awt包,我假设它是某种图形包 无论如何,我尝试使用以下方法创建一个框架: import java.awt.*; class WindowTest{ public static void main(String[] args){ Frame f = new Frame(GraphicsConfiguration gc);
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
我在尝试编译时收到一个编译错误,如下所示:
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();
啊,所以在文档中,它只是让我知道必须输入的参数类型。我想我只是把自己弄糊涂了。谢谢。@DominicStore:是的,以及在方法本身中使用的参数名称。