pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

爪哇.拖動&將 ImageIcon 從面板 1 上的 JLabel

Java. Drag amp; Drop ImageIcon from JLabel on panel 1, to JLabel on panel 2. Add Counter function(爪哇.拖動amp;將 ImageIcon 從面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加計數器功能) - IT屋-程序員軟件開發技術
本文介紹了爪哇.拖動&將 ImageIcon 從面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加計數器功能的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我已經實現了這個非常基本的,在兩個 JPanel 之間拖放,但這并不能真正滿足我的要求!

I've implemented this very basic, drag and drop between two JPanels, but this doesn't really meet my requirements!

public class test extends JFrame {

{    JPanel mainpanel, storypanel, imageselect;



    public test(){

          mainpanel = new JPanel(new BorderLayout());
          storypanel = new JPanel();
          imageselect = new JPanel();
            MouseListener listener = new MouseAdapter(){
            public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
        };

            int j = 0;
            BufferedImage myImages;
            JLabel imgselect = new JLabel();
            try { myImages = ImageIO.read(new File("four.jpg"));
            //myImages[j] = resize(myImages[j]);
            imgselect= new JLabel(new ImageIcon(myImages));
            System.out.println(j);
            imageselect.add(imgselect);
            imgselect.addMouseListener(listener);
            imgselect.setTransferHandler(new TransferHandler("icon"));

            } catch(Exception e) {};

            int i = 0;
            BufferedImage storyimages;
            JLabel storylabel = new JLabel();


            //targetImg = new ImageIcon("lmkpackage/base/TargetImg.jpg");
            try{ storyimages = ImageIO.read(new File("TargetImg.jpg"));
            //storyimages[i] = resize(storyimages[i]);
            storylabel = new JLabel(new ImageIcon(storyimages));
            System.out.println(i);
            storypanel.add(storylabel); 
            storylabel.addMouseListener(listener);
            storylabel.setTransferHandler(new TransferHandler("icon"));
            } catch(Exception e) {};


            mainpanel.add(imageselect, BorderLayout.NORTH);
            mainpanel.add(storypanel, BorderLayout.SOUTH);

            getContentPane().add(mainpanel);



        }

        public static void main(String[] args){

        System.out.println("Application Running");
        JFrame mainframe =  new test();
            mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setSize(1000,1000);
            mainframe.setVisible(true);

        }

    }

對不起,我在這里不是很清楚.我試圖將 ImageIcon 從圖像選擇面板上的 JLabel 拖到故事情節面板的另一個 JLabel 上.我可以用上面的代碼做到這一點.但是當我這樣做時,我可以從圖像選擇面板中拖動一個 ImageIcon,并在同一面板上替換另一個 ImageIcon.我不希望這種情況發生.我也可以從故事情節面板拖動到圖像選擇面板,這是我不想要的.

Sorry I wasn't very clear here. Im trying to drag an ImageIcon from a JLabel on the imageselect panel, to another JLabel the storyline panel. I can do this, with the above code. But when I do this, I can drag an ImageIcon from the imageselect panel, and replace another ImageIcon on the same panel. I do NOT want this to happen. I can also drag from the storyline panel to the imageselect panel, which I do NOT want.

我不是要求被勺子喂食代碼,我只是在尋找正確方向的推動力!

I'm not asking to be spoon fed code, I'm just looking for a push in the right direction!

我想知道是否有任何方法可以計算成功的拖放操作,下面的解決方案回答了我原來的問題.

I'm wondering is there any way of counting a successful drag and drop operation, the solution below answered my original question.

推薦答案

這基本上是@Andrew 評論的大綱 - 由于您的 SSCCE 很容易實現 :-)

This is basically an outline of @Andrew's comment - easily possible due to your SSCCE :-)

子類 TransferHandler,覆蓋 canImport 以檢查源標簽是否在 imageSelect 面板上,如果是則拒絕.

Subclass TransferHandler, override canImport to check if the source label is on the imageSelect panel and reject if so.

    // custom transferHandler which decides about imports based on source
    TransferHandler handler = new TransferHandler("icon") {

        @Override
        public boolean canImport(TransferSupport support) {
            return super.canImport(support) 
                    && support.getComponent().getParent() != imageSelectPanel;
        }

    };
    // use the handler on all labels (handlers can be shared, btw)
    // for each label on imageSelectPanel
    imageSelectLabel.setTransferHandler(handler)
    // for each label on the target panel (aka storyPanel) 
    storyLabel.setTransferHandler(handler)

這篇關于爪哇.拖動&將 ImageIcon 從面板 1 上的 JLabel 拖放到面板 2 上的 JLabel.添加計數器功能的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 济南电缆桥架|山东桥架-济南航丰实业有限公司| 咖啡加盟,咖啡店加盟连锁品牌-卡小逗| 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | hc22_hc22价格_hc22哈氏合金—东锜特殊钢 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 天津暖气片厂家_钢制散热器_天津铜铝复合暖气片_维尼罗散热器 | 成都网站建设制作_高端网站设计公司「做网站送优化推广」 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 蒜肠网-动漫,二次元,COSPLAY,漫展以及收藏型模型,手办,玩具的新媒体.(原变形金刚变迷TF圈) | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 卡诺亚轻高定官网_卧室系统_整家定制_定制家居_高端定制_全屋定制加盟_定制家具加盟_定制衣柜加盟 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 深圳富泰鑫五金_五金冲压件加工_五金配件加工_精密零件加工厂 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | PE一体化污水处理设备_地埋式生活污水净化槽定制厂家-岩康塑业 | 小型数控车床-数控车床厂家-双头数控车床 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | 东莞工作服_东莞工作服定制_工衣订做_东莞厂服 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 太原装修公司_山西整装家装设计_太原室内装潢软装_肖邦家居 | 原色会计-合肥注册公司_合肥代理记账公司_营业执照代办 |