将图像拖动到JButton中

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

我正在研究战舰项目。我创建了两个面板,我放在一个框架中。第一个面板是按钮网格,第二个面板由可拖动的船舶图像组成。我想要的只是当我将图像拖动到某个按钮时,它会出现在该按钮上。换句话说,我想通过将图像拖入到JButton中来简单地添加图像。这是完整的代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class BattleShip extends JFrame{

public BattleShip() throws IOException {
    this.setResizable(false);
    this.pack();
    JPanel panelLeft = new JPanel();
    JPanel panelRight = new JPanel();
    panelLeft.setSize(500, 650);
    panelRight.setSize(200,650);
    panelRight.setBorder(BorderFactory.createLineBorder(Color.RED, 7));
    panelRight.setBackground(Color.WHITE);
    panelLeft.setLayout(new GridLayout(11,11));
    panelRight.setLayout(null);

    BufferedImage myPicture = ImageIO.read(new File("/home/hikmet/Desktop/595a7960d639a15d096a226d.png"));
    BufferedImage[] resized = new BufferedImage[14];
    for (int i = 0; i < 14; ++i) {
        resized[i] = resize(myPicture, 20, 30);
    }
    JLabel[] img = new JLabel[14];
    for (int i = 0; i < 14; ++i) {
        img[i] = new JLabel((new ImageIcon(resized[i])));
    }
    Dimension size = img[0].getPreferredSize();

    for (int i = 0; i < 14; ++i) {
        panelRight.add(img[i]);
        img[i].setBounds(7 + i * 50, 7, size.width, size.height);
    }


    JButton button[][] = new JButton[11][11];
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 11; j++) {
            button[i][j] = new JButton();
            panelLeft.add(button[i][j]);
        }
    }


    button[1][0].setText("A"); button[0][1].setText("1");
    button[2][0].setText("B"); button[0][2].setText("2");
    button[3][0].setText("C"); button[0][3].setText("3");
    button[4][0].setText("D"); button[0][4].setText("4");
    button[5][0].setText("E"); button[0][5].setText("5");
    button[6][0].setText("F"); button[0][6].setText("6");
    button[7][0].setText("G"); button[0][7].setText("7");
    button[8][0].setText("H"); button[0][8].setText("8");
    button[9][0].setText("I"); button[0][9].setText("9");
    button[10][0].setText("J"); button[0][10].setText("10");

    for (int i = 0; i < 14; i++) { //applying mouseEvent to each image
        MouseHandler movement = new MouseHandler(img[i]);
    }

    this.setTitle("BattleShip");
    this.setSize(700,700);
    this.setLayout(new GridLayout(2,4));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.getContentPane().add(panelLeft);
    this.getContentPane().add(panelRight);
}
//method for just resizing the size of image
private static BufferedImage resize(BufferedImage myPicture, int height, int width) {
    Image tmp = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = resized.createGraphics();
    g2d.drawImage(tmp,0,0,null);
    g2d.dispose();
    return resized;
}
//Class for handling mouseEvents
public class MouseHandler implements MouseMotionListener {
private int x, y;

public MouseHandler(JLabel img){
    img.addMouseMotionListener(this);
}

@Override
public void mouseDragged(MouseEvent mouseEvent) {
      mouseEvent.getComponent().setLocation((mouseEvent.getX()+mouseEvent.getComponent().getX())-x, (mouseEvent.getY()+mouseEvent.getComponent().getY())-y);
}

@Override
public void mouseMoved(MouseEvent mouseEvent) {
    x = mouseEvent.getX();
    y = mouseEvent.getY();
  }
}
//Main class
public class Main {
public static void main(String[] args) throws IOException {
    new BattleShip();
  }
}

希望有人能帮助我:)

java swing jpanel mouseevent jbutton
1个回答
0
投票

您可以在使用TransferHandler类时指定要拖动的属性:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DragIcon extends JPanel
{
    public DragIcon()
    {
        setLayout( new BorderLayout(40, 20) );

        TransferHandler iconHandler = new TransferHandler( "icon" );
//      TransferHandler iconHandler = new TransferHandler( "text" );
        MouseListener dragListener = new DragMouseAdapter();

        JLabel dragLabel = new JLabel("Drag");
        dragLabel.setTransferHandler( iconHandler );
        dragLabel.addMouseListener(dragListener);
        dragLabel.setIcon( new ImageIcon("copy16.gif") );
        dragLabel.setHorizontalAlignment(JLabel.CENTER);
        add(dragLabel, BorderLayout.PAGE_START);


        JLabel label = new JLabel("Label");
        label.setTransferHandler( iconHandler );
        add(label, BorderLayout.LINE_START);

        JButton button = new JButton("Button");
        button.setTransferHandler( iconHandler );
        add(button, BorderLayout.LINE_END);
    }

    private class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            JComponent c = (JComponent)e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Drag Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DragIcon());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.