这是component类中的方法....

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【sure2003】截止到2008-07-28 09:14:15的历史汇总数据(不包括此帖):
    发帖的总数量:275                      发帖的总分数:11233                    每贴平均分数:40                       
    回帖的总数量:240                      得分贴总数量:10                       回帖的得分率:4%                       
    结贴的总数量:274                      结贴的总分数:11193                    
    无满意结贴数:2                        无满意结贴分:50                       
    未结的帖子数:1                        未结的总分数:40                       
    结贴的百分比:99.64 %               结分的百分比:99.64 %                  
    无满意结贴率:0.73  %               无满意结分率:0.45  %                  
    值得尊敬
      

  2.   

    很少用到这个呢不过看名字应该是可以得到一个Graphics对象,然后你就可以用这个对象在Component中写字画画啦~
      

  3.   

    呵呵,说过没用过啦试试就知道咯稍等,我开下eclipse
      

  4.   

    package test;import java.awt.Graphics;import javax.swing.JFrame;public class TestGetGraphics{
    public static void main(String[] args){
    JFrame f = new JFrame("TestGetGraphics");


    f.setSize(800, 600);
    f.setVisible(true);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Graphics g = f.getGraphics();
    //while(true)
    g.drawLine(0, 0, 500, 500);
    }
    }以上是代码.运行时可以看到一闪而过的一条斜线,那是因为我没有持续画图,一刷新就没有了
    如果在drawLine前加个while(true)
    你就可以看到那条斜线了在JFrame没有setVisible(true)的时候,getGraphics()的返回结果是null
      

  5.   

    /**
         * Creates a graphics context for this component. This method will
         * return <code>null</code> if this component is currently not
         * displayable.
         * @return a graphics context for this component, or <code>null</code>
         *             if it has none
         * @see       #paint
         * @since     JDK1.0
         */
        public Graphics getGraphics() {
            if (peer instanceof LightweightPeer) {
                // This is for a lightweight component, need to
                // translate coordinate spaces and clip relative
                // to the parent.
                if (parent == null) return null;
                Graphics g = parent.getGraphics();
                if (g == null) return null;
                if (g instanceof ConstrainableGraphics) {
                    ((ConstrainableGraphics) g).constrain(x, y, width, height);
                } else {
                    g.translate(x,y);
                    g.setClip(0, 0, width, height);
                }
                g.setFont(getFont());
                return g;
            } else {
                ComponentPeer peer = this.peer;
                return (peer != null) ? peer.getGraphics() : null;
            }
        }
    看Component.class中此方法
    peer是组件的peer,peer实现了组件的行为。
    当组件被加入到容器中时peer被创设,且此时组件也是peer。
    因此当peer为null时,也即Component的行为没有被实现之前,getGraphics方法是得不到对象的* The peer of the component. The peer implements the component's
    * behavior. The peer is set when the <code>Component</code> is 
    * added to a container that also is a peer.
      

  6.   

    取得一个画图的环境,
    你可以在上面画你要的图像。
    可以看成是一个画画用的画架,
    而Graphics g中的g可以看成是画笔。
      

  7.   

    没一个Component都有一个Graphics来负责外观的,英雌可以通过对Graphics 中的方法调用来改变Component的外观。
      

  8.   

    据尚学堂老马的说法,就是拿到某个Component的画笔,其实用多了也就是那个意思
      

  9.   

    peer是组件的peer,peer实现了组件的行为。 
    当组件被加入到容器中时peer被创设,且此时组件也是peer。 
    因此当peer为null时,也即Component的行为没有被实现之前
      

  10.   

    纠正 应该是容器也是一个peer
      

  11.   

    y3cbgvwfbjhcdvbcnvbdhgbndbhufbjdhcjcdbhvc nbenjdvhdfevnrjv