开发者

JFrame not appearing on mouse click Location (SSCCE included)

开发者 https://www.devze.com 2023-01-04 17:39 出处:网络
I am trying to make a JFrame appear on mousePressed Location but I keep failing and it get\'s annoying :( Any ideas what isn\'t working?

I am trying to make a JFrame appear on mousePressed Location but I keep failing and it get's annoying :( Any ideas what isn't working?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SSCCE
{

    @SuppressWarnings("static-access")
    public static void getInputData()
    {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
        JLabel emptyLabel = new JLabel("Test");
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
        frame.setSize(new Dimension(375, 100));

        MouseAdapter ml = new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent me)
            {

          开发者_JAVA百科      frame.setLocation(me.getX(), me.getY());
            }

            @Override
            public void mouseDragged(MouseEvent me)
            {
                frame.setLocation(me.getX(), me.getY());
            }
        };
        frame.getContentPane().addMouseListener(ml);
        frame.getContentPane().addMouseMotionListener(ml);

        frame.setVisible(true);

    }

    public static void main(String args[])
    {
        JFrame test = new JFrame();
        JButton but = new JButton("Click me");
        but.addActionListener(
                new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {
                        getInputData();
                    }
                });
        test.getContentPane().add(but, BorderLayout.CENTER);
        test.setSize(500, 500);
        test.setVisible(true);


    }
}


Use the SwingUtilities methods convertPointToScreen() and convertPointFromScreen() to transform the MouseEvent coordinates.

Addendum: Alternatively, calculate the offset from getLocationOnScreen(), which is "the component's top-left corner in the screen's coordinate space."

Addendum: To position the new frame relative to the original mouse click, add a mouse listener to the parent frame instead of a button; use the coordinates to position the new frame, as shown below.

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

public class SSCCE {

    public static void getInputData(MouseEvent e) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel emptyLabel = new JLabel("Test", JLabel.CENTER);
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(375, 100));

        MouseAdapter ma = new MouseAdapter() {

            Point local, global;
            Point delta = new Point();

            @Override
            public void mousePressed(MouseEvent me) {
                local = me.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                delta.setLocation(
                    me.getX() - local.x, me.getY() - local.y);
                global = frame.getLocationOnScreen();
                global.setLocation(
                    global.x + delta.x, global.y + delta.y);
                frame.setLocation(global.x, global.y);
            }
        };
        frame.getContentPane().addMouseListener(ma);
        frame.getContentPane().addMouseMotionListener(ma);
        frame.pack();
        frame.setLocation(e.getLocationOnScreen());
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(640, 480));
        frame.add(new JLabel("Click me", JLabel.CENTER));
        frame.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                getInputData(e);
            }
        });
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Previously,

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

public class SSCCE {

    public static void getInputData() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel emptyLabel = new JLabel("Test", JLabel.CENTER);
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(375, 100));

        MouseAdapter ma = new MouseAdapter() {

            Point local = new Point();
            Point delta = new Point();
            Point global = new Point();

            @Override
            public void mousePressed(MouseEvent me) {
                local = me.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                delta.setLocation(
                    me.getX() - local.x,
                    me.getY() - local.y);
                global = frame.getLocationOnScreen();
                global.setLocation(global.x + delta.x, global.y + delta.y);
                frame.setLocation(global.x, global.y);
            }
        };
        frame.getContentPane().addMouseListener(ma);
        frame.getContentPane().addMouseMotionListener(ma);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton but = new JButton("Click me");
        but.addActionListener(
            new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    getInputData();
                }
            });
        frame.getContentPane().add(but, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消