久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java 中從 JButton 拖放到 JComponent

Drag and Drop from JButton to JComponent in Java(在 Java 中從 JButton 拖放到 JComponent)
本文介紹了在 Java 中從 JButton 拖放到 JComponent的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在互聯網上搜索了如何將 JButtons 拖放到對象的示例,但我無法使其工作.

我的程序所做的是,當我單擊按鈕時,對象會更新一個字段(使用 selectedobject.setField()).我希望能夠通過單擊而不是通過拖動 JButton 來做到這一點.

我該怎么做?我找到了這個,并嘗試輸入我的代碼:

btn.setTransferHandler(new ImageHandler());btn.addMouseListener(新鼠標適配器(){公共無效鼠標按下(鼠標事件e){JComponent c = (JComponent)e.getSource();TransferHandler 處理程序 = c.getTransferHandler();handler.exportAsDrag(c, e, TransferHandler.COPY);}});

我從 作為另一個示例...

I searched on the internet for examples how to Drag and Drop JButtons to an Object but I could not make it work.

What my program does, is that when I click on a button, the object updated a field (with a selectedobject.setField()). I want to be able to do this not by clicking, but by dragging the JButton.

How can I do this ? I found this, and I tried to put in my code:

btn.setTransferHandler(new ImageHandler());
btn.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
      JComponent c = (JComponent)e.getSource();
      TransferHandler handler = c.getTransferHandler();
      handler.exportAsDrag(c, e, TransferHandler.COPY);
            }           
});

I took the ImageHandler class from here.

解決方案

Drag'n'drop is a fun bag of crunchy, munchy carrots...not helped by the fact that there is a "core" API and the newer "transfer" API, so it's really easy to get confused

The following example uses the "transfer" API and basically transfers a String value from a button to a JLabel.

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(1, 2));
            add(createLeftPanel());
            add(createRightPanel());

        }

        protected JPanel createLeftPanel() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            for (int index = 0; index < 10; index++) {
                JButton btn = new JButton(Integer.toString(index + 1));
                panel.add(btn, gbc);
                btn.setTransferHandler(new ValueExportTransferHandler(Integer.toString(index + 1)));

                btn.addMouseMotionListener(new MouseAdapter() {
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        JButton button = (JButton) e.getSource();
                        TransferHandler handle = button.getTransferHandler();
                        handle.exportAsDrag(button, e, TransferHandler.COPY);
                    }
                });
            }
            return panel;
        }

        protected JPanel createRightPanel() {
            JPanel panel = new JPanel(new GridBagLayout());
            JLabel label = new JLabel("Drop in");
            label.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(20, 20, 20, 20)));
            label.setTransferHandler(new ValueImportTransferHandler());
            panel.add(label);
            return panel;
        }

    }

    public static class ValueExportTransferHandler extends TransferHandler {

        public static final DataFlavor SUPPORTED_DATE_FLAVOR = DataFlavor.stringFlavor;
        private String value;

        public ValueExportTransferHandler(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        @Override
        public int getSourceActions(JComponent c) {
            return DnDConstants.ACTION_COPY_OR_MOVE;
        }

        @Override
        protected Transferable createTransferable(JComponent c) {
            Transferable t = new StringSelection(getValue());
            return t;
        }

        @Override
        protected void exportDone(JComponent source, Transferable data, int action) {
            super.exportDone(source, data, action);
            // Decide what to do after the drop has been accepted
        }

    }

    public static class ValueImportTransferHandler extends TransferHandler {

        public static final DataFlavor SUPPORTED_DATE_FLAVOR = DataFlavor.stringFlavor;

        public ValueImportTransferHandler() {
        }

        @Override
        public boolean canImport(TransferHandler.TransferSupport support) {
            return support.isDataFlavorSupported(SUPPORTED_DATE_FLAVOR);
        }

        @Override
        public boolean importData(TransferHandler.TransferSupport support) {
            boolean accept = false;
            if (canImport(support)) {
                try {
                    Transferable t = support.getTransferable();
                    Object value = t.getTransferData(SUPPORTED_DATE_FLAVOR);
                    if (value instanceof String) {
                        Component component = support.getComponent();
                        if (component instanceof JLabel) {
                            ((JLabel) component).setText(value.toString());
                            accept = true;
                        }
                    }
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
            return accept;
        }
    }
}

I've gone out my way to separate the TransferHandlers allowing for a "drag" and "drop" version. You don't "have" to do this and you "could" use a single TransferHandler to perform both operations, that's up to you.

You will have to modify the ValueExportTransferHandler to accept different values and modify the SUPPORTED_DATE_FLAVOR accordingingly, but those are the basics

You could also have a look at Drag and Drop custom object from JList into JLabel as another example...

這篇關于在 Java 中從 JButton 拖放到 JComponent的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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:獲取當前星期幾的值)
主站蜘蛛池模板: 精品久久久久久红码专区 | 精品欧美一区二区在线观看 | 国产精品国产精品 | 免费精品国产 | 黄色毛片免费看 | 先锋av资源网 | 久久久久国产精品一区二区 | 久久综合九九 | 亚洲一区二区三区四区五区中文 | 精品一区二区三区四区在线 | 午夜免费在线电影 | 高清国产一区二区 | 91久久 | 成人免费视频网站在线观看 | 国产精品一区二区久久 | 中文字幕不卡在线观看 | 欧美日韩一区二区三区四区 | 在线视频国产一区 | 亚洲 欧美 另类 综合 偷拍 | 国产精品国产成人国产三级 | 亚洲国产中文在线 | 日韩免费av网站 | 成年人免费看 | 国产999精品久久久久久 | 中文字幕一区二区三区日韩精品 | 91精品国产777在线观看 | 亚洲欧美日韩国产综合 | av网站观看 | 五月婷婷色 | 欧美精品一区二区三区一线天视频 | 黄色网址免费看 | 国产欧美日韩精品一区二区三区 | jdav视频在线观看免费 | 欧洲精品一区 | 亚洲一区中文字幕 | 中国大陆高清aⅴ毛片 | 日本精品视频在线观看 | 欧美精品久久 | 国产久| 在线视频日韩精品 | 亚洲一区av|