Basic paintComponent not being called by repaint()?

回眸只為那壹抹淺笑 提交于 2019-12-13 04:24:09

问题


I'm using the book Headfirst java, and I have put together a program that I thought would compile fine. 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");
  }
}

回答1:


See my answer to this question . It provides an example of the proper way to set up a JPanel.

Like other commenters/answerers have stated, paintComponent belongs to the JPanel class. What this means for you is that you need to create a class (let's call it MyPanel) that extends JPanel. (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).

Once you have done that, simply cut the paintCOmponent method from your setup class and paste it into your new MyPanel class.

And finally, within your setup class, instead of creating a JPanel object, create a MyPanel.

Basically, this MyPanel object is your own custom JPanel object that does whatever you want it to! 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. Only for this one, you won't override any methods other than the 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 ActionListeners.

Then, in another class (such as your setup class), have a main method that has 1 line: one that instantiates a 'JFrame` object. 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. I am not sure why it is setup that way.



来源:https://stackoverflow.com/questions/17793873/basic-paintcomponent-not-being-called-by-repaint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!