有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing使用鼠标移动JFrame

我正在玩Java swing图形等。我正在尝试设计一个注册/登录表单

enter image description here

我已经设置了未装饰(true),并创建了自己的顶部栏。拖动该条时,我希望整个帧移动。我还希望鼠标保持在框架上的相同位置

应用此代码后:

框架在移动。但鼠标移动到右上角,框定初始X和Y坐标

    JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);

    int mouseX = MouseInfo.getPointerInfo().getLocation().x; // Mouse X Coordinate
    int mouseY = MouseInfo.getPointerInfo().getLocation().y; // Mouse Y Coordinate
    int diffX = mouseX - frame.getX(); // The distance from JFrame X Coordinate to Mouse X Coordinate. 
    int diffY = mouseY - frame.getY(); // The distance from JFrame Y Coordinate to Mouse Y Coordinate.



    if (diffY < 45) { // Y Coordinate 0 - 45 is the height of the top part of the window. Where you can move the frame.
        frame.setLocation(mouseX, mouseY);
    }

enter image description here

据我所知,下面的代码应该使鼠标在拖动之前保持在它的位置

    JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);

    int mouseX = MouseInfo.getPointerInfo().getLocation().x; // Mouse X Coordinate
    int mouseY = MouseInfo.getPointerInfo().getLocation().y; // Mouse Y Coordinate
    int diffX = mouseX - frame.getX(); // The distance from JFrame X Coordinate to Mouse X Coordinate. 
    int diffY = mouseY - frame.getY(); // The distance from JFrame Y Coordinate to Mouse Y Coordinate.



    if (diffY > 0 && diffY < 45) { // Y Coordinate 0 - 45 is the height of the top part of the window. Where you can move the frame.
        int x = mouseX - diffX;
        int y = mouseY - diffY;

        frame.setLocation(x, y);
    }

但是。。什么也没发生。调用该函数,但帧不移动。我做错了什么?我似乎找不到问题

编辑* 我创建了ComponentMover类,并创建了如下实例:

    DragListener drag = new DragListener();
    JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);

    int mouseX = MouseInfo.getPointerInfo().getLocation().x; // Mouse X Coordinate
    int mouseY = MouseInfo.getPointerInfo().getLocation().y; // Mouse Y Coordinate
    int diffX = mouseX - frame.getX(); // The distance from JFrame X Coordinate to Mouse X Coordinate. 
    int diffY = mouseY - frame.getY(); // The distance from JFrame Y Coordinate to Mouse Y Coordinate.



    if (diffY > 0 && diffY < 45) { // Y Coordinate 0 - 45 is the height of the top part of the window. Where you can move the frame.
        frame.addMouseListener( drag );
        frame.addMouseMotionListener( drag );
    }

    ComponentMover cm = new ComponentMover(this, frame);

但问题依然存在

提前感谢您的帮助

编辑2* 这就是我开始使用ComponentMover的方法。虽然我不确定它能做我想让它做的事

这是代码

public class CompMoverTest extends JFrame{

    public CompMoverTest(){
        MainPanel mp = new MainPanel();

        setLayout(new FlowLayout());
        setUndecorated(true);
        add(mp);
        setSize(500,500);
        setVisible(true);
        setLocationRelativeTo(null);
    }


    public static void main(String[] args) {
        new CompMoverTest();
    }

}

class MainPanel extends JPanel{

    public MainPanel(){
        JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
        setPreferredSize(new Dimension(300,300));
        setBackground(Color.yellow);

        ComponentMover cm = new ComponentMover(frame, this);
    }
}

这是结果

enter image description here

这就是我想要的

enter image description here

我希望这足够清楚。我现在看到的是一个黄色面板,当我拖动它时它会移动。我知道这是ComponentMover类的函数

然而,我想要发生的是,当我拖动黄色面板时,整个帧在屏幕上移动,就像第二个gif一样。ComponentMover类是否可以实现这一点?我不能让它工作。其余的都好


共 (1) 个答案

  1. # 1 楼答案

    JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
    

    面板尚未添加到框架中,因此无法在构造函数中使用上述方法访问父框架

    如果确实想在自定义面板类中添加ComponentMover,则可以重写addNotify()方法:

    @Override
    public void addNotify()
    {
        super.addNotify();
    
        JFrame frame = (JFrame) SwingUtilities.windowForComponent(this);
        ComponentMover cm = new ComponentMover(frame, this);
    }