Swing页面属性的设置顺序

[b]这是一个细节问题,但细节问题关乎一个软件的设计成败[/b],我想这个例子可以充分说明这个问题。

问题是这样产生的,我给一个JPanel设置了一幅背景图片,结果我发现绘制图像的protected void paintComponent(Graphics g)方法,总是调用两次,于是着手调查原因,因为连续调用联系protected void paintComponent(Graphics g)方法,并将占用大量的时间,让Swing页面加载明显减慢。[b]当您的protected void paintComponent(Graphics g)方面里有复杂和大量的画图程序时,加载变慢更加明显。[/b]

最后我发现protected void paintComponent(Graphics g)之所以连续调用两次,竟然因为以下两行代码:

setSize(610, 440);
setVisible(true);


如果我把这两行代码颠倒顺序,程序就只调用protected void paintComponent(Graphics g)一次:

setVisible(true);
setSize(610, 440);


另外这两行代码的顺行竟然还会影响您页面的显示效果,您也许觉得玄乎,请看画面:
当我们用[b]setVisible(true);setSize(610, 440);[/b]顺序执行程序时,显示页面:
[img]http://www.kutoku.info/images/java/100915/demo01.png[/img]
[b]protected void paintComponent(Graphics g)方法调用一次,页面也显示正常。[/b]

当我们用[b]setSize(610, 440);setVisible(true);[/b]顺序执行程序时,显示页面:
[img]http://www.kutoku.info/images/java/100915/demo02.png[/img]
[b]protected void paintComponent(Graphics g)方法调用两次,页面也显示不正常[/b],请大家注意红框位置。

好了现在大家知道了吧,我们在Swing设置显示顺行的时候,要把[b][color=red]设置窗体尺寸大小方法setSize放在窗体可见方法setVisible后面[/color][/b]。如下:
private MainFrame01() {
setTitle("ZakiSoft Demo");
con = getContentPane();
ZPanel zp = new ZPanel();
zp.setBackground(new javax.swing.ImageIcon(getClass()
.getResource("/com/zakisoft/frame02/demo.jpg")));

con.add(zp);

setVisible(true);
setSize(610, 440);

setResizable(false);
setLocationRelativeTo(getOwner());
setDefaultCloseOperation(EXIT_ON_CLOSE);

}


之所以会产生以上的现象,我查看了源码问题可能出在Component类的repaintParentIfNeeded方法。
 private void repaintParentIfNeeded(int oldX, int oldY, int oldWidth,
int oldHeight)
{
if (parent != null && peer instanceof LightweightPeer && isShowing()) {
// Have the parent redraw the area this component occupied.
parent.repaint(oldX, oldY, oldWidth, oldHeight);
// Have the parent redraw the area this component *now* occupies.
repaint();
}
}

如果我们提前设置了setVisible,可以减少一次repaint();

附件是一个完整的例子,运行代码可以给一个JPanel设置背景图片。

文章地址:[url]http://javapub.iteye.com/blog/763970[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值