2

クラス javax.swing.JLabel を拡張するクラス JLabelExtended があります。マウスを使ってドラッグするプロパティを追加したいので、拡張します。これが私のコードです:

public class JLabelExtended extends JLabel {
private MouseMotionAdapter mouseMotionAdapter;

private JLabelExtended jLabelExtended;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println(e.getX() + "  :   " + e.getY());
            jLabelExtended.setLocation(e.getX(), e.getY()
            );
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
}

}

これは、ラベルをドラッグした後のコンソール部分です:

163  :   163
144  :   -87
163  :   162
144  :   -88
163  :   161
144  :   -89

いくつか質問があります:

  1. なぜ e.getY() は否定的な結果になるのですか?

  2. ラベルをドラッグすると、ラベルの近くにドラッグするラベルのコピーが表示されます。どうすれば修正できますか?

  3. ラベルをドラッグすると、ドラッグが非常に遅くなります。たとえば、カーソルを 10 ポイント上に移動すると、ラベルは 5 ポイント上でしか移動しません。どうすれば修正できますか?

前もって感謝します

JLabel を拡張する別の方法を次に示します。

public class LabelEasy extends JLabel { private MouseAdapter moveMouseAdapter; プライベート MouseMotionAdapter mouseMotionAdapter;

private LabelEasy jLabelExtended;

private int xAdjustment, yAdjustment;
Boolean count = false;

public LabelEasy(String text) {
    super(text);
    jLabelExtended = this;

    moveMouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == 1) {
                xAdjustment = e.getX();
                yAdjustment = e.getY();
            }
        }
    };

    mouseMotionAdapter = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (count) {
                System.out.println(e.getX() + "  :   " + e.getY());
                jLabelExtended.setLocation(xAdjustment + e.getX(), yAdjustment + e.getY());
                count = false;
            } else {
                count = true;
            }
            ;
        }
    };

    jLabelExtended.addMouseMotionListener(mouseMotionAdapter);
    jLabelExtended.addMouseListener(moveMouseAdapter);
}

}

ただし、以前のバリアントと同様に機能します。

4

1 に答える 1

2

私はあなたが間違っていると思います。MouseMotionListener は JLabel に追加され、その位置はJLabel を保持する Container ではなく、 JLabel に対して相対的であるため、ドラッグするのに役立つ情報は役に立ちません。MouseAdapter を使用して、MouseListener と MouseMotionListener の両方として追加することもできます。mousePressed で、画面に対する JLabel とマウスの位置を取得し、それを mouseDragged でのドラッグに使用します。私自身、これを行うために JLabel を拡張するのではなく、通常の JLabel を使用するだけですが、それが私の好みです。

編集:画面に対するマウスの位置(getLocationOnScreenを呼び出すことによって)と、コンテナに対するJLabelの位置(getLocationを呼び出すことによって)を処理すると、うまくいきました。たとえば、

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

public class DragLabelEg {
    private static final String[] LABEL_STRINGS = { "Do", "Re", "Me", "Fa",
            "So", "La", "Ti" };
    private static final int HEIGHT = 400;
    private static final int WIDTH = 600;
    private static final Dimension MAIN_PANEL_SIZE = new Dimension(WIDTH,
            HEIGHT);
    private static final int LBL_WIDTH = 60;
    private static final int LBL_HEIGHT = 40;
    private static final Dimension LABEL_SIZE = new Dimension(LBL_WIDTH,
            LBL_HEIGHT);
    private JPanel mainPanel = new JPanel();
    private Random random = new Random();

    public DragLabelEg() {
        mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
        mainPanel.setLayout(null);

        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        for (int i = 0; i < LABEL_STRINGS.length; i++) {
            JLabel label = new JLabel(LABEL_STRINGS[i], SwingConstants.CENTER);
            label.setSize(LABEL_SIZE);
            label.setOpaque(true);
            label.setLocation(random.nextInt(WIDTH - LBL_WIDTH),
                    random.nextInt(HEIGHT - LBL_HEIGHT));
            label.setBackground(new Color(150 + random.nextInt(105),
                    150 + random.nextInt(105), 150 + random.nextInt(105)));
            label.addMouseListener(myMouseAdapter);
            label.addMouseMotionListener(myMouseAdapter);

            mainPanel.add(label);
        }
    }

    public JComponent getMainPanel() {
        return mainPanel;
    }

    private class MyMouseAdapter extends MouseAdapter {
        private Point initLabelLocation = null;
        private Point initMouseLocationOnScreen = null;

        @Override
        public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            // get label's initial location relative to its container
            initLabelLocation = label.getLocation();

            // get Mouse's initial location relative to the screen 
            initMouseLocationOnScreen = e.getLocationOnScreen();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            initLabelLocation = null;
            initMouseLocationOnScreen = null;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            // if not dragging a JLabel
            if (initLabelLocation == null || initMouseLocationOnScreen == null) {
                return;
            }
            JLabel label = (JLabel)e.getSource();

            // get mouse's new location relative to the screen
            Point mouseLocation = e.getLocationOnScreen();

            // and see how this differs from the initial location.
            int deltaX = mouseLocation.x - initMouseLocationOnScreen.x;
            int deltaY = mouseLocation.y - initMouseLocationOnScreen.y;

            // change label's position by the same difference, the "delta" vector
            int labelX = initLabelLocation.x + deltaX;
            int labelY = initLabelLocation.y + deltaY;

            label.setLocation(labelX, labelY);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGui();
            }
        });
    }

    private static void createGui() {
        JFrame frame = new JFrame("App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DragLabelEg().getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
于 2011-02-21T22:20:39.657 に答える