【发布时间】:2019-08-08 05:50:40
【问题描述】:
我正在玩 Java swing 图形等。我正在尝试设计一个注册/登录表单。
我有 setUndecorated(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);
}
据我所知,下面的这段代码应该使鼠标在拖动之前保持在其位置。
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);
}
}
这就是结果
这就是我想要的
我希望这已经足够清楚了。我现在拥有的是一个黄色面板,当我拖动它时它会移动。我知道这是 ComponentMover 类的功能。
但是,我想要发生的是,当我拖动黄色面板时,整个框架会像第二个 gif 一样在屏幕上移动。这对 ComponentMover 类是否可行?我不能让它工作。其余的都很好。
【问题讨论】:
-
您可以查看Moving Windows 的课程,该课程将为您完成此任务。您可以使用花哨的类,或者修改基本的拖动代码。添加简单的检查以查看鼠标是否在帧的前 45 个像素中。
-
尝试实现该类。但是同样的问题仍然存在,当我拖动它时没有任何反应。
-
Tried implementing that Class.- 什么课? ComponentMover 类完全符合您的要求。 DragListener 需要自定义,我没有看到您的 minimal reproducible example 展示了您所做的事情。 -
编辑了我最初的帖子我是如何添加课程的。
-
ComponentMover 需要访问 JFrame,如链接中所示。我猜不出您试图访问构造函数中的框架,这将不起作用,因为面板尚未添加到框架中。这就是为什么我们需要为每个问题提供minimal reproducible example,以便我们可以准确地看到您在做什么。请参阅答案以获取替代方案。
标签: java swing graphics jframe mouseevent