gpt4 book ai didi

java - 如何在JFC中模拟JDialog运动

转载 作者:行者123 更新时间:2023-12-02 00:14:23 26 4
gpt4 key购买 nike

我有一个 JDialog 显示在屏幕上,我想根据条件模拟它的移动(从一个位置拖动到另一个位置)。有什么办法可以做到这一点吗?

最佳答案

请参阅下面的这段代码。我刚刚测试过它并且工作正常。这只是一个概念证明。

private void startDialog() {
final JDialog d = new JDialog(this, "Test", true);
d.getContentPane().add(new JLabel("Something"));
d.setBounds(100, 100, 400, 300);
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 50; i++) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Point p = d.getLocation();
d.setLocation(p.x + 10, p.y + 10);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
}
}
});
t.start();
d.setVisible(true);
}

您可以自己改进代码:

  • 使用计时器而不是常规线程
  • 调整 sleep 时间和位置跳跃等

只需从任何 Swing 应用程序调用此方法即可工作。

关于java - 如何在JFC中模拟JDialog运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116091/

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