这是一个细节问题,但细节问题关乎一个软件的设计成败,我想这个例子可以充分说明这个问题。
问题是这样产生的,我给一个JPanel设置了一幅背景图片,结果我发现绘制图像的protected void paintComponent(Graphics g)方法,总是调用两次,于是着手调查原因,因为连续调用联系protected void paintComponent(Graphics g)方法,并将占用大量的时间,让Swing页面加载明显减慢。当您的protected void paintComponent(Graphics g)方面里有复杂和大量的画图程序时,加载变慢更加明显。
最后我发现protected void paintComponent(Graphics g)之所以连续调用两次,竟然因为以下两行代码:
如果我把这两行代码颠倒顺序,程序就只调用protected void paintComponent(Graphics g)一次:
另外这两行代码的顺行竟然还会影响您页面的显示效果,您也许觉得玄乎,请看画面:
之所以会产生以上的现象,我查看了源码问题可能出在Component类的repaintParentIfNeeded方法。
如果我们提前设置了setVisible,可以减少一次repaint();
附件是一个完整的例子,运行代码可以给一个JPanel设置背景图片。
文章地址: http://javapub.iteye.com/blog/763970
问题是这样产生的,我给一个JPanel设置了一幅背景图片,结果我发现绘制图像的protected void paintComponent(Graphics g)方法,总是调用两次,于是着手调查原因,因为连续调用联系protected void paintComponent(Graphics g)方法,并将占用大量的时间,让Swing页面加载明显减慢。当您的protected void paintComponent(Graphics g)方面里有复杂和大量的画图程序时,加载变慢更加明显。
最后我发现protected void paintComponent(Graphics g)之所以连续调用两次,竟然因为以下两行代码:
- setSize(610, 440);
- setVisible(true);
如果我把这两行代码颠倒顺序,程序就只调用protected void paintComponent(Graphics g)一次:
- setVisible(true);
- setSize(610, 440);
另外这两行代码的顺行竟然还会影响您页面的显示效果,您也许觉得玄乎,请看画面:
当我们用setVisible(true);setSize(610, 440);顺序执行程序时,显示页面:
//pic
protected void paintComponent(Graphics g)方法调用一次,页面也显示正常。
当我们用setSize(610, 440);setVisible(true);顺序执行程序时,显示页面:
//pic
protected void paintComponent(Graphics g)方法调用两次,页面也显示不正常,请大家注意红框位置。
好了现在大家知道了吧,我们在Swing设置显示顺行的时候,要把设置窗体尺寸大小方法setSize放在窗体可见方法setVisible后面。如下:
- 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设置背景图片。
文章地址: http://javapub.iteye.com/blog/763970