2012-12-21 24 views
1

我一直在google搜索了幾個小時試圖無濟於事一百萬個不同的細微變化都,所以我想我最好的選擇是選擇的人的大腦比我問題重新驗證/再油漆按鈕面板

我更熟練編寫一個從數據庫加載一堆按鈕的類,目標是讓用戶能夠按照自己的喜好排列按鈕,但是,出於某種原因,我似乎無法獲得框架以重新驗證或重繪。按鈕會四處移動,但它們不會重新排列,因爲我已將它們編碼。組織似乎正在正常工作,即在釋放鼠標按鈕時參與重新排列的代碼,只是組件只會停留在被拖放的位置,即使它們在其各自的列表中重新排序。

代碼很長,我不想發佈整個班級,因爲它可能會關閉一些人,但我不知道我在哪裏犯錯,所以我認爲這將是我最好的興趣發佈整個事情。關注的主要方面是mouseReleased(MouseEvent e) {...}repaint()/refresh()方法,但是,可能有一些我在其他地方丟失的東西。

tl; dr: 我基本上只是嘗試執行一個setBounds()後,用戶拖動和按下他們想要的順序按鈕,但按鈕停留在同一個地方,他們被拖放,而不會revalidate()repaint()。我甚至不能通過removeAll清除組件面板並重新加載。

謝謝你在先進。這裏是我的代碼:

public class AdminButtonEditor extends javax.swing.JFrame { 
    public AdminButtonEditor(OrderView parent) { ... 
     ... 
     Component[] components = buttonsPanel.getComponents(); 
     for (int i = 0; i < components.length; i++) { 
      Component c = components[i]; 
      if (c instanceof JButton) { 
       JButton jb = (JButton) c; 
       jb.setFocusable(false); 
       buttons.add(new MyJButton(...)); 
      } 
     } 
     for (int i = 0; i < buttons.size(); i++) { 
      buttons.get(i).addTo(editPanel); 
      buttons.get(i).orderIndex=modButtonList.get(i).menuModifier.getViewOrderValue(); 
      buttons.get(i).idx=i; 
     } 
     EventHandler eh = new EventHandler(); 
     addWindowListener(eh); 
     editPanel.addMouseMotionListener(eh); 
     editPanel.addMouseListener(eh); 
     contentPane.add(editPanel, BorderLayout.CENTER); 
    } 
    protected void refresh() { 
     if (!buttons.isEmpty() && buttons.get(0) != null) { 
      contentPane.remove(editPanel); 
      editPanel.removeAll(); 
      for (int i = 0; i < buttons.size(); i++) { 
       MyJButton s = buttons.get(i); 
       s.addTo(editPanel); 
      } 
      contentPane.add(editPanel, BorderLayout.CENTER); 
      editPanel.repaint(); 
     } 
    } 
    public void paint(Graphics g) { 
     refresh(); 
     super.paint(g); 
    } 
    private int getSelectionIndex(int x, int y) { 
     int s=-1; 
     for (int i=buttons.size()-1; i>=0;i--){ 
      if (buttons.get(i).contains(x, y)) { 
       s = i; 
       break; 
      } 
     } 
     return s; 
    } 
private class EventHandler implements MouseInputListener,WindowListener, ActionListener { 
    private int selectionIndex, startX, startY, lastX, lastY; 
    private MyJButton selected; 
    private boolean moving=false; 
    .... 
    .... 
    public void mouseReleased(MouseEvent e) { 
     if (moving){ 
      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
      moving=false; 
      int dropX=e.getX(); 
      int dropY=e.getY(); 
      int row = dropY/selected.height; 
      int col = dropX/selected.width; 
      int idx=(row*3+col)-1; 
      int oldIdx=buttons.indexOf(selected); 
      insertIntoList(idx,oldIdx); 
     } 
     if (selected!=null){ 
      selected.unHighLight(); 
      selected=null; 
     } 

     Collections.sort(buttons); 
     for (int i=0;i<buttons.size();i++){ 
     //modButtonList.get(buttons.get(i).idx).menuModifier.setViewOrderValue(buttons.get(i).orderIndex); 
     } 
     editPanel.validate(); 
     repaint(); 
    } 

    private void insertIntoList(int idx, int oldIdx) { 
     MyJButton temp = buttons.get(idx); 
     int tempid=buttons.get(idx).idx; 
     buttons.set(idx, new MyJButton(selected.text,selected.x,selected.y,selected.width,selected.height,selected.idx)); 
     buttons.get(idx).orderIndex=temp.orderIndex; 
     if (idx<oldIdx){ 
      int id; 
      for (int i=oldIdx;i>idx+1;i--){ 
       id=buttons.get(i).orderIndex; 
       buttons.set(i, new MyJButton(buttons.get(i-1).text,buttons.get(i-1).x,buttons.get(i-1).y,buttons.get(i-1).width,buttons.get(i-1).height,buttons.get(i-1).idx)); 
       buttons.get(i).orderIndex=id; 
      } 
      id = buttons.get(idx+1).orderIndex; 
      buttons.set(idx+1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx)); 
      buttons.get(idx+1).orderIndex=id; 
     } else if (idx>oldIdx) { 
      int id; 
      for (int i=oldIdx;i<idx-1;i++){ 
       id=buttons.get(i).orderIndex; 
       buttons.set(i, new MyJButton(buttons.get(i+1).text,buttons.get(i+1).x,buttons.get(i+1).y,buttons.get(i+1).width,buttons.get(i+1).height,buttons.get(i+1).idx)); 
       buttons.get(i).orderIndex=id; 
      } 
      id = buttons.get(idx-1).orderIndex; 
      buttons.set(idx-1,new MyJButton(temp.text,temp.x,temp.y,temp.width,temp.height,temp.idx)); 
      buttons.get(idx-1).orderIndex=id;; 
     } else { 
      buttons.get(idx).x=buttons.get(idx).originx; 
      buttons.get(idx).y=buttons.get(idx).originy; 
     } 
     repaint(); 
    } 
    public void mouseDragged(MouseEvent e) { 
     if (moving) { 
      Graphics g = editPanel.getGraphics(); 
      g.setColor(Color.black); 
      g.drawLine(selected.getXPos(), 0, selected.getXPos(),editPanel.getWidth()); 
      g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos()); 
      selected.moveBy(e.getX()-lastX, e.getY()-lastY); 
      g.setXORMode(Color.black); 

      g.drawLine(selected.getXPos(), 0, selected.getXPos(), editPanel.getWidth()); 
      g.drawLine(0, selected.getYPos(), editPanel.getHeight(), selected.getYPos()); 

      lastX=e.getX(); 
      lastY=e.getY(); 
      repaint(); 
     } 
    } 
    .... 
} 
    private class MyJButton extends JButton implements Comparable { 
    private int orderIndex,idx; 
    private int x, y, width, height,originx,originy; 
    private String text; 
    public Border DEFAULT_BORDER;// new SoftBevelBorder(BevelBorder.RAISED); 
    public Border SELECT_BORDER = BorderFactory.createLineBorder(Color.RED, 3, true); 

    public MyJButton(String text, int x, int y, int width, int height) { 
     .... 
     setFocusable(false); 
    } 
    public MyJButton(String text, int x, int y, int width, int height,int idx) {....} 

    public void addTo(JPanel p) { 
     setBounds(x, y, width, height); 
     p.add(this); 
    } 

    @Override 
    public boolean contains(int x, int y) { 
     int x1 = x, y1 = y; 
     if (x1 >= this.x && y1 >= this.y && x1 <= this.x + width && y1 <= this.y + height) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    @Override 
    public void setSize(int w, int h) { 
     width = w; 
     height = h; 
    } 
    .... 
    public void moveBy(int dx, int dy) { 
     x += dx; 
     y += dy; 
    } 

    @Override 
    public void resize(int newWidth, int newHeight) { 
     this.width = newWidth; 
     this.height = newHeight; 
     setBounds(x, y, width, height); 
    } 

    public int compareTo(Object o) { 
     MyJButton mjb = (MyJButton)o; 
     return this.idx-mjb.idx; 
    } 

    } 
} 
+1

忘記setSize()/ setBounds/setLocation並使用適當的'LayoutManager'。不要使用setPreferred/setMinimum/setMaximum-size()方法。每次從面板添加/刪除組件時,只需在該面板上調用'revalidate(); repaint();'。這一切都適用於一個適當的'佈局管理器'。 –

+0

如果您發佈了一個能夠再現您的問題的最小程序,它將有所幫助。 –

+0

@GuillaumePolet - 任何LayoutManager都能工作嗎?我不是一個Swing主腦,我認爲你在使用setBounds()方法時不應該使用LayoutManagers?我非常感謝你的意見,但我不確定你爲什麼給我提出一個問題的負面觀點,雖然對你來說也許是一個愚蠢的問題。 –

回答

3

+1給GagandeepBalis評論。

好吧,我發現這非常酷,並決定更多地考慮它。

我想出了一些邏輯,這將需要爲它工作,也許不是最好的,但...:

1)我們需要使我們的JButton小號拖動(謝謝@camickr和他DragLayout ):)

2)當JButton s的拖累,比上mouseReleased(..)即下跌,我們應該檢查是否有我們拖按鈕與任何其他

3)我們檢查是否JButton碰撞與其他通過獲取碰撞JButton圖像並計算多少個o JButton的paque像素,我們正在拖動,正在覆蓋另一個按鈕。 4)對碰撞次數進行排序並找到最高值,這將用於我們可以看到我們拖動的JButton的位置。即它將被碰撞最多的部件插入。他們使用Array(並且因此它們將被重新排序

5)進行排序,其保持的按鈕以匹配變化

6)除去所有按鈕和ArrayList重新添加)。

下面是一個例子(大多數代碼發生在重寫ComponentMovermouseReleased(..)法):

拖動任何設備之前:

enter image description here

拖動按鈕4比1鍵讓鼠標按鈕去後:

enter image description here

import java.awt.Component; 
import java.awt.Cursor; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GraphicsEnvironment; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.Window; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.Map; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class DragButtons { 

    ArrayList<JButton> buttons = new ArrayList<>(); 

    public DragButtons() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new DragButtons(); 
      } 
     }); 
    } 

    private void initComponents() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JPanel panel = new JPanel(new GridLayout(2, 2)); 

     ComponentMover cm = new ComponentMover() { 
      @Override 
      public void mouseReleased(MouseEvent e) { 
       super.mouseReleased(e); 
       HashMap<Integer, JButton> collisions = new HashMap<>(); 
       JButton draggedButton = (JButton) e.getSource(); 

       for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each 
        if (btn != draggedButton) {//dont chck button we were dragging 
         int col = checkPerPixelCollision(draggedButton, btn); 
         System.out.println("Button " + btn.getText()); 
         System.out.println(col); 
         collisions.put(col, btn); 
        } 
       } 
       //lets get the button which had most collisions 
       int maxCollisions = 0; 
       JButton intersectingButton = null; 
       for (Map.Entry<Integer, JButton> entry : collisions.entrySet()) { 
        Integer collisionCount = entry.getKey(); 
        JButton button = entry.getValue(); 
        if (collisionCount > maxCollisions) { 
         maxCollisions = collisionCount; 
         intersectingButton = button; 
        } 
       } 

       boolean reLayout = false; 

       if (maxCollisions > 0) {//check if there was any 
        System.out.println("Button " + draggedButton.getText() + " is intersecting more of Button " + intersectingButton.getText()); 
        System.out.println("Collisions: " + maxCollisions); 
        reLayout = true; 
       } else { 
        System.out.println("No change made"); 
        reLayout = false; 
       } 

       ArrayList<JButton> tmpButtons = (ArrayList<JButton>) buttons.clone();//create clone of buttons 

       if (reLayout) {//a button as moved and panel needs to be layed out 
        buttons.clear();//clear old buttons 

        for (JButton b : tmpButtons) {//re-order jbuttons 
         if (b == intersectingButton) { 
          buttons.add(draggedButton); 
         } else if (b == draggedButton) { 
          buttons.add(intersectingButton); 
         } else { 
          buttons.add(b); 
         } 
        } 
        panel.removeAll();//remove all buttons 
        for (JButton btn : buttons) {//iterate through all buttons and get the number of collsions of each 
         panel.add(btn);//re-add buttons according to arraylist 
        } 
        panel.revalidate(); 
        panel.repaint(); 
        //re-order the Array of buttons to fit 
        //remove all button and re add them using sorted array 
       } 

      } 
     }; 

     for (int i = 0; i < 4; i++) { 
      JButton b = new JButton(String.valueOf(i + 1)); 
      buttons.add(b); 
      panel.add(b); 
      cm.registerComponent(b); 
     } 

     frame.add(panel); 

     frame.pack(); 
     frame.setVisible(true); 

    } 

    public HashSet<String> getMask(JButton e) { 
     HashSet<String> mask = new HashSet<>(); 
     int pixel, a; 
     BufferedImage bi = null; 
     try { 
      bi = componentToImage(e, e.getBounds()); //gets the current image being shown 
     } catch (IOException ex) { 
      Logger.getLogger(DragButtons.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     for (int i = 0; i < bi.getWidth(); i++) { // for every (x,y) component in the given box, 
      for (int j = 0; j < bi.getHeight(); j++) { 
       pixel = bi.getRGB(i, j); // get the RGB value of the pixel 
       a = (pixel >> 24) & 0xff; 
       if (a != 0) { // if the alpha is not 0, it must be something other than transparent 
        mask.add((e.getX() + i) + "," + (e.getY() - j)); // add the absolute x and absolute y coordinates to our set 
       } 
      } 
     } 
     return mask; //return our set 
    } 

    public static BufferedImage componentToImage(Component component, Rectangle region) throws IOException { 
     BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); 
     Graphics g = img.getGraphics(); 
     g.setColor(component.getForeground()); 
     g.setFont(component.getFont()); 
     component.paintAll(g); 
     ImageIO.write(img, "png", new File("c:/saved.png")); 
     return img; 
    } 

    // Returns true if there is a collision between object a and object b 
    public int checkPerPixelCollision(JButton b, JButton b2) { 
     // This method detects to see if the images overlap at all. If they do, collision is possible 
     int ax1 = (int) b2.getX(); 
     int ay1 = (int) b2.getY(); 

     int ax2 = ax1 + (int) b2.getWidth(); 
     int ay2 = ay1 + (int) b2.getHeight(); 

     int bx1 = (int) b.getX(); 
     int by1 = (int) b.getY(); 

     int bx2 = bx1 + (int) b.getWidth(); 

     int by2 = by1 + (int) b.getHeight(); 

     if (by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1) { 
      return 0; // Collision is impossible. 
     } else { // Collision is possible. 
      // get the masks for both images 
      HashSet<String> maskPlayer1 = getMask(b2); 
      HashSet<String> maskPlayer2 = getMask(b); 
      maskPlayer1.retainAll(maskPlayer2); // Check to see if any pixels in maskPlayer2 are the same as those in maskPlayer1 
      if (maskPlayer1.size() > 0) { // if so, than there exists at least one pixel that is the same in both images, thus 
       return maskPlayer1.size(); 
      } 
     } 
     return 0; 
    } 
} 

class ComponentMover extends MouseAdapter { 

    private Insets dragInsets = new Insets(0, 0, 0, 0); 
    private Dimension snapSize = new Dimension(1, 1); 
    private Insets edgeInsets = new Insets(0, 0, 0, 0); 
    private boolean changeCursor = true; 
    private boolean autoLayout = false; 
    private Class destinationClass; 
    private Component destinationComponent; 
    private Component destination; 
    private Component source; 
    private Point pressed; 
    private Point location; 
    private Cursor originalCursor; 
    private boolean autoscrolls; 
    private boolean potentialDrag; 

    /** 
    * Constructor for moving individual components. The components must be 
    * regisetered using the registerComponent() method. 
    */ 
    public ComponentMover() { 
    } 

    /** 
    * Constructor to specify a Class of Component that will be moved when drag 
    * events are generated on a registered child component. The events will be 
    * passed to the first ancestor of this specified class. 
    * 
    * @param destinationClass the Class of the ancestor component 
    * @param component the Components to be registered for forwarding drag 
    * events to the ancestor Component. 
    */ 
    public ComponentMover(Class destinationClass, Component... components) { 
     this.destinationClass = destinationClass; 
     registerComponent(components); 
    } 

    /** 
    * Constructor to specify a parent component that will be moved when drag 
    * events are generated on a registered child component. 
    * 
    * @param destinationComponent the component drage events should be 
    * forwareded to 
    * @param components the Components to be registered for forwarding drag 
    * events to the parent component to be moved 
    */ 
    public ComponentMover(Component destinationComponent, Component... components) { 
     this.destinationComponent = destinationComponent; 
     registerComponent(components); 
    } 

    /** 
    * Get the auto layout property 
    * 
    * @return the auto layout property 
    */ 
    public boolean isAutoLayout() { 
     return autoLayout; 
    } 

    /** 
    * Set the auto layout property 
    * 
    * @param autoLayout when true layout will be invoked on the parent 
    * container 
    */ 
    public void setAutoLayout(boolean autoLayout) { 
     this.autoLayout = autoLayout; 
    } 

    /** 
    * Get the change cursor property 
    * 
    * @return the change cursor property 
    */ 
    public boolean isChangeCursor() { 
     return changeCursor; 
    } 

    /** 
    * Set the change cursor property 
    * 
    * @param changeCursor when true the cursor will be changed to the 
    * Cursor.MOVE_CURSOR while the mouse is pressed 
    */ 
    public void setChangeCursor(boolean changeCursor) { 
     this.changeCursor = changeCursor; 
    } 

    /** 
    * Get the drag insets 
    * 
    * @return the drag insets 
    */ 
    public Insets getDragInsets() { 
     return dragInsets; 
    } 

    /** 
    * Set the drag insets. The insets specify an area where mouseDragged events 
    * should be ignored and therefore the component will not be moved. This 
    * will prevent these events from being confused with a MouseMotionListener 
    * that supports component resizing. 
    * 
    * @param dragInsets 
    */ 
    public void setDragInsets(Insets dragInsets) { 
     this.dragInsets = dragInsets; 
    } 

    /** 
    * Get the bounds insets 
    * 
    * @return the bounds insets 
    */ 
    public Insets getEdgeInsets() { 
     return edgeInsets; 
    } 

    /** 
    * Set the edge insets. The insets specify how close to each edge of the 
    * parent component that the child component can be moved. Positive values 
    * means the component must be contained within the parent. Negative values 
    * means the component can be moved outside the parent. 
    * 
    * @param edgeInsets 
    */ 
    public void setEdgeInsets(Insets edgeInsets) { 
     this.edgeInsets = edgeInsets; 
    } 

    /** 
    * Remove listeners from the specified component 
    * 
    * @param component the component the listeners are removed from 
    */ 
    public void deregisterComponent(Component... components) { 
     for (Component component : components) { 
      component.removeMouseListener(this); 
     } 
    } 

    /** 
    * Add the required listeners to the specified component 
    * 
    * @param component the component the listeners are added to 
    */ 
    public void registerComponent(Component... components) { 
     for (Component component : components) { 
      component.addMouseListener(this); 
     } 
    } 

    /** 
    * Get the snap size 
    * 
    * @return the snap size 
    */ 
    public Dimension getSnapSize() { 
     return snapSize; 
    } 

    /** 
    * Set the snap size. Forces the component to be snapped to the closest grid 
    * position. Snapping will occur when the mouse is dragged half way. 
    */ 
    public void setSnapSize(Dimension snapSize) { 
     if (snapSize.width < 1 
       || snapSize.height < 1) { 
      throw new IllegalArgumentException("Snap sizes must be greater than 0"); 
     } 

     this.snapSize = snapSize; 
    } 

    /** 
    * Setup the variables used to control the moving of the component: 
    * 
    * source - the source component of the mouse event destination - the 
    * component that will ultimately be moved pressed - the Point where the 
    * mouse was pressed in the destination component coordinates. 
    */ 
    @Override 
    public void mousePressed(MouseEvent e) { 
     source = e.getComponent(); 
     int width = source.getSize().width - dragInsets.left - dragInsets.right; 
     int height = source.getSize().height - dragInsets.top - dragInsets.bottom; 
     Rectangle r = new Rectangle(dragInsets.left, dragInsets.top, width, height); 

     if (r.contains(e.getPoint())) { 
      setupForDragging(e); 
     } 
    } 

    private void setupForDragging(MouseEvent e) { 
     source.addMouseMotionListener(this); 
     potentialDrag = true; 

     // Determine the component that will ultimately be moved 

     if (destinationComponent != null) { 
      destination = destinationComponent; 
     } else if (destinationClass == null) { 
      destination = source; 
     } else // forward events to destination component 
     { 
      destination = SwingUtilities.getAncestorOfClass(destinationClass, source); 
     } 

     pressed = e.getLocationOnScreen(); 
     location = destination.getLocation(); 

     if (changeCursor) { 
      originalCursor = source.getCursor(); 
      source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
     } 

     // Making sure autoscrolls is false will allow for smoother dragging of 
     // individual components 

     if (destination instanceof JComponent) { 
      JComponent jc = (JComponent) destination; 
      autoscrolls = jc.getAutoscrolls(); 
      jc.setAutoscrolls(false); 
     } 
    } 

    /** 
    * Move the component to its new location. The dragged Point must be in the 
    * destination coordinates. 
    */ 
    @Override 
    public void mouseDragged(MouseEvent e) { 
     Point dragged = e.getLocationOnScreen(); 
     int dragX = getDragDistance(dragged.x, pressed.x, snapSize.width); 
     int dragY = getDragDistance(dragged.y, pressed.y, snapSize.height); 

     int locationX = location.x + dragX; 
     int locationY = location.y + dragY; 

     // Mouse dragged events are not generated for every pixel the mouse 
     // is moved. Adjust the location to make sure we are still on a 
     // snap value. 

     while (locationX < edgeInsets.left) { 
      locationX += snapSize.width; 
     } 

     while (locationY < edgeInsets.top) { 
      locationY += snapSize.height; 
     } 

     Dimension d = getBoundingSize(destination); 

     while (locationX + destination.getSize().width + edgeInsets.right > d.width) { 
      locationX -= snapSize.width; 
     } 

     while (locationY + destination.getSize().height + edgeInsets.bottom > d.height) { 
      locationY -= snapSize.height; 
     } 

     // Adjustments are finished, move the component 

     destination.setLocation(locationX, locationY); 
    } 

    /* 
    * Determine how far the mouse has moved from where dragging started 
    * (Assume drag direction is down and right for positive drag distance) 
    */ 
    private int getDragDistance(int larger, int smaller, int snapSize) { 
     int halfway = snapSize/2; 
     int drag = larger - smaller; 
     drag += (drag < 0) ? -halfway : halfway; 
     drag = (drag/snapSize) * snapSize; 

     return drag; 
    } 

    /* 
    * Get the bounds of the parent of the dragged component. 
    */ 
    private Dimension getBoundingSize(Component source) { 
     if (source instanceof Window) { 
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
      Rectangle bounds = env.getMaximumWindowBounds(); 
      return new Dimension(bounds.width, bounds.height); 
     } else { 
      return source.getParent().getSize(); 
     } 
    } 

    /** 
    * Restore the original state of the Component 
    */ 
    @Override 
    public void mouseReleased(MouseEvent e) { 
     super.mouseReleased(e); 
     if (!potentialDrag) { 
      return; 
     } 

     source.removeMouseMotionListener(this); 
     potentialDrag = false; 

     if (changeCursor) { 
      source.setCursor(originalCursor); 
     } 

     if (destination instanceof JComponent) { 
      ((JComponent) destination).setAutoscrolls(autoscrolls); 
     } 

     // Layout the components on the parent container 

     if (autoLayout) { 
      if (destination instanceof JComponent) { 
       ((JComponent) destination).revalidate(); 
      } else { 
       destination.revalidate(); 
      } 
      destination.repaint(); 
     } 
    } 
} 
+2

非常感謝你!也感謝所有幫助我解決問題的人!對不起,我沒有早點回到你身邊,我不得不離開這個城市度假,把我的工作留下。我感謝您花時間幫助我解決這個問題,併爲我提供一個完全符合我的要求的例子,你們是最棒的! –