覆盖paint()方法 [英] Overriding the paint() method

查看:172
本文介绍了覆盖paint()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个初学者的问题,我希望有Java经验的人可以为我提供帮助.目前正在介绍面向Java的OOP课程.我的讲师目前正在讲解awt和swing,特别是需要重写paint方法,以便在调整窗口大小时重新绘制图形等.我喜欢做尽可能多的外部阅读,而我担心的是我自己的示例教授给出的内容涉及我所阅读的不是最佳做法.达到目的...

I've got a beginner question here that I was hoping someone with some Java experience could help me with. Currently taking an intro to OOP course focused on Java. My instructor is currently covering awt and swing, specifically the need to override the paint method so that graphics are redrawn when a window is resized, etc. I like to do as much outside reading as possible, and my concern is that the examples that my professor gives involve things that I've read are not best practices. To get to the point...

我知道有必要重写paint方法,但是我不知道这样做的最佳方法.我教授的例子都类似于以下内容:

I understand that it's necessary to override the paint method, but I don't know the best way to do so. My professor's examples are all similar to the following:

class Example extends JFrame {
  public void paint(Graphics g) {
    super.paint(g);
    g.drawString("Blah, blah");
  }

  public static void main(String[] args) {
    Example a = new Example();
    a.setDefaultCl...
    \\Etc...
  }
}

这让我感到困扰,因为在与我的主要方法相同的类中包含GUI的所有内容似乎并不正确.另外,我在这里的另一个线程上读到,您不应该扩展JFrame,但是没有关于原因的解释.我的解决方案是创建一个处理gui的类,并在构造函数中实例化JFrame.但是,除非我弄错了,否则不会让我覆盖paint方法.我不得不扩展JFrame以允许我覆盖绘画,但是我再次读到那是错误的事情.

This bothers me because it doesn't seem right to include everything for the GUI in the same class as my main method. Also, I read on a different thread here that you shouldn't extend JFrame, but there wasn't an explanation as to why. My solution was to create a class handling the gui and instantiate JFrame within the constructor. However, unless I'm mistaken, doing so won't let me override the paint method. I feel compelled to extend JFrame to allow me to override paint, but again I read that was the wrong thing to do.

任何帮助都将由衷地感谢,我知道我可以根据他所拥有的代码来建模我的代码,但我真的很想了解这一点,并知道处理它的最佳方法.

Any help would be sincerely appreciated, I know I can just model my code off of what he has but I really want to understand this and know the best way to handle it.

推荐答案

我了解有必要覆盖绘画方法

I understand that it's necessary to override the paint method

不,您不应该覆盖paint()方法.

No you should not override the paint() method.

您应该覆盖JPanel的paintComponent()方法,然后将面板添加到框架中.

You should override the paintComponent() method of a JPanel and then add the panel to the frame.

自定义绘画上,从Swing教程中阅读本节.有关更多信息和工作示例.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

本教程还将向您展示如何更好地构建代码结构,以便在事件调度线程(EDT)上创建GUI.本教程的Concurrency部分也将对此进行解释,以解释其重要性.

The tutorial will also show you how to better structure your code so that the GUI is created on the Event Dispatch Thread (EDT). The tutorial also has a section on Concurrency which will explain why this is important.

这篇关于覆盖paint()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆