【问题标题】:i cant get the program below, please explain我不能得到下面的程序,请解释
【发布时间】:2013-12-09 17:56:34
【问题描述】:
public class aaa {
public static void main(String[] args)
{

bbb b=new bbb();
    b.setVisible(true);
    b.setSize(400, 400);

}

}
class bbb extends JFrame
{
bbb()
{

draw d=new draw();
add(d);
}

}
class draw extends JComponent
{
public void paintComponent(Graphics g)
{
    Graphics2D g2=(Graphics2D) g;
    Ellipse2D rect=new Ellipse2D.Double();
    rect.setFrameFromCenter(50,50,70,70);
    g2.draw(rect);
    g2.setPaint(Color.blue);
    g2.fill(rect);
}
}

实际上,我知道这个程序会生成一个框架并在矩形内绘制椭圆...... 我唯一的问题是,什么时候会调用“paintcomponent(Graphics g)”方法... 我知道,它是“JComponent”中的一个方法,但是什么时候会被调用……请简要解释一下……

还有一个问题是......它在“Graphics”类型中有参数......但是它没有为“Graphics”类创建对象,那么我们如何调用它的方法(“Graphics”类中的方法")..

我知道这是个愚蠢的问题,但我知道我在概念上遗漏了一些东西......

【问题讨论】:

标签: java swing graphics jframe jcomponent


【解决方案1】:

【讨论】:

    【解决方案2】:
    • paintComponent()会在需要的时候被系统调用,你不用担心自己调用。
    • 您可以使用传递的参数 g 来调用 Graphics 方法,例如。 g.setPaint() 等

    来自docs

    (...)绘画子系统将确定组件已损坏,并确保调用您的paintComponent方法。

    【讨论】:

      猜你喜欢
      • 2019-09-22
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多