<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的網站

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲女女女同性video| 岛国精品一区免费视频在线观看 | 亚洲欧洲免费视频| 亚洲视频免费一区| 爱情岛论坛亚洲品质自拍视频网站| 亚洲精品A在线观看| 久久综合给合久久国产免费| 亚洲欧美国产国产综合一区| 国产亚洲AV无码AV男人的天堂| 国产在线观看片a免费观看 | 91大神免费观看| 免费观看亚洲人成网站| 日韩亚洲AV无码一区二区不卡| 女人被男人躁的女爽免费视频| 日本免费在线中文字幕| 亚洲AV无码国产剧情| 亚洲最新视频在线观看| 亚洲福利精品电影在线观看| 亚洲免费视频网址| 黄桃AV无码免费一区二区三区| 国产成人精品日本亚洲11| 国产亚洲精品资源在线26u| 精品久久久久久久免费人妻| 99re在线免费视频| 久久不见久久见免费影院www日本 久久WWW免费人成—看片 | 成人免费视频小说| 四虎影视成人永久免费观看视频| 亚洲AV综合永久无码精品天堂| 亚洲精品私拍国产福利在线| 亚洲片一区二区三区| 成人免费无毒在线观看网站| 亚洲精品免费视频| 中文字幕在线视频免费观看| 亚洲精品美女久久7777777| 亚洲黄色片在线观看| 亚洲日产韩国一二三四区| 国产成人在线观看免费网站 | 国产精品国产亚洲精品看不卡| 在线免费视频一区| 国产一卡2卡3卡4卡2021免费观看| 91在线免费视频|