第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Java-如何拖放JPanel及其組件

Java-如何拖放JPanel及其組件

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

3 回答

?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊

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


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();


}


}

那么您可以在創(chuàng)建按鈕時執(zhí)行此操作:


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


查看完整回答
反對 回復(fù) 2019-12-03
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗 獲得超13個贊

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

查看完整回答
反對 回復(fù) 2019-12-03
  • 3 回答
  • 0 關(guān)注
  • 653 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號