Java 如何使用ImageIcon制作可拖动组件

Java 如何使用ImageIcon制作可拖动组件,java,swing,draggable,jlabel,layout-manager,Java,Swing,Draggable,Jlabel,Layout Manager,我正在尝试为国际象棋游戏构建一个用户界面。我使用了一个网格布局填充了JLabels,棋子是JLabels的图像图标 现在我想通过在黑板上拖动来移动这些碎片。有没有办法用图像图标来实现这一点?还是有更好的方法来解决这个问题 编辑:下面是一个示例代码。您可以注意到,您可以移动图标图像,但它不会用鼠标“拖动” import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.C

我正在尝试为国际象棋游戏构建一个用户界面。我使用了一个网格布局填充了JLabels,棋子是JLabels的图像图标

现在我想通过在黑板上拖动来移动这些碎片。有没有办法用图像图标来实现这一点?还是有更好的方法来解决这个问题

编辑:下面是一个示例代码。您可以注意到,您可以移动图标图像,但它不会用鼠标“拖动”

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;




public class MainDebug extends JFrame implements MouseListener {

    private JPanel BoardPanel;


    private String buffercase_mousepressed;
    private String buffercase_mouseentered;

    private JLabel A8 = new JLabel("A8");
    private JLabel B8 = new JLabel("B8");

    private HashMap componentMap;

    private ImageIcon RookIcon = createImageIcon("50px-Rook.png", "Rook");



    public MainDebug(String name) {
        super(name);
        setResizable(true);

    }

    private ImageIcon createImageIcon(String path, String description) {

        java.net.URL imgURL = getClass().getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL, description);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
    }

    public void addComponentsToPane(final Container pane) {

        BoardPanel = new JPanel();
        BoardPanel.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        Dimension dim50 = new Dimension(50,50);

        A8.setOpaque(true);
        A8.setBackground(Color.white);
        A8.setPreferredSize(dim50);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        BoardPanel.add(A8,c);
        A8.setName("A8");
        A8.addMouseListener(this);


        B8.setOpaque(true);
        B8.setBackground(Color.lightGray);
        B8.setPreferredSize(dim50);
        B8.setName("B8");
        c.gridx=1;
        BoardPanel.add(B8,c);
        B8.addMouseListener(this);

        A8.setIcon(RookIcon);

        pane.add(BoardPanel, BorderLayout.CENTER);

        createComponentMap();

    }

    private void createComponentMap() {
        componentMap = new HashMap<String,Component>();

        int max_components = BoardPanel.getComponentCount();
        //Component[] components = BoardPanel.getComponentCount();
        //Component[] components = BoardPanel.getContentPane().getComponents();
        for (int i=0; i < max_components; i++) {
                componentMap.put(BoardPanel.getComponent(i).getName(), BoardPanel.getComponent(i));
        }
    }

    public Component getComponentByName(String name) {
        if (componentMap.containsKey(name)) {
                return (Component) componentMap.get(name);
        }
        else return null;
}

    public void mousePressed(MouseEvent e) {

        buffercase_mousepressed = e.getComponent().getName();
    }

    public void mouseReleased(MouseEvent e) {

        moveIcon(buffercase_mousepressed,buffercase_mouseentered);
    }

    public void mouseEntered(MouseEvent e) {

        buffercase_mouseentered = e.getComponent().getName();
    }


    public void mouseExited(MouseEvent e) {

    }

    public void mouseClicked(MouseEvent e) {

    }


    public void moveIcon(String A, String B){

        if ((A != null) && (B != null)){

            JLabel Ja = (JLabel)getComponentByName(A);
            JLabel Jb = (JLabel)getComponentByName(B);

            Icon iconeA = Ja.getIcon();
            Icon iconeB = Jb.getIcon();

            if (iconeA != null && iconeB == null){

                Ja.setIcon(null);
                Jb.setIcon(iconeA);

            }

        }

        buffercase_mousepressed = null;
        buffercase_mouseentered = null;
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        MainDebug frame = new MainDebug("Test interface");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });


    }

}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.util.HashMap;
导入javax.swing.Icon;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类MainDebug扩展JFrame实现MouseListener{
私人JPanel董事会;
私有字符串缓冲区(鼠标按下);
私用字符串缓冲区大小写;
专用JLabel A8=新JLabel(“A8”);
专用JLabel B8=新JLabel(“B8”);
私有HashMap组件映射;
私有ImageIcon RookIcon=createImageIcon(“50px Rook.png”,“Rook”);
公共MainDebug(字符串名称){
超级(姓名);
可设置大小(真);
}
private ImageIcon createImageIcon(字符串路径、字符串描述){
java.net.URL imgURL=getClass().getResource(路径);
如果(imgURL!=null){
返回新的图像图标(imgURL,description);
}否则{
System.err.println(“找不到文件:+path”);
返回null;
}
}
公共void addComponentsToPane(最终容器窗格){
BoardPanel=新的JPanel();
BoardPanel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
尺寸dim50=新尺寸(50,50);
A8.设置不透明(真);
A8.立根背景(颜色:白色);
A8.设置首选尺寸(dim50);
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=0;
c、 gridy=0;
配电盘。添加(A8,c);
A8.集合名(“A8”);
A8.addMouseListener(本);
B8.设置不透明(真);
B8.挫折背景(颜色:浅灰色);
B8.设置首选尺寸(dim50);
B8.集合名(“B8”);
c、 gridx=1;
配电盘。添加(B8,c);
B8.addMouseListener(本);
A8.设置图标(RookIcon);
添加(BoardPanel,BorderLayout.CENTER);
createComponentMap();
}
私有void createComponentMap(){
componentMap=新的HashMap();
int max_components=BoardPanel.getComponentCount();
//Component[]components=BoardPanel.getComponentCount();
//Component[]components=BoardPanel.getContentPane().getComponents();
对于(int i=0;i
几年前,我编写了一个框架来定制Swing控件

也许你可以把它作为一个开始,并根据你的用例调整它

教程:

Javadoc:

马文:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-swing-customizer</artifactId>  
    <version>0.3</version>  
</dependency>  

org.softsmithy.lib
替换为棋盘布局,也可能扩展

您可以使用用于图像/图标支持的。 您还可能希望从JXIconCustomizer的可自定义属性集中删除“宽度”和“高度”,以防止用户更改图像的大小:

您可以在此处找到有关最新版本的更多信息:

如果出于某种原因,您觉得这种方法不适合您,您可以查看源代码(该库是开源的),以获得一些起点:


好吧,这是我对你的小问题

现在,我没有使用
GridBagLayout
,而是设计了自己的布局管理器,它允许我指定工件的“网格”位置
public class Chess {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Board());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public static final int GRID_SIZE = 50;
    public static final boolean SNAP_TO_GRID = false;

    public class Board extends JPanel {

        private BufferedImage board;
        private Point highlightCell;

        public Board() {
            setLayout(new BoardLayoutManager());
            int width = GRID_SIZE * 8;
            int height = GRID_SIZE * 8;
            board = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = board.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fill(new Rectangle(0, 0, width, height));
            g2d.setColor(Color.BLACK);
            for (int row = 0; row < 8; row++) {
                int xPos = (row % 2 == 0) ? GRID_SIZE : 0;
                for (int col = 0; col < 8; col += 2) {
                    g2d.fill(new Rectangle(xPos, row * GRID_SIZE, GRID_SIZE, GRID_SIZE));
                    xPos += (GRID_SIZE * 2);
                }
            }
            JLabel piece = new JLabel();
            try {
                piece.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Luke.png"))));
            } catch (IOException ex) {
                piece.setBackground(new Color(255, 0, 0, 64));
                piece.setOpaque(true);
            }

            add(piece, new Point(0, 0));

            MouseHandler mouseHandler = new MouseHandler(this);
            addMouseListener(mouseHandler);
            addMouseMotionListener(mouseHandler);

        }

        protected Rectangle getBoardBounds() {
            return new Rectangle(getBoardOffset(), new Dimension(GRID_SIZE * 8, GRID_SIZE * 8));
        }

        public Point gridToPoint(Point grid) {
            Point p = new Point();
            if (grid != null) {
                Point offset = getBoardOffset();
                p.x = grid.x * GRID_SIZE + offset.x;
                p.y = grid.y * GRID_SIZE + offset.y;
            }
            return p;
        }

        public Point pointToGrid(Point p) {
            Point grid = null;
            Rectangle board = getBoardBounds();
            if (board.contains(p)) {
                p.x = p.x - board.x;
                p.y = p.y - board.y;

                grid = new Point();
                grid.x = p.x / GRID_SIZE;
                grid.y = p.y / GRID_SIZE;
            }
            return grid;
        }

        public void setPieceGrid(Component comp, Point grid) {
            ((BoardLayoutManager) getLayout()).setPieceGrid(comp, grid);
            invalidate();
            revalidate();
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(GRID_SIZE * 8, GRID_SIZE * 8);
        }

        protected Point getBoardOffset() {
            int width = getWidth();
            int height = getHeight();
            Point p = new Point();
            p.x = (width - board.getWidth()) / 2;
            p.y = (height - board.getHeight()) / 2;

            return p;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            Point p = getBoardOffset();
            g2d.drawImage(board, p.x, p.y, this);
            if (highlightCell != null) {
                Point cell = gridToPoint(highlightCell);
                Rectangle bounds = new Rectangle(cell.x, cell.y, GRID_SIZE, GRID_SIZE);
                g2d.setColor(Color.RED);
                g2d.draw(bounds);
            }
            g2d.dispose();
        }

        public void setHightlightCell(Point p) {
            if (highlightCell != p) {
                highlightCell = p;
                repaint();
            }
        }

    }

    public class MouseHandler extends MouseAdapter {

        private Component dragComponent;
        private Board board;
        private Point dragOffset;

        public MouseHandler(Board board) {
            this.board = board;
        }

        public Board getBoard() {
            return board;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Component comp = getBoard().getComponentAt(e.getPoint());
            if (comp != null) {
                dragComponent = comp;
                dragOffset = new Point();
                dragOffset.x = e.getPoint().x - comp.getX();
                dragOffset.y = e.getPoint().y - comp.getY();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (dragComponent != null) {
                Board board = getBoard();
                Point p = board.pointToGrid(e.getPoint());
                System.out.println(p);
                board.setPieceGrid(dragComponent, p);

                dragComponent = null;
                board.setHightlightCell(null);
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (dragComponent != null) {
                Board board = getBoard();
                Point grid = board.pointToGrid(e.getPoint());
                if (SNAP_TO_GRID) {
                    Point p = board.gridToPoint(grid);
                    dragComponent.setLocation(p);
                } else {
                    Point dragPoint = new Point();
                    dragPoint.x = e.getPoint().x - dragOffset.x;
                    dragPoint.y = e.getPoint().y - dragOffset.y;
                    dragComponent.setLocation(dragPoint);
                }
                board.setHightlightCell(grid);
            }
        }

    }

    public class BoardLayoutManager implements LayoutManager2 {

        private Map<Component, Point> mapGrid;

        public BoardLayoutManager() {
            mapGrid = new HashMap<>(25);
        }

        public void setPieceGrid(Component comp, Point grid) {
            mapGrid.put(comp, grid);
        }

        @Override
        public void addLayoutComponent(Component comp, Object constraints) {
            if (constraints instanceof Point) {
                mapGrid.put(comp, (Point) constraints);
            } else {
                throw new IllegalArgumentException("Unexpected constraints, expected java.awt.Point, got " + constraints);
            }
        }

        @Override
        public Dimension maximumLayoutSize(Container target) {
            return new Dimension(GRID_SIZE * 8, GRID_SIZE * 8);
        }

        @Override
        public float getLayoutAlignmentX(Container target) {
            return 0.5f;
        }

        @Override
        public float getLayoutAlignmentY(Container target) {
            return 0.5f;
        }

        @Override
        public void invalidateLayout(Container target) {
        }

        @Override
        public void addLayoutComponent(String name, Component comp) {
        }

        @Override
        public void removeLayoutComponent(Component comp) {
            mapGrid.remove(comp);
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return new Dimension(GRID_SIZE * 8, GRID_SIZE * 8);
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return new Dimension(GRID_SIZE * 8, GRID_SIZE * 8);
        }

        @Override
        public void layoutContainer(Container parent) {
            Point offset = ((Board) parent).getBoardOffset();
            for (Component comp : parent.getComponents()) {
                Point p = mapGrid.get(comp);
                if (p == null) {
                    comp.setBounds(0, 0, 0, 0); // Remove from sight :P
                } else {
                    int x = p.x * GRID_SIZE + offset.x;
                    int y = p.y * GRID_SIZE + offset.y;
                    comp.setBounds(x, y, GRID_SIZE, GRID_SIZE);
                }
            }
        }
    }    
}
// the shared mouseListener
MouseListener listener = new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        JComponent c = (JComponent) e.getSource();
        TransferHandler handler = c.getTransferHandler();
        handler.exportAsDrag(c, e, TransferHandler.COPY);
    }
};
// the shared TransferHandler
// super supports copy only, so it needs to do the move itself
// taking the lazy way of null the icon on the source 
TransferHandler handler = new TransferHandler("icon") {

    private JComponent source;

    @Override
    public void exportAsDrag(JComponent comp, InputEvent e, int action) {
        super.exportAsDrag(comp, e, action);
    }

    @Override
    public boolean canImport(TransferSupport support) {
        // empty fields only 
        return (((JLabel) support.getComponent()).getIcon() == null) 
                && super.canImport(support); 
    }

    @Override
    protected void exportDone(JComponent source, Transferable data,
            int action) {
        ((JLabel) source).setIcon(null);
    }

};
// lazy me: a one row board
JComponent board = new JPanel(new GridLayout(0, 8));
Color[] colors = new Color[] {Color.WHITE, Color.BLACK};
for (int column = 0; column < 8; column++) {
    // filled with labels as fields
    board.add(createField(colors[column % 2], listener, handler));
}
Icon figure = XTestUtils.loadDefaultIcon();
((JLabel) board.getComponent(0)).setIcon(figure);

// create and configure a JLabel as field
private JLabel createField(Color color, MouseListener listener,
        TransferHandler handler) {
    JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground(color);
    label.addMouseListener(listener);
    label.setTransferHandler(handler);
    return label;
}