希望在java swing中完成组件移动后记录组件位置

希望在java swing中完成组件移动后记录组件位置,java,swing,Java,Swing,我想将目标位置显示为 组件移动到位置-Xloc:634.0,yloc:394.0 import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.JFrame; public class ComponentEventDemo extends JFrame { public ComponentEventDemo() { this.setDefaultCl

我想将目标位置显示为 组件移动到位置-Xloc:634.0,yloc:394.0

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

public class ComponentEventDemo extends JFrame {
  public ComponentEventDemo() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(400, 400);
    this.setVisible(true);
  }

  public static void main(String[] args) {
    final ComponentEventDemo component = new ComponentEventDemo();
    component.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentMoved(ComponentEvent e) {
        logLocation(component);
      }
    });

  }

  public static void logLocation(ComponentEventDemo component) {
    System.out.println("Component moved to location - Xloc:" + component.getBounds().getMinX() + ", yloc:"
        + component.getBounds().getMinY());
  }
}
在移动帧时,我希望在移动完成后获得帧位置

我正在为一个移动事件获取多个日志,如

样本输出

“组件已移动到位置-Xloc:0.0,yloc:0.0

组件移动到位置-Xloc:5.0,yloc:2.0

组件移动到位置-Xloc:15.0,yloc:11.0

组件移动到位置-Xloc:29.0,yloc:24.0

组件移动到位置-Xloc:50.0,yloc:41.0

组件移动到位置-Xloc:76.0,yloc:64.0

组件移动到位置-Xloc:113.0,yloc:95.0

组件移动到位置-Xloc:150.0,yloc:124.0

组件移动到位置-Xloc:191.0,yloc:154.0

组件移动到位置-Xloc:238.0,yloc:185.0

部件移动到位置-Xloc:288.0,yloc:214.0“

我正在查找鼠标释放后的最后一个日志。

谢谢你的建议。我对我的代码做了如下更改 `

` 它减少了输出中的日志数,但没有达到完整的目标。。 正在寻找其他选择。。谢谢你事先的帮助

我正在寻找鼠标释放后的最后一个日志

无法获取该信息,因为框架的拖动由操作系统控制,并且您无权访问在框架上生成的鼠标事件

您可以尝试使用摆动计时器。您计划定时器在特定时间段后启动,比如说500毫秒

在componentMoved事件中,启动/重新启动计时器。因此,如果用户持续拖动帧,计时器将不会启动。然后,如果用户在500毫秒内没有拖动帧,将生成计时器事件,您可以获得当前帧位置


因此,是的,使用此解决方案在了解帧位置时总会有一些延迟。

添加swing timer会减少打印的行数。谢谢,但是寻找其他替代方法
添加swing timer会减少打印的行数。
-为什么这很重要?如果我们知道实际需求,而不是您尝试的解决方案,我们可能能够帮助您提出替代方案。我们有一个swing应用程序,用户有时会抱怨该应用程序在屏幕上不可见。如果我们在更改应用程序位置时记录应用程序位置,那么它将帮助我们轻松解决类似问题。考虑添加我尝试的解决方案,但停止了,因为它仍在向日志中添加多行内容,这是不可接受的
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.Timer;

import javax.swing.JFrame;

public class ComponentEventDemo extends JFrame {
  Timer timer = new Timer(500, new MyTimer());
  static ComponentEventDemo component;

  public ComponentEventDemo() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(400, 400);
    this.setVisible(true);
  }

  public static void main(String[] args) {
    component = new ComponentEventDemo();
    component.addListener(component);
  }

  public void addListener(final ComponentEventDemo component) {

    component.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentMoved(ComponentEvent e) {
        timer.start();
      }
    });
  }

  class MyTimer implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      logLocation();
      timer.stop();
    }
  }

  public void logLocation() {
    System.out.println("Component moved to location - Xloc:" + component.getBounds().getMinX() + ", yloc:"
        + component.getBounds().getMinY());
  }
}