这是component类中的方法....
解决方案 »
- 安装了jdk1.7.0_01后发现少docs
- java 如何调节 linux系统音量,以及获得硬盘容量,CPU占用率,内存使用率等硬件信息?
- 如何学好java语言?
- 怎么导入poi.jar WordExtracto还是报错呀?
- 请大家帮帮忙,编写一个小型的考试练习系统
- 在演示hibernate练习的时候,我在生成ORM映射的时候,为什么会出现两个POJO类呢?
- java在执行copy命令时,如何实时获取当前的copy进度呀?
- 小应用程序中,鼠标的指针如何改变,this.setCursor(Cursor.HAND_CURSOR);提示我的类中没有setCursor
- jb6使用体会。
- Test类究竟实现的是哪个接口中的方法
- 请假一下runtime.exec调用exp命令导出oracle数据库的问题
- Hashtable这是什么意思?怎么改?
楼主【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 %
值得尊敬
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
* 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.
你可以在上面画你要的图像。
可以看成是一个画画用的画架,
而Graphics g中的g可以看成是画笔。
当组件被加入到容器中时peer被创设,且此时组件也是peer。
因此当peer为null时,也即Component的行为没有被实现之前