JFrame 未出现在鼠标单击位置(包括 SSCCE)

发布于 2024-09-07 08:46:37 字数 1837 浏览 2 评论 0原文

我试图让 JFrame 出现在 mousePressed Location 上,但我一直失败,而且很烦人:( 有什么想法不起作用吗?

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)
            {

                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);


    }
}

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)
            {

                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);


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2024-09-14 08:46:37

使用 SwingUtilities方法 convertPointToScreen()convertPointFromScreen() 用于转换 MouseEvent 坐标。

附录:或者,计算 getLocationOnScreen(),即“组件在屏幕坐标空间中的左上角”。

附录:要相对于原始鼠标单击定位新框架,请向父框架添加鼠标侦听器而不是按钮;使用坐标来定位新框架,如下所示。

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);
    }
}

之前,

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文