错误 "The method setDefaultCloseOperation(int) is undefined for the type Frame"
error "The method setDefaultCloseOperation(int) is undefined for the type Frame"
我正在使用 Visual Studio Code 2020,它给我错误消息“未定义 setDefaultCloseOperation(int) 方法对于类型 Frame”
问题是第52行,
import java.awt.Frame;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import java.awt.Color;
public class Main {
public static void main(String[] args) {
boolean loop = false;
/* while (loop = true) {
try {
Thread.sleep(2000);
} catch (InterruptedException reallyIgnored) {}
System.out.println("Loop is working.");
} */
}
static class GraphicsEngine extends Component {
public void paint(Graphics g) {
// Creating Graphics Shortcut
Graphics2D g2d = (Graphics2D)g;
// Creating new framw window, declaring size
Frame frame = new Frame();
frame.add(new GraphicsEngine());
int frameWidth = 700;
int frameHeight = 500;
frame.setSize(frameWidth, frameHeight);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
/* Next part will create dot that moves across screen.
It will have a loop that draws the dot, and also a loop
that erases the previous dot. */
g2d.SetColor(new Color(255, 255, 255));
g2d.fillRect(0, 0, getSize().height-1, getSize().height-1);
}
}
}
我尝试寻找答案,但 none 我找到的答案有效。
我是菜鸟,前几天捡到的java。我不太了解,但我现在正在努力学习图形的工作原理
错误意味着找不到 setDefaultCloseOperation(int)
作为对象 frame
的方法。这可能是因为框架对象中没有具有该名称的方法,或者即使有这样的方法,也没有采用单个 int 参数的方法。
在 API 此处查找 Frame 和 JFrame:
https://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html
setDeaultCloseOperation 似乎是为 JFrame 对象而不是 Frame 定义的。 JFrame 扩展了 Frame,这意味着它是一种特定的框架。具体来说,它是一个具有 setDefaultOperation()
方法的框架。
也许将您的框架定义更改为 Frame frame = new JFrame();
或 JFrame frame = new JFrame();
你的标签说你问的是 JFrame,它确实有 setDefaultCloseOperation(...)
方法。
但是,您的代码使用的是 Frame
,它是 AWT 组件,而不是 Swing 组件。对 Swing 使用 JFrame
。
此外,您 class 正在扩展 Canvas
。对于 Swing 应用程序,您应该扩展 JPanel
并覆盖 paintComponent()
.
能用Swing就不用AWT组件了
I am trying to learn how graphics work right now
首先你永远不应该在绘画方法中创建一个组件。一个绘画方法应该只使用Graphics对象来绘画。
阅读 Swing 教程中关于 Custom Painting 的部分以获取工作示例以帮助您入门。
遵循教程中的示例以获得结构正确的代码。您在此处发布的代码大部分是错误的。
我正在使用 Visual Studio Code 2020,它给我错误消息“未定义 setDefaultCloseOperation(int) 方法对于类型 Frame”
问题是第52行,
import java.awt.Frame;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import java.awt.Color;
public class Main {
public static void main(String[] args) {
boolean loop = false;
/* while (loop = true) {
try {
Thread.sleep(2000);
} catch (InterruptedException reallyIgnored) {}
System.out.println("Loop is working.");
} */
}
static class GraphicsEngine extends Component {
public void paint(Graphics g) {
// Creating Graphics Shortcut
Graphics2D g2d = (Graphics2D)g;
// Creating new framw window, declaring size
Frame frame = new Frame();
frame.add(new GraphicsEngine());
int frameWidth = 700;
int frameHeight = 500;
frame.setSize(frameWidth, frameHeight);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
/* Next part will create dot that moves across screen.
It will have a loop that draws the dot, and also a loop
that erases the previous dot. */
g2d.SetColor(new Color(255, 255, 255));
g2d.fillRect(0, 0, getSize().height-1, getSize().height-1);
}
}
}
我尝试寻找答案,但 none 我找到的答案有效。
我是菜鸟,前几天捡到的java。我不太了解,但我现在正在努力学习图形的工作原理
错误意味着找不到 setDefaultCloseOperation(int)
作为对象 frame
的方法。这可能是因为框架对象中没有具有该名称的方法,或者即使有这样的方法,也没有采用单个 int 参数的方法。
在 API 此处查找 Frame 和 JFrame: https://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html
setDeaultCloseOperation 似乎是为 JFrame 对象而不是 Frame 定义的。 JFrame 扩展了 Frame,这意味着它是一种特定的框架。具体来说,它是一个具有 setDefaultOperation()
方法的框架。
也许将您的框架定义更改为 Frame frame = new JFrame();
或 JFrame frame = new JFrame();
你的标签说你问的是 JFrame,它确实有 setDefaultCloseOperation(...)
方法。
但是,您的代码使用的是 Frame
,它是 AWT 组件,而不是 Swing 组件。对 Swing 使用 JFrame
。
此外,您 class 正在扩展 Canvas
。对于 Swing 应用程序,您应该扩展 JPanel
并覆盖 paintComponent()
.
能用Swing就不用AWT组件了
I am trying to learn how graphics work right now
首先你永远不应该在绘画方法中创建一个组件。一个绘画方法应该只使用Graphics对象来绘画。
阅读 Swing 教程中关于 Custom Painting 的部分以获取工作示例以帮助您入门。
遵循教程中的示例以获得结构正确的代码。您在此处发布的代码大部分是错误的。