Java——JDialog不可移动

发布于 2024-12-03 10:04:29 字数 296 浏览 1 评论 0原文

什么代码有助于使 JDialog 不可移动?我研究了两个选项:

  1. setUndecorated(true);,它可以工作,但会删除所有装饰。
  2. addComponentListener 并重写 componentMoved() 方法,这会导致 JDialog 在移动时随后调用 induceEpilepticSeizure()

有什么想法吗?

What code will facilitate making a JDialog unmovable? I've looked at two options:

  1. setUndecorated(true); which works but removes all the trimmings.
  2. addComponentListener and overriding the componentMoved() method, which causes the JDialog to subsequently call induceEpilepticSeizure() upon moving.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-12-10 10:04:29

我的第一直觉是 - 你不能,除非你确实使用 setUndecorated(true)...你可以手动在那里放置一些装饰,但是,嗯,呃!

因此,如果您想要原生装饰,并且希望它不可移动,而又不会因使用组件侦听器而产生可怕的闪烁,我认为您不能。

您可以手动创建一个看起来像默认边框的边框...这是一个如何执行此操作的示例,尽管我故意使边框看起来像您整天看到的最丑陋的东西。您需要找到正确的 BorderFactory 调用组合来实现您想要做的事情。

public static void main(String[] args) throws InterruptedException {
    JDialog frame = new JDialog((Frame) null, "MC Immovable");
    frame.setUndecorated(true);
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.RED));
    panel.add(new JLabel("You can't move this"));

    frame.setContentPane(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

My first instinct is - you can't unless you DO use setUndecorated(true)... You could manually put some trimmings there, but, well, UGH!

So if you want the native trimmings AND you want it immovable without the horrible flickering from using a component listener, I think you can't.

You could create a border manually that LOOKS like the default border...here's an example of how to do it, although I've intentionally made the border look like the ugliest thing you've seen all day. You'll need to find the right combination of BorderFactory calls to achieve what you want to do.

public static void main(String[] args) throws InterruptedException {
    JDialog frame = new JDialog((Frame) null, "MC Immovable");
    frame.setUndecorated(true);
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.RED));
    panel.add(new JLabel("You can't move this"));

    frame.setContentPane(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文