2011-09-05 52 views
5

什麼代碼將有助於作出JDialog不可移動?我已經看了兩個選項:爪哇 - 的JDialog不可移動

  1. setUndecorated(true);這工作,但刪除所有的禮品。
  2. addComponentListener並重寫componentMoved()方法,這會導致JDialog隨後調用induceEpilepticSeizure()在移動。

任何想法?

+2

你能解釋一下你爲什麼要這樣做嗎?它可能會提供一種可行的方法。 –

回答

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); 
} 
+1

我不使用setUndecorated(true)的原因是我想保留默認邊框。窗口是否是無邊界的?哥們,謝啦。 –