- 浏览: 473645 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
tianya2010ji:
谢了 工作了好多年,今天刚好用到它。
使用SPRING中的线程池ThreadPoolTaskExecutor实现JAVA并发 -
Mr.Bruce:
有用
jquery在加载完iframe的内容后才进行下一步操作 -
lyyltt:
有一个外部引用谷歌api没办法download
好东西 - JS生成多种图形,饼状图,柱状图,折线图,雷达图 -
bluky999:
这里写错了应该。。。--sm-plugins=......,+ ...
tigase自定义插件的开发与配置 -
lyy3323:
spring的 threadpool 没有submit方法呀 ...
使用SPRING中的线程池ThreadPoolTaskExecutor实现JAVA并发
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 { 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(); } }
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; 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.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileReader; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.UIManager; import javax.swing.WindowConstants; import javax.swing.text.Document; import javax.swing.text.EditorKit; public class Test extends JFrame implements DropTargetListener { private JEditorPane textPane = new JEditorPane(); public Test() { super("Drag and Drop With Swing"); new DropTarget(textPane, DnDConstants.ACTION_COPY_OR_MOVE, this); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createTreePanel(), createTextPanel()); splitPane.setDividerLocation(250); splitPane.setOneTouchExpandable(true); getContentPane().add(splitPane, BorderLayout.CENTER); } public static void main(String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e1) { e1.printStackTrace(); } Test test = new Test(); test.setBounds(300, 300, 850, 350); test.setVisible(true); test.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); test.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); } private JPanel createTreePanel() { JPanel treePanel = new JPanel(); DragTree tree = new DragTree(); treePanel.setLayout(new BorderLayout()); treePanel.add(new JScrollPane(tree), BorderLayout.CENTER); treePanel.setBorder(BorderFactory .createTitledBorder("Drag source for filenames")); return treePanel; } private JPanel createTextPanel() { JPanel textPanel = new JPanel(); textPanel.setLayout(new BorderLayout()); textPanel.add(new JScrollPane(textPane), BorderLayout.CENTER); textPanel.setMinimumSize(new Dimension(375, 0)); textPanel.setBorder(BorderFactory .createTitledBorder("Drop target for filenames")); return textPanel; } private void readFile(final String filename) { try { textPane.setPage(new File(filename).toURL()); } catch (IOException e) { EditorKit kit = textPane.getEditorKit(); Document document = textPane.getDocument(); try { document.remove(0, document.getLength()); kit.read(new FileReader(filename), document, 0); } catch (Exception ex) { ex.printStackTrace(); } } } public void drop(DropTargetDropEvent e) { try { DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable tr = e.getTransferable(); if (e.isDataFlavorSupported(stringFlavor)) { 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); readFile(filename); textPane.setCaretPosition(0); e.dropComplete(true); } else { e.rejectDrop(); } } else { e.rejectDrop(); } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } } public void dragEnter(DropTargetDragEvent e) { } public void dragExit(DropTargetEvent e) { } public void dragOver(DropTargetDragEvent e) { } public void dropActionChanged(DropTargetDragEvent e) { } }
从上面可以看出第一个类实现了DragGestureListener,DragSourceListener,DropTargetListener三个接口。
第二个类实现了DropTargetListener接口。
DragGestureListener是用于监听拖拽时间的发生的,发生时触发dragGestureRecognized方法,该方法讲解如下
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 = getRowForLocation(ptDragOrigin.x, ptDragOrigin.y); //lbl是用来获得当前选中目录的cellRenderer JLabel lbl = (JLabel) getCellRenderer().getTreeCellRendererComponent( this, // tree path.getLastPathComponent(), // value true, // 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 //把lbl画到BufferedImage中去 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 被拖拽组件在源组件中的事件 }
DropTargetListener接口用于描述被拖拽到某个组件(如ComponentEaxmple)上时触发的事件。添加该事件的操作例子如下:
new DropTarget(ComponentEaxmple, DnDConstants.ACTION_COPY_OR_MOVE, DropTargetListener);
相关推荐
下面是一个简单的例子,展示如何创建一个可透明拖动的JFrame: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TransparentDraggableFrame extends JFrame { private ...
本篇文章将深入解析一个基于Swing编写的刻度表实现的例子,通过这个实例,我们将探讨Swing中的JSlider组件的使用方法及其各种属性设置,帮助读者更全面地理解和掌握Swing框架中的刻度表设计。 #### JSlider:Swing...
总的来说,“swing之拖拽功能DragDemo”是一个展示Swing DnD功能的简单应用。通过理解拖动源和放置目标的概念,以及如何使用`DragSource`、`TransferHandler`和相关监听器,开发者可以为自己的Swing应用添加丰富的...
以下是一个简单的示例代码片段: ```java public class DragDropTable extends JTable { private int draggedRow = -1; public DragDropTable(TableModel model) { super(model); addMouseListener(new ...
Swing 是一个JavaScript库,专为实现容器内卡片元素的拖拽功能而设计,尤其吸引人的是它提供了独特的视觉效果,当用户尝试将卡片元素移出容器时,卡片会呈现出逼真的弹性摇摆效果。这一特性使 Swing 成为了前端...
本示例主要关注如何实现一个特殊的Swing按钮功能:允许用户通过鼠标拖动来改变按钮在窗口中的位置。 首先,我们需要创建一个自定义的JButton子类,这个子类将扩展Swing的JButton类,并添加额外的拖动功能。我们可以...
这个"基于Swing的图片浏览器源运行例子"是《疯狂Java实战演义》一书中第三章的一个实践项目,旨在帮助读者深入理解如何利用Swing来创建一个功能完备的图片查看器应用程序。以下是对这个案例的详细解析: 首先,...
这个例子展示了如何实现一个可拖动节点的`JTree`,这在用户界面设计中是一个常见且实用的功能,特别是在需要展示层次结构信息时。`JTree`允许用户通过拖放操作来重新排列节点,提高交互性和用户体验。 拖放(Drag ...
在这个例子中,我们创建了一个JFrame窗口,并添加了一个JButton。我们为按钮添加了一个ActionListener,当按钮被点击时,`actionPerformed`方法会被调用,打印出"Button clicked!"。 除了ActionListener,Swing还有...
接下来,我们创建一个简单的Swing应用,包含一个JFrame(主窗口)和一个JLabel(用于显示坐标)。在JFrame中添加MouseMotionListener,这样每当鼠标在窗口内移动时,就会触发`mouseMoved`方法: ```java public ...
这个“精美Swing 折叠菜单”项目就是这样一个例子,它模仿了Windows XP操作系统中左侧的折叠式导航菜单,为用户提供了方便的层级式导航。 Swing中的菜单主要由JMenuBar、JMenu、.JMenuItem等组件构成。JMenuBar用于...
总的来说,这个"java swing高仿QQ截屏源码"项目是一个很好的学习和实践Java GUI编程,特别是Swing组件和事件处理的例子。通过对源码的学习,开发者可以深入理解如何利用Java Swing构建复杂交互的桌面应用,同时也...
在这个项目中,开发人员创建了一个应用程序,允许用户选择屏幕上的任意矩形区域进行截图,未被选中的部分会显示为阴影效果,同时支持拖动和拉伸选区的八个方向。接下来,我们将深入探讨相关的Java Swing知识点。 ...
这篇博客文章“所支持拖拽功能的Swing-JTree代码”探讨了如何增强JTree的功能,使其支持拖放(Drag and Drop)操作,这是在图形用户界面设计中非常实用的一个特性。 拖放功能允许用户通过鼠标将一个项目从一处拖到...
在Java Swing库中,`JList`是一个常用的组件,用于展示一系列可选择的项。它在用户界面设计中常用于创建列表视图,允许用户通过单击或键盘导航来选择项目。在本教程中,我们将深入探讨如何实现`JList`的上下左右拖动...
以下是一个简单的Swing按钮到文本框拖放的示例: ```java import javax.swing.*; import java.awt.*; import java.awt.dnd.*; public class DragAndDropExample { public static void main(String[] args) { ...
在Java编程领域,Swing是用于构建图形用户界面(GUI)的一个重要库。它提供了丰富的组件,其中之一就是JTable,这是一个强大的组件,用于展示二维数据集,并允许用户进行交互操作。在"swing中JTable的简单使用"这个...
以下是一个简化的JavaFX示例: ```java @FXML private ListView<String> listView; public void initialize() { listView.setOnDragDetected(new EventHandler() { @Override public void handle(MouseEvent ...
在Java Swing中,JTree是一个非常重要的组件,用于展示层次结构的数据,通常以树状的形式显示。它在各种用户界面中都有广泛的应用,例如文件系统浏览、组织结构展示等。本教程将深入讲解如何使用JTree以及相关的操作...
3. **拖动过程**:在拖动过程中,系统通常会显示一个视觉反馈,如高亮边框或缩略图,表明当前选中的对象正在被拖动。 4. **释放鼠标**:当用户将鼠标移动到目标位置并释放按钮时,系统根据用户的行为(拖拽拷贝或...