错误:“The method setDefaultCloseOperation(int) is undefined for the type Frame”

huangapple go评论95阅读模式
英文:

error "The method setDefaultCloseOperation(int) is undefined for the type Frame"

问题

我正在使用Visual Studio Code 2020,但出现了错误:"The method setDefaultCloseOperation(int) is undefined for the type 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) {

    // 创建Graphics快捷方式

    Graphics2D g2d = (Graphics2D)g;

    // 创建新的框架窗口,声明大小

    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();

    /* 下一部分将创建在屏幕上移动的点。
    它将有一个绘制点的循环,还有一个循环
    用于擦除先前的点。 */

    g2d.setColor(new Color(255, 255, 255));

    g2d.fillRect(0, 0, getSize().height-1, getSize().height-1);

}

}

}


我已尝试寻找答案,但找到的答案都没有起作用。

我是一个新手,几天前开始学习Java。我真的不太理解,但我现在正在努力学习图形的工作方式。

英文:

I am using Visual Studio Code 2020, and it is giving me the error "The method setDefaultCloseOperation(int) is undefined for the type Frame"

The problem is line 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);

        }

    }

}

I have tried looking for answers, but none of the ones I found had worked.

I am a noob, and picked up java a few days ago. I don't really understand much, but I am trying to learn how graphics work right now

答案1

得分: 0

错误意味着 setDefaultCloseOperation(int) 不是你的对象 frame 的方法。这可能是因为在 frame 对象中没有这个名称的方法,或者如果有这样的方法,就没有一个接受单个整数参数的方法。

在此查找 Frame 和 JFrame 的 API:
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();

英文:

The error means that the setDefaultCloseOperation(int) is not found as a method of your object frame . This could be because there is no method with that name in the frame object, or if there is such a method, there isn't one that takes a single int argument.

Looking up the API here for Frame and 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 seems to be defined for the JFrame object, and not Frame. JFrame extends Frame, meaning it is a specific kind of frame. Specifically, it is a frame that has the setDefaultOperation() method.

Perhaps change your frame definition to Frame frame = new JFrame();
or JFrame frame = new JFrame();

答案2

得分: 0

你的标签显示你在询问关于一个JFrame,它确实有setDefaultCloseOperation(...)方法。

然而,你的代码正在使用Frame,这是一个AWT组件,不是Swing组件。在Swing中使用JFrame

另外,你的类正在扩展Canvas。对于Swing应用程序,你应该扩展JPanel并覆盖paintComponent()方法。

当你可以使用Swing时,就没有必要使用AWT组件。

> 我现在正在尝试学习图形是如何工作的。

首先,你绝对不应在绘制方法中创建组件。绘制方法只应该使用Graphics对象进行绘制。

阅读Swing教程中关于自定义绘制的部分,其中有一些示例可以帮助你入门。

按照教程中的示例来编写正确结构的代码。你在这里发布的代码大部分是错误的。

英文:

Your tag says you are asking about a JFrame, which does have the setDefaultCloseOperation(...) method.

However, your code is using Frame which is an AWT component, not a Swing component. Use a JFrame for Swing.

Also, you class is extending Canvas. For a Swing application you should be extending JPanel and overriding paintComponent().

There is no need to use AWT components when you can use Swing.

> I am trying to learn how graphics work right now

First of all you should NEVER create a component in a painting method. A painting method should only use the Graphics object to do painting.

Read the section from the Swing tutorial on Custom Painting for working examples to get you started.

Follow the examples from the tutorial for properly structured code. The code you posted here is mostly wrong.

huangapple
  • 本文由 发表于 2020年9月6日 09:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63759930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定