(大刀精品)半透明拖拽组件(JLabel JButton JPanel JTable JTree等等等等...)

最近一直在搞Java3D 没什么时间更新博客...最近又有很多事..
准备上学的事..减肥的事..等等等.....咱以后也是大学生啦。。哈哈

半透明的拖拽组件 可以拖拽任何有

addMouseListener方法并且有addMouseMotionListener方法的任何组件
包括JPanel 可以拖拽整个面板(JPanel)..
介绍一个人的博客(打个小广告.嘿嘿)

http://blog.sina.com.cn/swingjava

这个人很强吧,拖拽的本来是从他博客里看到他的项目介绍..我才想起来想办法实现这个拖拽效果的功能
不过他没开放源码..我就弄了一个......有些地方我并没有精简..因为这样的话..比较直观..更容易去理解他...如果你真正理解了..有很多地方是可以精简的...大家仔细看哦...有点难度.

效果图:


为了让你们看出更好的效果。。我把JPanel的背景色设置了一下


一共7个类..大家仔细的研究..难度是有点..不过努力就对了!!
第一个类.
package GhostGlassPane;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class demo extends JFrame
{
    
private GhostGlassPane glassPane;
    
private GhostComponentAdapter componentAdapter;
    
private JButton ceshi1;
    
private JButton ceshi2;
    
private JButton ceshi3;
    
private JButton ceshi4;
    
private JLabel  lceshi;
    
private JPanel jpanel;
    
private JButton button;
    
public demo()
    
{
        
super("Drag n' Ghost Demo");
        setLayout(
new BorderLayout());

        glassPane 
= new GhostGlassPane();
        componentAdapter 
= new GhostComponentAdapter(null,null);
        setGlassPane(glassPane);

        ceshi1 
= new JButton("按住我移动鼠标");
        ceshi1.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级Button"));
        ceshi1.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        ceshi2 
= new JButton("按住我移动鼠标");
        ceshi2.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级Button"));
        ceshi2.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        ceshi3 
= new JButton("按住我移动鼠标");
        ceshi3.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级Button"));
        ceshi3.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        ceshi4 
= new JButton("按住我移动鼠标");
        ceshi4.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级Button"));
        ceshi4.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        lceshi 
= new JLabel("按住我拖拽(JLabel)");
        lceshi.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级JLabel"));
        lceshi.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        button 
= new JButton("按住我移动鼠标");
        button.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级Button"));
        button.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        
        jpanel 
= new JPanel();
        jpanel.setBackground(Color.gray);
        jpanel.addMouseListener(componentAdapter 
= new GhostComponentAdapter(glassPane, "我是超级JPanel"));
        jpanel.addMouseMotionListener(
new GhostMotionAdapter(glassPane));
        jpanel.add(lceshi);
        jpanel.add(button);
 
        add(ceshi1,BorderLayout.SOUTH);
        add(ceshi2,BorderLayout.WEST);
        add(ceshi3,BorderLayout.EAST);
        add(ceshi4,BorderLayout.NORTH);
        add(jpanel,BorderLayout.CENTER);
        setSize(
400,300);
        setTitle(
"半透明拖拽组件");
        setResizable(
false);
        setLocationRelativeTo(
null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


     
public static void main(String[] args)
    
{
        demo d 
= new demo();
        d.setVisible(
true);
    }

}

第二个类:
package GhostGlassPane;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class GhostComponentAdapter extends GhostDropAdapter
{
    
public GhostComponentAdapter(GhostGlassPane glassPane, String action) {
        
super(glassPane, action);
    }


    
public void mousePressed(MouseEvent e)
    
{
        Component c 
= e.getComponent();

        BufferedImage image 
= new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g 
= image.getGraphics();
        c.paint(g);

        glassPane.setVisible(
true);

        Point p 
= (Point) e.getPoint().clone();
        SwingUtilities.convertPointToScreen(p, c);
        SwingUtilities.convertPointFromScreen(p, glassPane);

        glassPane.setPoint(p);
        glassPane.setImage(image);
        glassPane.repaint();
    }


    
public void mouseReleased(MouseEvent e)
    
{
        Component c 
= e.getComponent();

        Point p 
= (Point) e.getPoint().clone();
        SwingUtilities.convertPointToScreen(p, c);

        Point eventPoint 
= (Point) p.clone();
        SwingUtilities.convertPointFromScreen(p, glassPane);

        glassPane.setPoint(p);
        glassPane.setVisible(
false);
        glassPane.setImage(
null);

        fireGhostDropEvent(
new GhostDropEvent(action, eventPoint));
    }

}

第三个类
package GhostGlassPane;

import java.awt.event.MouseAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

public class GhostDropAdapter extends MouseAdapter {
    
protected GhostGlassPane glassPane;
    
protected String action;

    
private List listeners;

    
public GhostDropAdapter(GhostGlassPane glassPane, String action) {
        
this.glassPane = glassPane;
        
this.action = action;
        
this.listeners = new ArrayList();
    }


    
public void addGhostDropListener(GhostDropListener listener) {
        
if (listener != null)
            listeners.add(listener);
    }


    
public void removeGhostDropListener(GhostDropListener listener) {
        
if (listener != null)
            listeners.remove(listener);
    }


    
protected void fireGhostDropEvent(GhostDropEvent evt) {
        Iterator it 
= listeners.iterator();
        
while (it.hasNext()) {
            ((GhostDropListener) it.next()).ghostDropped(evt);
        }

    }

}

第四个类:
package GhostGlassPane;

import java.awt.Point;


public class GhostDropEvent {
    
private Point point;
    
private String action;

    
public GhostDropEvent(String action, Point point) {
        
this.action = action;
        
this.point = point;
    }


    
public String getAction() {
        
return action;
    }


    
public Point getDropLocation() {
        
return point;
    }

}


第五个类:
package GhostGlassPane;


public interface GhostDropListener {
    
public void ghostDropped(GhostDropEvent e);
}


第六个类:
package GhostGlassPane;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;


public class GhostGlassPane extends JPanel
{
    
private AlphaComposite composite;
    
private BufferedImage dragged = null;
    
private Point location = new Point(00);

    
public GhostGlassPane()
    
{
        setOpaque(
false);
        composite 
= AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
    }


    
public void setImage(BufferedImage dragged)
    
{
        
this.dragged = dragged;
    }


    
public void setPoint(Point location)
    
{
        
this.location = location;
    }


    
public void paintComponent(Graphics g)
    
{
        
if (dragged == null)
            
return;

        Graphics2D g2 
= (Graphics2D) g;
        g2.setComposite(composite);
        g2.drawImage(dragged,
                     (
int) (location.getX() - (dragged.getWidth(this)  / 2)),
                     (
int) (location.getY() - (dragged.getHeight(this/ 2)),
                     
null);
    }

}

第七个类:
package GhostGlassPane;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;


import javax.swing.SwingUtilities;

public class GhostMotionAdapter extends MouseMotionAdapter
{
    
private GhostGlassPane glassPane;

    
public GhostMotionAdapter(GhostGlassPane glassPane) {
        
this.glassPane = glassPane;
    }


    
public void mouseDragged(MouseEvent e)
    
{
        Component c 
= e.getComponent();

        Point p 
= (Point) e.getPoint().clone();
        SwingUtilities.convertPointToScreen(p, c);
        SwingUtilities.convertPointFromScreen(p, glassPane);
        glassPane.setPoint(p);

        glassPane.repaint();
    }

}

posted on 2008-06-20 07:23 相信 阅读(5699) 评论(3)  编辑  收藏 所属分类: Swing文章

评论

# re: (大刀精品)半透明拖拽组件(JLabel JButton JPanel JTable JTree等等等等...)[未登录] 2008-12-17 15:58 xxx

玻璃窗格上实现吗?不知道你有没有遇到过,多次拖拉会产生图形重叠的现象

Ps:你在每次设置图形之后全部重绘的做法很不科学....  回复  更多评论   

# re: (大刀精品)半透明拖拽组件(JLabel JButton JPanel JTable JTree等等等等...) 2012-03-22 15:30 draco

学习ING,不错  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

公告

不显示applet

常用链接

留言簿(16)

我参与的团队

随笔档案

文章分类

文章档案

新闻档案

相册

swingchina 专业搞Swing的网站

搜索

最新评论

阅读排行榜

评论排行榜