`paintComponent` 方法在 JPanel 构造函数内运行。

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

paintComponent method run inside of the JPanel constructor

问题

I'm new to the forum. This is actually my first post, and I'm still figuring things out. I recently started working on learning Java, and I have been working on the paintComponent method. I was looking up a way to paint to a panel that I constructed in the main method. I have done it the way that I have seen where you create a second class that extends JPanel, but I was curious if it can be done without extending JPanel, and completed entirely from the main class. I came across this post by SantaXL about 7 years ago, and it works beautifully, but I can't figure out why it works. I'm not even sure what to call the process to look it up. I would love to know why it's okay to run a method inside the main method if I do it inside the JPanel constructor in this way.

I appreciate any help you can give.

Old Post

I just want to know why this works, and what this style is called.

英文:

I'm new to the forum. This is actually my first post, and I'm still figuring things out. I recently started working on learning Java, and I have been working on the paintComponent method. I was looking up a way to paint to a panel that I constructed in the main method. I have done it the way that I have seen where you create a second class that extends JPanel, but I was curious if it can be done without extending JPanel, and completed entirely from the main class. I came across this post by SantaXL about 7 years ago, and it works beautifully, but I can't figure out why it works. I'm not even sure what to call the process to look it up. I would love to know why it's okay to run a method inside the main method if I do it inside the JPanel constructor in this way.

I appreciate any help you can give.

Old Post

I just want to know why this works, and what this style is called.

答案1

得分: 1

这个代码部分的翻译如下:

所以基本上这里正在创建一个新版本的 JPanel 类,而无需创建一个新类,扩展 JPanel 并为其命名。我已经在下面的代码片段中添加了许多注释,希望能帮助您理解发生了什么。

// 这一行创建了一个新实例,并使用布尔参数设置了 BufferStrategy 为双缓冲(稍后很重要,但现在只需将其视为一个良好的默认设置)。
JPanel panel = new JPanel(true) {
// 这里你正在覆盖 JPanel 类中的内置方法(paintComponent)。覆盖它允许您更改其默认行为。请注意,这要求您使用内置方法的确切名称。
@Override
public void paintComponent(Graphics g) {
// 使用 Graphics 对象的 super.paintComponent(g) 允许该类正常运行,就好像你没有覆盖此方法,这允许背景在每一帧都被重新绘制。如果删除这个部分,当您尝试制作动画或更改要绘制到面板的内容时,会出现问题。
super.paintComponent(g);
// 所有绘制代码都应放在这里。
}
}

通常,这被称为匿名内部类或嵌套类。您可以在这里了解更多关于它们的信息。

英文:

So essentially what's going on here is you are creating a new version of the JPanel class, without having to create a new class, extend the JPanel, and give it a name. I've added a bunch of notes to your code snippet below to hopefully walk you through what's happening.

//This first line creates a new instance, and with the boolean
//parameter, sets the BufferStrategy to double-buffered (important later,
//but for now just think of this as a good default).
JPanel panel = new JPanel(true) {
    //Here you're overriding a built-in method (paintComponent)in the JPanel class. 
    //Overriding it allows you to alter its default behavior. 
    //NOTE that this requires you to use the exact name of the built-in method.
    @Override
    public void paintComponent(Graphics g){
        //supering the paintComponent method with the Graphics object allows
        //the class to function as though you hadn't overridden this mathod,
        //which allows the background to be redrawn as it is supposed to, 
        //every frame. Removing this will cause issues when you try to animate
        //anything or change what is being painted to the panel. 
        super.paintComponent(g);
        //all your paint code would go here. 
    }
}

Typically this is called an anonymous inner class or a nested class. You can read more about them here.

huangapple
  • 本文由 发表于 2023年3月21日 01:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793612-4.html
匿名

发表评论

匿名网友

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

确定