【问题标题】:JDialog Freezes on Loss of Focus?JDialog 因失去焦点而冻结?
【发布时间】:2013-06-02 22:03:13
【问题描述】:

我正在开发一个程序,该程序旨在通过允许您编辑贴图中的单个纹理来促进纹理贴图的制作。我有几个地方使用 JDialog 来输入地图的瓦片大小、新地图的初始大小,以及一个简单的按钮,用户在完成编辑时按下该按钮使用外部程序(例如 photoshop 或paint)选择纹理。但是,只要这些 JDialog 之一启动,如果程序失去焦点,它就会完全没有响应。这是我在外部编辑选定纹理时弹出的 JDialog 代码:

JDialog editing = new JDialog(mainFrame, "Externally Editing");//mainFrame is the name of the JFrame containing everything.
JPanel pnl = new JPanel();
editing.setLayout(new BorderLayout());
pnl.setPreferredSize(new Dimension(150, 60));
editing.add(pnl);
editing.pack();
JButton button = new JButton("Done Editing");
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        editng = false;//Obviously, a boolean that I have outside of this method.
    }
});
pnl.add(button);
Thread.sleep(100);
editing.setVisible(true);
while(editng){System.out.print("");}//Doing nothing while the user is externally editing.. Unfortunately, if I don't have "System.out.print("");" it doesn't work.. Oh, Java..
new Thread(new Runnable(){public void run(){editing.dispose();}}).start();//Gotta dispose of it in a separate thread since AWT isn't Thread-safe... Ugh..

我会假设它在它为 JDialog 创建/拥有的 AWT 线程中冻结,而我的线程只是在等待按钮被按下。这不可能发生,因为 JDialog 被冻结了。

【问题讨论】:

  • 使您的对话模式化。您的应用程序将停止处理,直到对话框关闭。
  • 哦。谢谢!大声笑.. 把它作为一个答案,我会接受它。

标签: java swing user-interface jdialog


【解决方案1】:

使您的对话框模态化。您的应用程序将停止处理,直到对话框关闭

【讨论】:

    【解决方案2】:

    为什么是这个虚假的环形交叉路口?只需在侦听器中处理对话框即可。

    【讨论】:

    • 我试过了,相信我.. 当我这样做时,它也会冻结。我所看到的只是它被 dispose 方法捕获。这是我能找到的唯一可行的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多