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

时间:2010-06-27 12:35:04

标签: java swing location jframe

我正在尝试让一个JFrame出现在mousePressed 位置上,但我一直都失败了,这让人讨厌:(任何想法什么都行不通?

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


    }
}

1 个答案:

答案 0 :(得分:2)

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