Swing页面属性的设置顺序
2011-07-07 11:04
253 查看
这是一个细节问题,但细节问题关乎一个软件的设计成败,我想这个例子可以充分说明这个问题。
问题是这样产生的,我给一个JPanel设置了一幅背景图片,结果我发现绘制图像的protected void paintComponent(Graphics g)方法,总是调用两次,于是着手调查原因,因为连续调用联系protected void paintComponent(Graphics g)方法,并将占用大量的时间,让Swing页面加载明显减慢。当您的protected void paintComponent(Graphics g)方面里有复杂和大量的画图程序时,加载变慢更加明显。
最后我发现protected void paintComponent(Graphics g)之所以连续调用两次,竟然因为以下两行代码:
Java代码
setSize(610, 440);
setVisible(true);
如果我把这两行代码颠倒顺序,程序就只调用protected void paintComponent(Graphics g)一次:
Java代码
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后面。如下:
Java代码
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方法。
Java代码
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设置背景图片。
文章地址:/content/3025633.html
问题是这样产生的,我给一个JPanel设置了一幅背景图片,结果我发现绘制图像的protected void paintComponent(Graphics g)方法,总是调用两次,于是着手调查原因,因为连续调用联系protected void paintComponent(Graphics g)方法,并将占用大量的时间,让Swing页面加载明显减慢。当您的protected void paintComponent(Graphics g)方面里有复杂和大量的画图程序时,加载变慢更加明显。
最后我发现protected void paintComponent(Graphics g)之所以连续调用两次,竟然因为以下两行代码:
Java代码
setSize(610, 440);
setVisible(true);
如果我把这两行代码颠倒顺序,程序就只调用protected void paintComponent(Graphics g)一次:
Java代码
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后面。如下:
Java代码
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方法。
Java代码
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设置背景图片。
文章地址:/content/3025633.html
相关文章推荐
- JFrame打开居中和相关设置 swing 页面设置顺序有关
- asp.net页面的AutoEventWireup="true"属性设置
- 技巧笔记:Algin属性设置顺序问题
- C#获取页面上的所有checkbox控件对象并设置属性
- Expires 属性可设置在页面缓存失效前页面被缓存的时间(分钟)。Response.Expires=-1 指示
- 9.22 CSS复合属性和JavaScript在Html页面中的执行顺序
- web页面一些移动端样式属性设置
- 【MFC】RADIO按钮GROUP属性未设置正确的TAB顺序而引起单选BUG
- jsp页面上读取和设置select标签下的选中项和radio属性的选中项
- VS 中C#下"的页面布局"简单设置のの"PageLayout属性"哪里去了
- 关于.net winform ComboBox的DataSource,DisplayMember和ValueMember属性的设置顺序的问题。
- textbox设置只读属性 页面提交时值丢失解决办法
- Struts2的学习Struts,web.xml,Struts.xml设置,Struts属性读取顺序
- asp.net 遍历aspx页面中的TextBox控件,并设置属性为只读
- 解决JSP页面乱码,理解pageEncoding、contentType属性设置
- zIndex 属性设置元素的堆叠顺序。
- ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前
- 设置控件获得焦点顺序的属性tabindex
- 往list中放对象与设置对象属性先后顺序
- C# .NET 遍历一个Web页面中所有的TextBox控件 并设置读写属性