swing中模态对话框(setModal(true))和显示对话框(setVisible(true))的编写顺序
今天給大家分享一個(gè)鄙人在編程中總結(jié)出的一個(gè)易錯(cuò)點(diǎn)和最容易讓人感到困惑的一個(gè)知識(shí)點(diǎn):
當(dāng)你要從一個(gè)窗體跳轉(zhuǎn)到另一個(gè)窗體,你把跳轉(zhuǎn)目標(biāo)的窗體設(shè)成模態(tài)對(duì)話框,設(shè)計(jì)成模態(tài)對(duì)話框就是禁止父窗體與子窗體之間操作,簡(jiǎn)單說(shuō)就是當(dāng)調(diào)用子窗體的時(shí)候,父窗體不能使用,必須等子窗體銷(xiāo)毀才能使用,但是在這里會(huì)有個(gè)容易出錯(cuò)的地方就是子窗體不能正常現(xiàn)實(shí)出來(lái),而是顯示一個(gè)圓點(diǎn),也就是下圖這種格式
為什么會(huì)出現(xiàn)這種情況呢?剛開(kāi)始我也有些疑惑,后來(lái)靈機(jī)一動(dòng),把setModal(true)放在setVisible(true)的后面,竟然發(fā)現(xiàn)解決了這個(gè)問(wèn)題。
還有就是 javasetModal(true) 要在dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);的后面,否則將無(wú)法關(guān)閉子對(duì)話框
正確寫(xiě)法
public class EmployeeRegisterFrame extends JDialog {private JTextField accountField;private JTextField phoneField;private JPasswordField passwordField;public EmployeeRegisterFrame() {try {setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);**setVisible(true);**setResizable(false);**setModal(true);**} catch (Exception e) {e.printStackTrace();}setSize( 581, 420);setLocationRelativeTo(null);getContentPane().setLayout(null);錯(cuò)誤寫(xiě)法
public class EmployeeRegisterFrame extends JDialog {private JTextField accountField;private JTextField phoneField;private JPasswordField passwordField;public EmployeeRegisterFrame() {try {**setModal(true);**setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);**setVisible(true)**;setResizable(false);} catch (Exception e) {e.printStackTrace();}setSize( 581, 420);setLocationRelativeTo(null);getContentPane().setLayout(null); 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的swing中模态对话框(setModal(true))和显示对话框(setVisible(true))的编写顺序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ant-design-vue 环境搭建及
- 下一篇: html-图像标签