简体   繁体   English

repaint()不调用基本的paintComponent吗?

[英]Basic paintComponent not being called by repaint()?

I'm using the book Headfirst java , and I have put together a program that I thought would compile fine. 我正在使用《 Headfirst java》这本书,并且整理了一个我认为可以很好编译的程序。 But when the window is created, the background or oval isn't showing up. 但是在创建窗口时,背景或椭圆形没有显示出来。

import javax.swing.*;
import java.awt.*;

public class setup {  
  public static void main(String[] args) {    
    JFrame f = new JFrame();
    System.out.println("Created Frame");
    JPanel myJPan = new JPanel();
    System.out.println("Created Panel");

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    f.setSize(300,300);
    System.out.println("Set Size");
    f.setLocationRelativeTo(null);  
    f.setContentPane(myJPan);  
    f.setVisible(true);
    System.out.println("Made Visible");
    myJPan.repaint();
  }


  // @Override  ???
  //  "protected void" ??
  public void paintComponent(Graphics g) {
      // super.paintComponent(); ???
      g.fillRect(0,0,300,300);
      System.out.println("painted");
      int red = (int) (Math.random()*255);
      int green = (int) (Math.random()*255);
      int blue = (int)(Math.random()*255);
      System.out.println("Got Random Colors");
      Color randomColor = new Color(red, green, blue);
      g.setColor(randomColor);
      System.out.println("Set Random Colors");
      g.fillOval(70,70,100,100);
      System.out.println("Filled Oval");
  }
}

See my answer to this question . 请参阅我对这个问题的回答 It provides an example of the proper way to set up a JPanel. 它提供了设置JPanel的正确方法的示例。

Like other commenters/answerers have stated, paintComponent belongs to the JPanel class. 就像其他评论者/答者说的那样, paintComponent属于JPanel类。 What this means for you is that you need to create a class (let's call it MyPanel ) that extends JPanel. 这对您意味着什么,您需要创建一个扩展 JPanel的类(我们称其为MyPanel )。 (Note: you can either make a new .java file for this class if you are in eclipse or make it an inner class, it should work either way). (注意:如果您在eclipse中,则可以为此类创建一个新的.java文件,或者将其设置为内部类,无论哪种方式都可以工作)。

Once you have done that, simply cut the paintCOmponent method from your setup class and paste it into your new MyPanel class. 完成此操作后,只需从setup类中剪切paintCOmponent方法并将其粘贴到新的MyPanel类中即可。

And finally, within your setup class, instead of creating a JPanel object, create a MyPanel . 最后,在您的安装程序类中,而不是创建JPanel对象,而是创建MyPanel

Basically, this MyPanel object is your own custom JPanel object that does whatever you want it to! 基本上,这个MyPanel对象是您自己的自定义JPanel对象,它可以执行您想要的任何操作! It's almost like magic! 几乎就像魔术!

On a side note (this will hopefully help you better format your code in the future), and hopefully more experience Java programmers will agree with me on this, I would also recommend that you create your own custom JFrame object as well. 附带说明(这希望将来可以帮助您更好地格式化代码),并希望有更多Java程序员的经验,对此我也表示同意,我也建议您也创建自己的自定义JFrame对象。 Only for this one, you won't override any methods other than the constructor . 仅此一项,您将不会覆盖constructor方法以外的任何方法。 Instead, in your constructor for this custom JFrame , you will make all of the specifications for the window (such as your setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE and setSize(300,300) calls). This constructor is also where you will instantiate your MyPanel object (as well as any other component objects in your window) and maybe give it a few ActionListener s. 相反,在此自定义JFrame constructor中,您将创建窗口的所有规范(例如setDefaultCloseOperation(JFrame.EXIT_ON_CLOSEsetSize(300,300)调用)。在此constructor中,还将实例化MyPanel对象(如以及窗口中的任何其他组件对象),并可能给它一些ActionListener

Then, in another class (such as your setup class), have a main method that has 1 line: one that instantiates a 'JFrame` object. 然后,在另一个类(例如您的setup类)中,有一个main方法,其中有1行:实例化“ JFrame”对象的行。 This will automagically create the window. 这将自动创建窗口。

Oh and one more vitally import thing: you must call setVisible(true) on your JFrame if you want it to display. 哦,还有一件至关重要的事情:如果要显示它,必须在JFrame上调用setVisible(true) I am not sure why it is setup that way. 我不确定为什么要这样设置。

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

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