如何在一个JFrame中拖动多个JLabel?

时间:2019-04-06 10:18:08

标签: java

选择多个JLabel之后,我必须同时拖动它们。我想出了如何拖动单个JLabel以及如何选择多个JLabel的方法,但是我却不知道一次只能移动一个以上。

JFrame类的代码

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

public class HallViewer extends JFrame {
    private String nomeSala;
    private JPanel draggableSeatsPanel;
    private List<MyDraggableSeat> draggableSeats = new ArrayList<>();

    public HallViewer(String nomeSala) {
        this.nomeSala = nomeSala;
        initDraggableSeatsList();
        initDraggableSeatsPanel();
        initFrame();
        addMouseListener(mouseHandler);
        addMouseMotionListener(mouseMotionHandler);
    }

    private void initDraggableSeatsList() {
        MyDraggableSeat mds1 = new MyDraggableSeat(253, 210);
        mds1.setText("A1");
        MyDraggableSeat mds2 = new MyDraggableSeat(408, 341);
        mds2.setText("A2");
        MyDraggableSeat mds3 = new MyDraggableSeat(157, 213);
        mds3.setText("A3");
        draggableSeats = new ArrayList<>();
        draggableSeats.add(mds1);
        draggableSeats.add(mds2);
        draggableSeats.add(mds3);
    }

    private void initDraggableSeatsPanel() {
        draggableSeatsPanel = new JPanel();
        draggableSeatsPanel.setMinimumSize(new Dimension(300, 150));
        draggableSeatsPanel.setLayout(null);

        for(MyDraggableSeat mds : draggableSeats) {
            draggableSeatsPanel.add(mds);
            initMouseListenerForMDS(mds);
            repaint();
        }
    }

    private void initFrame() {
        setTitle("Piantina " + nomeSala);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                setVisible(false);
            }
        });
        setLayout(new GridLayout(1,1));
        add(draggableSeatsPanel);
        setSize(700,500);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private int screenX = 0;
    private int screenY = 0;
    private int myX = 0;
    private int myY = 0;

    private void initMouseListenerForMDS(MyDraggableSeat mds) {
        mds.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                screenX = e.getXOnScreen();
                screenY = e.getYOnScreen();
                myX = mds.getX();
                myY = mds.getY();
            }
        });

        mds.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int deltaX = e.getXOnScreen() - screenX;
                int deltaY = e.getYOnScreen() - screenY;
                mds.setLocation(myX + deltaX, myY + deltaY);
            }
        });
    }

    private int xbegin;
    private int ybegin;
    private int xend;
    private int yend;
    private int width;
    private int height;
    private int x;
    private int y;
    private boolean isNewRect = false;
    private List<MyDraggableSeat> selectedMDSList = new ArrayList<>();
    private MouseListener mouseHandler = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            for(MyDraggableSeat mds : selectedMDSList) {
                mds.setBorder(new LineBorder(Color.BLUE, 3));
            }
            selectedMDSList.clear();


            xbegin = e.getX();
            ybegin = e.getY();
            isNewRect = true;
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            xend = e.getX();
            yend = e.getY();
            isNewRect = true;
            repaint();

            Rectangle rectangle = new Rectangle(x,y,width,height);
            for(MyDraggableSeat mds : draggableSeats) {
                if(rectangle.intersects(new Rectangle(mds.getX(), mds.getY(), mds.getWidth(), mds.getHeight()))) {
                    selectedMDSList.add(mds);
                }
            }
            for(MyDraggableSeat mds : selectedMDSList) {
                mds.setBorder(new LineBorder(Color.CYAN, 3));
            }

        }
    };

    private MouseMotionListener mouseMotionHandler = new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            xend = e.getX();
            yend = e.getY();
            isNewRect = false;

            repaint();

        }
    };

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        if((xbegin-xend)<0) {
            width = Math.abs(xbegin-xend);
            x = xbegin;
        } else {
            width = Math.abs(xend-xbegin);
            x = xend;
        }

        if((ybegin-yend)<0) {
            height = Math.abs(ybegin-yend);
            y = ybegin;
        } else {
            height = Math.abs(yend-ybegin);
            y = yend;
        }

        Graphics2D g2D = (Graphics2D) g;
        AlphaComposite transparency = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
        g2D.setComposite(transparency);
        g2D.setColor(new Color(0x0073b5e9));
        if(!isNewRect) {
            g2D.fill(new Rectangle2D.Float(x,y,width,height));
        }
    }

    public static class Main {
        public static void main(String[] argv){ new HallViewer("Example"); }
    }
}

MyDraggableSeat JLabel的代码

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class MyDraggableSeat extends JLabel {

    public MyDraggableSeat(int x, int y) {
        setBorder(new LineBorder(Color.BLUE, 3));
        setBackground(Color.WHITE);
        setBounds(x, y, 30, 25);
        setOpaque(true);
        setHorizontalAlignment(JLabel.CENTER);
        setVerticalAlignment(JLabel.CENTER);
    }
}

该计划是要能够选择多个JLabel,就像您在桌面上选择多个文件(蓝色选择框)并将其拖到JFrame中一样。如果我愿意,我仍然希望能够拖动一个

0 个答案:

没有答案