![](/img/trans.png)
[英]How to create a JDialog ModalityType.APPLICATION_MODAL in a background thread?
[英]Does ModalityType.APPLICATION_MODAL blocks the thread when we make dialog visible
我有以下代碼示例:
import java.awt.GridLayout;
import java.awt.Window;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyDialogParent extends JDialog {
MyDialogParent ()
{
super(null,ModalityType.APPLICATION_MODAL);
// super.setUndecorated(true);
super.setVisible(false);
// Disable Alt-F4 and any other close key sequences (MMSmk90468).
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
class MyDialogChild extends JDialog {
MyDialogChild (Window parent)
{
super(parent,ModalityType.MODELESS);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
}
}
public class MyDialogs {
public static void main(String args[])
{
MyDialogParent parent=new MyDialogParent();
parent.setSize(400, 400);
parent.setVisible(true);
System.out.println("1");
System.out.println("2");
System.out.println("3");
MyDialogChild child=new MyDialogChild(parent);
child.setLocationRelativeTo(parent);
child.setSize(300, 300);
child.setLayout(new GridLayout());
child.add(new JPanel());
child.setVisible(true);
}
}
現在執行掛起之后
parent.setVisible(true);
我知道ModalityType.APPLICATION_MODAL的概念在阻止某些頂級窗口的輸入方面。
我認為只有子框架不會收到任何輸入或焦點。 在代碼中,我有不同的看法。
我想知道它是如何工作的。 就像父級框架可見后線程停止執行一樣。 它會進入某種等待狀態嗎?
與線程相關的各種ModalityType的邏輯是什么?
不,不是。 Swing中的任何內容都不會阻塞任何線程-盡管所有處理,對布局和JComponent
結構的更改等都必須在Swing線程上進行。
通常,在該方面創建一個全新的窗口是安全的,但仍然值得在EDT上創建您的孩子(使用invokeLater
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.