基本paintComponent不被repaint()调用? [英] Basic paintComponent not being called by repaint()?

查看:82
本文介绍了基本paintComponent不被repaint()调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这本书 Headfirst java ,并且我编写了一个我认为能编译好的程序。但是,当窗口创建时,背景或椭圆形不显示。

  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(绘制);
int red =(int)(Math.random()* 255);
int green =(int)(Math.random()* 255);
int blue =(int)(Math.random()* 255);
System.out.println(Got Random Colors);
颜色randomColor =新颜色(红色,绿色,蓝色);
g.setColor(randomColor);
System.out.println(设置随机颜色);
g.fillOval(70,70,100,100);
System.out.println(Filled Oval);


$ / code>


解决方案

我的答案是这个问题。它提供了一个设置JPanel的正确方法的例子。



与其他评论者/回答者一样, paintComponent 属于 JPanel 类。这对您意味着您需要创建一个扩展 JPanel的类(让我们称它为 MyPanel )。 (注意:如果你在eclipse中,或者将它设置为内部类,那么你可以为这个类创建一个新的.java文件,它应该以任何方式工作)。



一次你已经完成了,只需从 setup 类中删除paintCOmponent方法,并将其粘贴到新的 MyPanel 类中。



最后,在安装类中,不是创建 JPanel 对象,而是创建一个 MyPanel



基本上,这个 MyPanel 对象是您自己定制的 JPanel 对象,可以做任何你想做的事!这几乎就像是魔法!



在旁注中(这将有助于您更好地设计未来的代码格式),并希望更多的经验Java程序员会同意我的观点这也是我也建议你自己创建自定义的 JFrame 对象。只有这个,你不会覆盖构造函数以外的任何方法。相反,在您的构造函数中为这个自定义 JFrame ,您将为窗口制定所有规格(例如您的 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE setSize(300,300) calls)。构造函数也是你实例化你的 MyPanel 对象(以及窗口中的任何其他组件对象)的地方,也可以给它一些 ActionListener s。

然后,在另一个类中(比如 setup class) ,有一个 main 方法,它有一行:一个实例化一个'JFrame`对象,这将自动创建窗口。



哦,还有一件更重要的事情:如果你在你的 JFrame 上必须调用 setVisible(true)希望它能够显示,我不确定为什么这样设置。


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");
  }
}

解决方案

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.

这篇关于基本paintComponent不被repaint()调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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