亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java-如何拖放JPanel及其組件

Java-如何拖放JPanel及其組件

冉冉說 2019-12-03 14:49:32
我有一個關于拖放的問題:我可以拖放標簽,文本或圖標。但是我想拖放一個JPanel及其所有組件(標簽,文本框等)。我怎樣才能做到這一點 ?
查看完整描述

3 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

該代碼是對MadProgrammer的巨大幫助。對于任何想要使用這些類,但想要從要拖動的面板中的按鈕發起拖動的人,我只需將擴展的JPanel替換為一個JButton,即可在構造函數中使用面板:


public class DragActionButton  extends JButton {

  private DragGestureRecognizer dgr;

  private DragGestureHandler dragGestureHandler;

  private JPanel actionPanel;

DragActionButton (JPanel actionPanel, String buttonText)

 {

 this.setText(buttonText);

 this.actionPanel = actionPanel;

 }   


@Override

public void addNotify() {


    super.addNotify();


    if (dgr == null) {


        dragGestureHandler = new DragGestureHandler(this.actionPanel);

        dgr = DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(

                this,

                DnDConstants.ACTION_MOVE,

                dragGestureHandler);


    }


}


@Override

public void removeNotify() {


    if (dgr != null) {


        dgr.removeDragGestureListener(dragGestureHandler);

        dragGestureHandler = null;


    }


    dgr = null;


    super.removeNotify();


}


}

那么您可以在創建按鈕時執行此操作:


  this.JButtonDragIt = new DragActionButton(this.JPanel_To_Drag, "button-text-here");


查看完整回答
反對 回復 2019-12-03
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

在該DragGestureHandler::dragGestureRecognized方法中,變量被調用parent,因此隱藏了實例的parent變量。解決起來很容易。當我使用給定的代碼時,我得到一個java.awt.dnd.InvalidDnDOperationException: Cannot find top-level for the drag source component。如果我將第一行(直到parent.repaint();方法的最底部,則不再拋出異常。區別在于new PanelTransferable(getPanel());從面板中 獲取了。為什么會這樣呢?)

查看完整回答
反對 回復 2019-12-03
  • 3 回答
  • 0 關注
  • 655 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號