如何使用拖放交换GridLayout中排列的按钮上的图标?

问题描述 投票:1回答:1

我正在尝试创建各种类型的拼图游戏,我将图像加载到5x5网格上,裁剪图像并将每个裁剪的部分分配为图标,以25个按钮排列在同一模式中。我希望能够将鼠标指针从按钮拖到另一个按钮并交换这两个按钮上的图标。

我尝试过使用多个MouseListener方法和MouseMotionListener方法,但到目前为止还没有任何工作。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.Component;

import javax.swing.*;
//import java.awt.image.*;
public class ImageMove {
    JFrame jf;
    JButton[][] grid;
    JLabel test;
    public static void main(String[] args) {
        ImageMove ob = new ImageMove();
        ob.start();
    }
    public void start() {
        jf = new JFrame();
        JPanel gridPanel = new JPanel();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        ImageIcon img = new ImageIcon("download.jpg");
        Image temp1= img.getImage();
        img=new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
        Image img1 = img.getImage();

        gridPanel.setLayout(new GridLayout (5,5));
        grid = new JButton[5][5];
        for(int y=0;y<5;y++) {
            for(int x=0; x<5; x++) {
                Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
                ImageIcon icon = new ImageIcon(image);
                JButton temp = new JButton(icon);
                temp.setTransferHandler(new TransferHandler("icon"));
                temp.addMouseMotionListener(new DragMouseAdapter());
                grid[x][y]=temp;                
                gridPanel.add(grid[x][y]);
            }
        }
        test= new JLabel();
        jf.getContentPane().add(BorderLayout.NORTH, test);
        jf.add(gridPanel);
        jf.pack();
        jf.setSize(500, 500);
        jf.setVisible(true);
    }
    class DragMouseAdapter extends MouseAdapter{
        private int x, y;
        public void mouseDragged(MouseEvent e) {
            final int x0=MouseInfo.getPointerInfo().getLocation().x;
            final int y0=MouseInfo.getPointerInfo().getLocation().y;
            x=x0;
            y=y0;
            JButton c = (JButton) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);

            }
        public void mouseReleased(MouseEvent e) {
            JButton c = (JButton) e.getSource();
            Icon icon = c.getIcon();
            grid[((int)(x/100))][((int)(y/100))].setIcon(icon);

        }

    }


}

目前,程序将图标从第一个按钮复制到第二个按钮,即它用第一个按钮替换第二个按钮的图标,但第一个按钮保持不变。我希望完全交换这两个图标。到最后的MouseDragged方法正在执行所描述的行为,但MouseReleased似乎根本不做任何事情。

任何帮助深表感谢。

java swing user-interface awt mouseevent
1个回答
0
投票

这很容易。您只需要在某个位置保存拖动源按钮,然后在drop上将其图标替换为drop target按钮的旧图标。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.Icon;
import javax.swing.ImageIcon;
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 java.awt.image.*;
public class ImageMove {
    JFrame jf;

    JButton[][] grid;

    JLabel test;

    JButton dragSource; // here we save drag source component

    public static void main(String[] args) {
        ImageMove ob = new ImageMove();
        ob.start();
    }

    public void start() {
        jf = new JFrame();
        JPanel gridPanel = new JPanel();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon img = new ImageIcon("download.jpg");
        Image temp1 = img.getImage();
        img = new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
        Image img1 = img.getImage();

        gridPanel.setLayout(new GridLayout(5, 5));
        grid = new JButton[5][5];
        for (int y = 0; y < 5; y++) {
            for (int x = 0; x < 5; x++) {
                Image image = Toolkit.getDefaultToolkit()
                        .createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
                ImageIcon icon = new ImageIcon(image);
                JButton temp = new JButton(icon);
                // use own extension of TransferHandler
                temp.setTransferHandler(new MyTransferHandler("icon"));
                // start drag on mouse pressed event.
                temp.addMouseListener(new DragMouseAdapter());
                grid[x][y] = temp;
                gridPanel.add(grid[x][y]);
            }
        }
        test = new JLabel();
        jf.getContentPane().add(BorderLayout.NORTH, test);
        jf.add(gridPanel);
        jf.pack();
        jf.setSize(500, 500);
        jf.setVisible(true);
    }

    class DragMouseAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            JButton c = (JButton) e.getSource();
            dragSource = c;
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }

    }

    private class MyTransferHandler extends TransferHandler {
        public MyTransferHandler(String property) {
            super(property);
        }

        @Override
        public boolean importData(JComponent comp, Transferable t) {
            Icon targetIcon = ((JButton) comp).getIcon();
            boolean result = super.importData(comp, t);
            if (dragSource != null) {
                dragSource.setIcon(targetIcon);
                dragSource = null;
            }
            return result;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.