简体   繁体   English

AWT:我正在尝试学习 Java,但无法理解以下程序

[英]AWT: I am trying to learn Java and was unable to understand the following program

  1. What is "new MyCanvas()" in f.add(new MyCanvas()); f.add(new MyCanvas()) 中的“new MyCanvas()”是什么?
  2. How did we get the oval even though the paint() method is not called either in CanvasExample class constructor or in the main() method即使在 CanvasExample 类构造函数或 main() 方法中都没有调用 Paint() 方法,我们是如何得到椭圆的
  3. What is new CanvasExample() in the main() method main() 方法中的新 CanvasExample() 是什么

import java.awt.*;

public class CanvasExample {  
    public CanvasExample() {  
        Frame f = new Frame("Canvas Example");  
        f.add(new MyCanvas());  
        f.setLayout(null);  
        f.setSize(400, 400);  
        f.setVisible(true);  
    }  

    public static void main(String args[]) {  
        new CanvasExample();
    }  
}  

class MyCanvas extends Canvas {  
    public MyCanvas() {  
        setBackground (Color.GRAY);  
        setSize(300, 200);  
    }

    public void paint(Graphics g){  
        g.setColor(Color.red);  
        g.fillOval(75, 75, 150, 75);  
    } 
}

Please explain请解释

  1. It's an instance of Canvas class.它是Canvas类的一个实例。 In Java new is a keyword used to create an object (an instance of given class).在 Java 中new是用于创建对象(给定类的实例)的关键字。 That being said - new Canvas() creates new instance of Canvas class.话虽如此 - new Canvas()创建Canvas类的新实例。 Brackets after class name indicate constructor - block of code that is being called when you wan to create an object of given type.类名后面的括号表示构造函数——当你想创建一个给定类型的对象时被调用的代码块。

  2. You got oval because the paint method was called .因为调用paint方法所以得到了椭圆形。 It just wasn't called explicitly from your code.它只是没有从您的代码中明确调用。 As you can read in this tutorial article published by Oracle , the paint method will be always triggerred as so called "callback mechanism".正如您在Oracle 发布的这篇教程文章中所读到的, paint方法将始终作为所谓的“回调机制”被触发。 This method belongs to Container class.该方法属于Container类。 I suggest reading docs about it .我建议阅读有关它的文档 The paint method in MyCanvas class overrides the paint method from Canvas .paint的方法MyCanvas类覆盖了paint的方法Canvas When extending Canvas class and overriding paint method you should always call the super method at the beginning of your method.当扩展Canvas类并覆盖paint方法时,您应该始终在方法的开头调用 super 方法。 You can read why in the links I have already included in this point.您可以在我已经包含在这一点的链接中了解原因。

  3. As in point #1 - new CanvasExample() creates new instance of MyCanvas class.与第 1 点一样 - new CanvasExample()创建MyCanvas类的新实例。 More specifically, it calls constructor ( public CanvasExample() { ... } ).更具体地说,它调用构造函数( public CanvasExample() { ... } )。 In code you presented, constructor of CanvasExample creates new object of type Frame and calls some of it's methods.在您提供的代码中, CanvasExample 的构造函数创建了Frame类型的新对象并调用了其中的一些方法。 One of these methods is add and it was inherited by class Frame from its the superclass - Container .其中一种方法是add ,它是由Frame类从其超类Container继承的。
    As Java awt API explains, the add method:正如Java awt API 所解释的, add方法:

    appends the specified component to the end of this container.将指定的组件附加到此容器的末尾。

I hope you will understand :D我希望你会明白:D

at first it call public static void main(String args[])首先它调用public static void main(String args[])
then main make new instance of class CanvasExample and call its constructor CanvasExample()然后main创建类CanvasExample新实例并调用其构造函数CanvasExample()

that constructor make new instance of class Frame named "f" and call constructor of class Frame it set title of that frame to "Canvas Example"该构造函数创建名为“f”的类Frame新实例并调用类Frame构造函数,它将该框架的标题设置为“Canvas Example”
in next row of constructor CanvasExample() it make and add new instance of class MyCanvas to instance of Frame named "f"在构造函数CanvasExample()下一行中,它创建并将类MyCanvas新实例添加到名为“f”的Frame实例中
third row of constructor CanvasExample() it just set Layout of "f" to null构造函数CanvasExample()第三行它只是将“f”的布局设置为null
fourth row of constructor CanvasExample() it set size of "f" to 400x400构造函数CanvasExample()第四行将“f”的大小设置为 400x400
fifth row of constructor CanvasExample() it just show "f" to screen (so you can see it)构造函数CanvasExample()第五行它只​​是在屏幕上显示“f”(所以你可以看到它)
and end of constructor CanvasExample() it get back to main并在构造函数CanvasExample()结束时返回main

when creating new instance of class MyCanvas is called constructor of it and that constructor set background of it to "Color.GRAY" and size of it to 300x200 and all of MyCanvas() constructor创建类MyCanvas新实例时称为它的构造函数,该构造函数将其背景设置为“Color.GRAY”,并将其大小设置为MyCanvas()和所有MyCanvas()构造函数

everytime when "f" needs to be rendered again it call method paint(Graphics g) of instance of class MyCanvas bit that instance is smaller than "f" so you can see white behind每次需要再次渲染“f”时,它都会调用MyCanvas类实例的方法paint(Graphics g) ,该实例小于“f”,因此您可以看到后面的白色
that white is background of "f"那个白色是“f”的背景

I think thats all what you ask for.我想这就是你所要求的。
have a nice day祝你今天过得愉快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM