repaint与updateUI的作用与区别

HideorHair 2008-04-15 06:22:37
从实现效果来看,repaint和updateUI差不多的。
那么rePaint和updateUI实质区别是什么,求解。

实例问题:
开发工具:MyEclipse
Java环境:jre1.6.0

我写了一个界面程序,通过一个button点击弹出图片,做好之后无法显示图片,我以为是图片路径问题,于是在监听里面修改了图片路径,加上了panel.updateUI()就OK了。
然后实现另一个button再添加另一张图片,也不行。在第二个button的监听当中加上了panel.repaint()也OK了。

为了测试repaint和updateUI的区别,我注释掉第二个button监听中的panel.repaint(),这时候,奇迹发生了,居然注释了之后第二个button依然可以完成功能添加另一张图片。
于是,我再注释掉第一个button监听中的panel.updateUI(),奇迹再次发生,现在不使用repaint和updateUI方法也可以实现按钮点击后界面的更新。


为了搞清楚“奇迹”的来源,我写了一个测试。
public class TestDisplay extends JPanel{
JPanel panel=new JPanel();

public TestDisplay(){
JButton button=new JButton("Click");
button.addActionListener(new ButtonTest());
panel.add(button);
add(panel);
}

class ButtonTest implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
panel.add(new JButton("Test"));
panel.updateUI(); add(panel);
}
}
}

public class Test {

public static void main(String[] args) {
TestDisplay td=new TestDisplay();
JFrame myFrame = new JFrame();
myFrame.setContentPane(td);
myFrame.setPreferredSize(new Dimension(700,550));
myFrame.setLocation(300, 150);
myFrame.pack();
myFrame.setVisible(true);
}

}

在此测试程序中,删除了updateUI()<红色部分>方法就无法实现显示中添加一个按钮的功能了,而这个测试程序将updateUI()改成repaint()也无法实现功能。


问题!!!!:那么实现这类事件监听中更改了界面的功能,是否必须调用界面容器的updateUI或repaint方法?如果是,那么请问,应该调用哪一个?两者有何区别?

小弟初次编写GUI程序,急求解答,望各位大侠帮忙!!!
...全文
1289 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
daly_ma 2011-06-07
  • 打赏
  • 举报
回复
同样在用,也同样不怎么理解。。。
HideorHair 2008-04-16
  • 打赏
  • 举报
回复
多谢楼上这位大哥了。虽然复杂得基本上看不怎么明白,还是谢谢您费心了。
hengxxh 2008-04-16
  • 打赏
  • 举报
回复
引用资料,希望对你有帮助!




Painting in Swing


Swing组件和AWT组件一样支持callback机制,拥有repaint方法,而且swing组件还内置了双缓冲机制和附加结构,如边界和UI delegate,还提供了 RepaintManager API


Swing组件默认是不透明的


不透明:会形成屏幕垃圾


透明:不会形成屏幕垃圾,降低效率


Swing组件的paint方法实际上是调用三个独立的方法,即:


1、 protected void paintComponent(Graphics g)


2、 protected void paintBorder(Graphics g)


3、 protected void paintChildren(Graphics g)


swing组件应该重载paintComponent方法而不是paint方法,而且尽量不要重载paintBorder和paintComponents方法(虽然这是允许的)。


UI delegates:标准swing组件实现的外观(look-and-feel)


与UI delegates相关联的标准组件的paint遵循下列步骤:


1、 paint方法调用paintComponent方法


2、 如果ui属性非空,paintComponent方法调用ui.update方法


3、 如果组件opaque属性为真,即不透明的话,ui.update方法会使用背景色填充背景,然后调用ui.paint方法


4、 ui.paint方法绘制组件内容


所以在重载的paintComponent方法中要调用super.paintComponent()方法,否则在不透明的前提下,组件必须负责填充背景。


Swing中paint调用的两种途径:


A. 由第一个重量级的祖先(ancestor)调用paint请求(通常是JFrame, JDialog, JWindow, JApplet)


1、 事件调度线程调用paint方法


2、 Container.paint方法默认操作是循环调用所有轻量级子类(descendents)的paint方法


3、 当绘制到第一个swing组件的时候,JComponent.paint方法的默认操作如下:


1. 如果组件实现了双缓冲的话,自动在内存中分配一个Graphics对象的副本;


2. 调用paintComponent方法(如果双缓冲的话就作用在后台(offscreen)graphics对象)


3. 调用paintBorder方法(如果双缓冲的话就作用在后台(offscreen)graphics对象)


4. 调用paintChildren方法(如果双缓冲的话就作用在后台(offscreen)graphics对象)


5. 如果组件实现了双缓冲的话,将后台graphics复制到前台来


B. 由javax.swing.JComponent的扩展的repaint方法产生的paint请求:


(详见原文) 主要是调用paintImmediately方法,对于swing组件来说,决定不会调用update方法。


Swing Painting Guidelines:


1、 swing组件中,paint方法通常是被系统或者应用程序触发,update方法决不会被swing组件触发;


2、 程序要调用repaint方法来实现paint功能,而不能直接调用paint方法;


3、 如果组件有复杂输出的话,应该使用带参数的repaint方法,即只重画需要重画的矩形区域,否则就要重新绘制全部组件;


4、 Swing组件的paint方法依次调用下列函数:


1. paintComponent()


2. paintBorder()


3. paintChildren()


swing组件的子类如果想实现绘制代码的话,需要重载paintComponent方法,而不是paint方法;


5、 swing推荐两个属性用来提高绘制效率:


1. opaque:组件是否绘制所有像素点,即透明与否,默认不透明


2. optimizedDrawingEnabled:是否组件的子类有交迭,只重画交迭部分提高效率,默认true,有交迭;


6、 如果swing组件的opaque属性设为真,即不透明的话,那么就会绘制所有的像素点(包括使用paintComponent方法清除背景),否则会保留屏幕垃圾(透明);


7、 把opaque或optimizedDrawingEnabled的任何一个属性设成false,会增加绘制的处理过程;


8、 Swing组件的拥有UI delegates的子类(包括JPanel),应该在他们的paintComponent()方法里调用super.paintComponent()方法,因为UI delegates要负责重画背景的工作,还要注意第五点;


9、 Swing组件支持双缓冲机制,可以靠JComponent.doubleBuffered属性来设置,默认为true;


10、 强烈推荐将swing的所有组件设成双缓冲;


11、 有着复杂输出的组件应该合理使用要重绘的区域来减少有覆盖时候的绘制操作。


小宏 2008-04-16
  • 打赏
  • 举报
回复
......
实现文件的拖拽功能 import java.awt.AlphaComposite; import java.awt.Component; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.Timer; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; class DragTree extends JTree implements DragGestureListener, DragSourceListener, DropTargetListener { /** * */ private static final long serialVersionUID = 1L; BufferedImage ghostImage; private Rectangle2D ghostRect = new Rectangle2D.Float(); private Point ptOffset = new Point(); private Point lastPoint = new Point(); private TreePath lastPath; private Timer hoverTimer; FileNode sourceNode; public DragTree() { DragSource dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, // component where // drag originates DnDConstants.ACTION_COPY_OR_MOVE, // actions this); // drag gesture recognizer setModel(createTreeModel()); addTreeExpansionListener(new TreeExpansionListener() { public void treeCollapsed(TreeExpansionEvent e) { } public void treeExpanded(TreeExpansionEvent e) { TreePath path = e.getPath(); if (path != null) { FileNode node = (FileNode) path.getLastPathComponent(); if (!node.isExplored()) { DefaultTreeModel model = (DefaultTreeModel) getModel(); node.explore(); model.nodeStructureChanged(node); } } } }); this.setCellRenderer(new DefaultTreeCellRenderer() { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { TreePath tp = tree.getPathForRow(row); if (tp != null) { FileNode node = (FileNode) tp.getLastPathComponent(); File f = node.getFile(); try { Icon icon = FileSystemView.getFileSystemView() .getSystemIcon(f); this.setIcon(icon); this.setLeafIcon(icon); this.setOpenIcon(icon); this.setClosedIcon(icon); this.setDisabledIcon(icon); } catch (Exception e) { e.printStackTrace(); } } return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); } }); super.setScrollsOnExpand(true); new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this); // Set up a hover timer, so that a node will be automatically expanded // or collapsed // if the user lingers on it for more than a short time hoverTimer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { if (lastPath == null) { return; } if (getRowForPath(lastPath) == 0) return; // Do nothing if we are hovering over the root node if (isExpanded(lastPath)) collapsePath(lastPath); else expandPath(lastPath); } }); hoverTimer.setRepeats(false); // Set timer to one-shot mode this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); int modifiers = e.getModifiers(); if (code == 'v' || code == 'V') { System.out.println("find v"); System.out.println("modifiers:" + modifiers + "\t" + ((modifiers & KeyEvent.CTRL_MASK) != 0)); } if ((modifiers & KeyEvent.CTRL_MASK) != 0 && (code == 'v' || code == 'V')) { Transferable tr = Toolkit.getDefaultToolkit() .getSystemClipboard().getContents(null); TreePath path = getSelectionPath(); if (path == null) { return; } FileNode node = (FileNode) path.getLastPathComponent(); if (node.isDirectory()) { System.out.println("file cp"); try { List list = (List) (tr .getTransferData(DataFlavor.javaFileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); } catch (Exception ioe) { ioe.printStackTrace(); } updateUI(); } } } }); } public void dragGestureRecognized(DragGestureEvent e) { // drag anything ... TreePath path = getLeadSelectionPath(); if (path == null) return; FileNode node = (FileNode) path.getLastPathComponent(); sourceNode = node; // Work out the offset of the drag point from the TreePath bounding // rectangle origin Rectangle raPath = getPathBounds(path); Point ptDragOrigin = e.getDragOrigin(); ptOffset.setLocation(ptDragOrigin.x - raPath.x, ptDragOrigin.y - raPath.y); // Get the cell renderer (which is a JLabel) for the path being dragged int row = this.getRowForLocation(ptDragOrigin.x, ptDragOrigin.y); JLabel lbl = (JLabel) getCellRenderer().getTreeCellRendererComponent( this, // tree path.getLastPathComponent(), // value false, // isSelected (dont want a colored background) isExpanded(path), // isExpanded getModel().isLeaf(path.getLastPathComponent()), // isLeaf row, // row (not important for rendering) false // hasFocus (dont want a focus rectangle) ); lbl.setSize((int) raPath.getWidth(), (int) raPath.getHeight()); // <-- // The // layout // manager // would // normally // do // this // Get a buffered image of the selection for dragging a ghost image this.ghostImage = new BufferedImage((int) raPath.getWidth(), (int) raPath.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2 = ghostImage.createGraphics(); // Ask the cell renderer to paint itself into the BufferedImage g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); // Make the image ghostlike lbl.paint(g2); g2.dispose(); // this.getGraphics().drawImage(ghostImage, e.getDragOrigin().x, // e.getDragOrigin().y, this); e.startDrag( null, // cursor ghostImage, new Point(5, 5), new StringSelection(getFilename()), // transferable this); // drag source listener } public void dragDropEnd(DragSourceDropEvent e) { ghostImage = null; sourceNode = null; } public void dragEnter(DragSourceDragEvent e) { } public void dragExit(DragSourceEvent e) { if (!DragSource.isDragImageSupported()) { repaint(ghostRect.getBounds()); } } public void dragOver(DragSourceDragEvent e) { } public void dropActionChanged(DragSourceDragEvent e) { } public String getFilename() { TreePath path = getLeadSelectionPath(); FileNode node = (FileNode) path.getLastPathComponent(); return ((File) node.getUserObject()).getAbsolutePath(); } private DefaultTreeModel createTreeModel() { File root = FileSystemView.getFileSystemView().getRoots()[0]; FileNode rootNode = new FileNode(root); rootNode.explore(); return new DefaultTreeModel(rootNode); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragOver(DropTargetDragEvent dtde) { Point pt = dtde.getLocation(); if (pt.equals(lastPoint)) { return; } if (ghostImage != null) { Graphics2D g2 = (Graphics2D) getGraphics(); // If a drag image is not supported by the platform, then draw my // own drag image if (!DragSource.isDragImageSupported()) { paintImmediately(ghostRect.getBounds()); // Rub out the last // ghost image and cue // line // And remember where we are about to draw the new ghost image ghostRect.setRect(pt.x - ptOffset.x, pt.y - ptOffset.y, ghostImage.getWidth(), ghostImage.getHeight()); g2.drawImage((ghostImage), AffineTransform .getTranslateInstance(ghostRect.getX(), ghostRect.getY()), null); } } TreePath path = getClosestPathForLocation(pt.x, pt.y); if (!(path == lastPath)) { lastPath = path; hoverTimer.restart(); } } public void dropActionChanged(DropTargetDragEvent dtde) { } public void drop(DropTargetDropEvent e) { try { DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable tr = e.getTransferable(); TreePath path = this.getPathForLocation(e.getLocation().x, e.getLocation().y); if (path == null) { e.rejectDrop(); return; } FileNode node = (FileNode) path.getLastPathComponent(); if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && node.isDirectory()) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); System.out.println("file cp"); List list = (List) (e.getTransferable() .getTransferData(DataFlavor.javaFileListFlavor)); Iterator iterator = list.iterator(); File parent = node.getFile(); while (iterator.hasNext()) { File f = (File) iterator.next(); cp(f, new File(parent, f.getName())); } node.reexplore(); e.dropComplete(true); this.updateUI(); } else if (e.isDataFlavorSupported(stringFlavor) && node.isDirectory()) { String filename = (String) tr.getTransferData(stringFlavor); if (filename.endsWith(".txt") || filename.endsWith(".java") || filename.endsWith(".jsp") || filename.endsWith(".html") || filename.endsWith(".htm")) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); File f = new File(filename); if (f.exists()) { f.renameTo(new File(node.getFile(), f.getName())); node.reexplore(); ((FileNode) sourceNode.getParent()).remove(sourceNode); e.dropComplete(true); this.updateUI(); } else { e.rejectDrop(); } } else { e.rejectDrop(); } } else { e.rejectDrop(); } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } finally { ghostImage = null; this.repaint(); } } private void cp(File src, File dest) throws IOException { if (src.isDirectory()) { if (!dest.exists()) { boolean ret = dest.mkdir(); if (ret == false) return; } File[] fs = src.listFiles(); for (int i = 0; i < fs.length; i++) { cp(fs[i], new File(dest, fs[i].getName())); } return; } byte[] buf = new byte[1024]; FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dest); int len; try { while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { in.close(); out.close(); } } public void dragExit(DropTargetEvent dte) { } } class FileNode extends DefaultMutableTreeNode { private boolean explored = false; public FileNode(File file) { setUserObject(file); } public boolean getAllowsChildren() { return isDirectory(); } public boolean isLeaf() { return !isDirectory(); } public File getFile() { return (File) getUserObject(); } public boolean isExplored() { return explored; } public boolean isDirectory() { File file = getFile(); return file.isDirectory(); } public String toString() { File file = (File) getUserObject(); String filename = file.toString(); int index = filename.lastIndexOf(File.separator); return (index != -1 && index != filename.length() - 1) ? filename .substring(index + 1) : filename; } public void explore() { if (!isDirectory()) return; if (!isExplored()) { File file = getFile(); File[] children = file.listFiles(); for (int i = 0; i < children.length; ++i) { if (children[i].isDirectory()) add(new FileNode(children[i])); } for (int i = 0; i < children.length; ++i) { if (!children[i].isDirectory()) add(new FileNode(children[i])); } explored = true; } } public void reexplore() { this.removeAllChildren(); explored = false; explore(); } }

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧