<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    (大刀精品)半透明拖拽組件(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 相信 閱讀(5703) 評論(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,不錯  回復  更多評論   

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    公告

    不顯示applet

    常用鏈接

    留言簿(16)

    我參與的團隊

    隨筆檔案

    文章分類

    文章檔案

    新聞檔案

    相冊

    swingchina 專業搞Swing的網站

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产精品久久| 国产亚洲AV夜间福利香蕉149| 亚洲AV中文无码乱人伦下载| 国产精品偷伦视频免费观看了 | 亚洲啪啪AV无码片| 大妹子影视剧在线观看全集免费 | 久久免费国产精品一区二区| 亚洲精品无码久久千人斩| 国产免费无码一区二区| 国产黄色一级毛片亚洲黄片大全| 一区二区三区精品高清视频免费在线播放 | 亚洲成电影在线观看青青| 99re免费在线视频| 亚洲黄网在线观看| 精品熟女少妇AV免费观看| 亚洲av永久无码精品秋霞电影秋 | 亚洲精品无码专区在线| 国产精品无码素人福利免费| 曰批免费视频播放免费| 亚洲色欲一区二区三区在线观看| 免费h视频在线观看| 亚洲免费在线视频观看| 国产精品麻豆免费版| 一级特黄色毛片免费看| 亚洲国产精品va在线播放| 97在线观免费视频观看 | 免费**毛片在线播放直播| 国产激情久久久久影院老熟女免费 | 黄色一级视频免费观看| 久久久久无码精品亚洲日韩| 无码一区二区三区AV免费| 牛牛在线精品观看免费正| 亚洲国产综合91精品麻豆| 天天操夜夜操免费视频| 美女无遮挡拍拍拍免费视频 | 在线成人精品国产区免费| 亚洲人成免费网站| 亚洲精品A在线观看| 999任你躁在线精品免费不卡| 亚洲人成色在线观看| 亚洲午夜久久久影院|