停止重新创建对话框 [英] Stop newly created dialog from taking focus

查看:182
本文介绍了停止重新创建对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建Java Swing对话框,但是我不想让对话框把焦点从当前的焦点上移开。因此,例如,如果您正在编辑word文档,而另一个应用程序创建了一个对话框,则应该看到该对话框,但word文档应该保持焦点,以便您可以继续编辑。



我在Mac OSX和Ubuntu 12.04上测试了以下代码。在Mac上,代码会创建不需要像我想要的焦点的对话框。在Ubuntu上,对话框占据了焦点,我不能在创建时输入。



我真的希望这不是特定于操作系统的。有什么办法可以实现我在所有平台上(或者至少在Ubuntu上)所做的事情?

  import javax。摇摆。*; 
import java.awt.Color;
import java.util.Random;

public class Focus {
private static Random r = new Random();

public static void main(String [] args){
while(true){
try {
Thread.sleep(500);
createObject(r.nextInt(1000),r.nextInt(600));
} catch(InterruptedException e){
e.printStackTrace();



$ b public static void createObject(int x,int y){
JDialog testDialog = new JDialog();
testDialog.setSize(50,50);
testDialog.setLocation(x,y);
testDialog.setAlwaysOnTop(true);
testDialog.setUndecorated(true);
testDialog.setFocusable(false);
testDialog.setEnabled(false);
testDialog.setVisible(true);



$ div $解析方案

也许:

  testDialog.setFocusableWindowState(false); 


I'm trying to have Java Swing dialog be created but I don't want to have the dialog to take focus away from whatever is currently focused. So for example, if you're editing a word doc and a different application creates a dialog, you should see the dialog but the word doc should remain in focus so that you can keep editing.

I've tested the following code on Mac OSX and on Ubuntu 12.04. On Mac, the code creates dialogs that do not take the focus like I want. On Ubuntu, the dialogs take the focus, and I can't type while they are being created.

I really hope this isn't operating system specific. Is there any way to achieve what I'm trying to do on all platforms (or at least Ubuntu)?

import javax.swing.*;
import java.awt.Color;
import java.util.Random;

public class Focus {
    private static Random r = new Random();

    public static void main(String[] args) {
        while(true) {   
            try {
                Thread.sleep(500);
                createObject(r.nextInt(1000), r.nextInt(600));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void createObject(int x, int y) {
        JDialog testDialog = new JDialog();
        testDialog.setSize(50, 50);
        testDialog.setLocation(x, y);
        testDialog.setAlwaysOnTop(true);
        testDialog.setUndecorated(true);
        testDialog.setFocusable(false);
        testDialog.setEnabled(false);
        testDialog.setVisible(true);
    }
}

解决方案

Maybe:

testDialog.setFocusableWindowState(false);

这篇关于停止重新创建对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆