Java - JDialog不可移动

far*_*ich 5 java swing jdialog

什么代码将有助于使一个JDialog不可移动的?我看了两个选项:

  1. setUndecorated(true); 哪个有效,但删除了所有的装饰.
  2. addComponentListener并覆盖该componentMoved()方法,这会导致JDialog随后调用induceEpilepticSeizure() 移动.

有任何想法吗?

Ste*_*eod 5

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

因此,如果你想要原生装饰并且你想要它不可移动而没有使用组件监听器的可怕闪烁,我想你不能.

您可以手动创建一个边框,看起来像默认边框...这是一个如何做的例子,虽然我有意让边框看起来像你整天看到的最丑陋的东西.您需要找到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);
}
Run Code Online (Sandbox Code Playgroud)