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

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

paintComponent method run inside of the JPanel constructor

问题

我是新来的,这实际上是我的第一篇帖子,我还在摸索中。最近我开始学习Java,一直在研究paintComponent方法。我正在寻找一种在主方法中构建的面板上绘图的方法。我已经按照我看到的方法创建了一个扩展JPanel的第二个类,但我很好奇是否可以在不扩展JPanel的情况下完成,并完全在主类中进行。大约7年前,我看到了SantaXL发布的这篇帖子,它运行得很好,但我搞不清楚为什么它有效。我甚至不确定应该用什么术语来查找它。我想知道为什么在这种方式中,如果我在JPanel构造函数内部运行一个方法,为什么会有效。

我感激您能提供的任何帮助。

英文:

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 对象调用父类的paintComponent方法,使类能够像没有重写这个方法一样运行,这允许背景按照应有的方式每帧重新绘制。如果去掉这一行,当你尝试进行动画或更改绘制到面板的内容时会出现问题。
        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.html
匿名

发表评论

匿名网友

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

确定