gpt4 book ai didi

Java Swing 用鼠标移动 JFrame

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:42 26 4
gpt4 key购买 nike

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

enter image description here

我已经设置了Undecorated(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 类是否可行?我无法让它发挥作用。其余的都还好。

最佳答案

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

关于Java Swing 用鼠标移动 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com