(大刀精品)半透明拖拽組件(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 相信 閱讀(5704) 評論(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年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泗阳县| 铜川市| 合肥市| 临泽县| 邻水| 墨竹工卡县| 师宗县| 象山县| 唐河县| 屏东市| 大同县| 宝山区| 湘潭市| 涪陵区| 钟山县| 沾化县| 汕尾市| 巴东县| 蓬莱市| 黎城县| 牟定县| 蓬安县| 霍林郭勒市| 霍城县| 嘉鱼县| 乌兰察布市| 达尔| 中西区| 荣昌县| 涟源市| 太仓市| 渭南市| 大同市| 青阳县| 始兴县| 屯门区| 乌兰浩特市| 枣阳市| 太白县| 射洪县| 宜川县|