简体   繁体   English

为什么Swing不发布对原始X Server的关注

[英]Why Swing does not release focus on raw X Server

I noticed that swing application behaves differently. 我注意到swing应用程序的行为有所不同。 I use Centos 6.5 and I start only raw X server without window manager. 我使用Centos 6.5,并且仅启动了没有窗口管理器的原始X服务器。 In this scenerio, keyboard focus depends on pointer posiotion. 在此场景中,键盘焦点取决于指针位置。 Window has keyboard focus if pointer is on it. 如果指针位于窗口上,则窗口具有键盘焦点。 I tested it on xterm, gnomer-terminal, gedit, firefox. 我在xterm,gnomer-terminal,gedit,firefox上进行了测试。

I developed very simple swing application: 我开发了非常简单的秋千应用程序:

                JFrame jFrame = new JFrame("JFrame tittle");
                JPanel p = new JPanel();

                JTextField textField = new JTextField("111111111111111");

                p.add(textField);
                jFrame.setContentPane(p);
                jFrame.pack();
                jFrame.setVisible(true);

If I run this application then JTextField has focus and I can't change focus to another window. 如果运行此应用程序,则JTextField具有焦点,并且无法将焦点更改到另一个窗口。 When I move pointer on gnome-terminal JTextField has still keyboard focus and I can use keyboard only on it. 当我在gnome终端上移动指针时, JTextField仍然具有键盘焦点,并且只能在其上使用键盘。 I can open menu in gnome-terminal using pointer and menu has keyboard focus then (I can use arrow on menu) but when I close menu, focus returns to JTextField. 我可以使用指针在gnome终端中打开菜单,然后菜单具有键盘焦点(我可以在菜单上使用箭头),但是当我关闭菜单时,焦点返回到JTextField。

In short I can not use any another application. 简而言之,我不能使用任何其他应用程序。 Why? 为什么? What differs swing application from another? 秋千应用与其他应用有何不同? Why swing does not release focus when I move pointer on another window? 当我将指针移到另一个窗口时,为什么摆动不释放焦点? EDIT: I also clicked on another window of course. 编辑:我当然也点击了另一个窗口。

If I start window manager ( metacity ) then it works properly. 如果我启动窗口管理器( metacity ),那么它将正常工作。

=====EDIT==== Picture 1 shows two window: xterm and gnome-terminal. ===== EDIT ====图片1显示了两个窗口:xterm和gnome-terminal。 When I move cursor on gnome-terminal then I can use keyboard to write in gnome-terminal, when I move cursor on xterm then keyboard writes to xterm. 当我在gnome-terminal上移动光标时,我可以使用键盘在gnome-terminal中写入,当我在xterm上移动光标时,键盘将写入xterm。 图片1

When I start my java swing application it appears in left top corner. 当我启动Java swing应用程序时,它会显示在左上角。 I can only write into JTextField and cursor does not impact on keyboard focus.Following picture shows it: 我只能写入JTextField并且光标不会影响键盘焦点,下图显示了它:

在此处输入图片说明

I added printing of Look and Feel name and description to swing application so It is printed in console. 我在外观应用程序中添加了外观名称和描述的打印,以便在控制台中打印。 It is irrelevant. 这无关紧要。 在此处输入图片说明

=====EDIT===== =====编辑=====

I found following quote int this tutorial: 我在教程中找到以下引文:

Unlike citizens, the window manager has rights but not responsibilities. 与公民不同,窗口管理员拥有权利,但没有责任。 Programs must be prepared to cooperate with any type of window manager or with none at all […]. 程序必须准备好与任何类型的窗口管理器合作,或者根本不合作[…]。 — Xlib Programming Manual §1.2.3 — Xlib编程手册§1.2.3

So I think that Swing application should be prepared to work without WM. 因此,我认为Swing应用程序应该准备好在没有WM的情况下工作。

As i already wrote in the comments, it may be a problem when not using a WindowManager. 正如我已经在评论中所写的那样, 不使用 WindowManager时可能会出现问题。

see: 看到:

There is no foolproof way, across all platforms, to ensure that a window gains the focus. 在所有平台上,没有万无一失的方法来确保窗口获得焦点。 On some operating systems, such as Microsoft Windows, the front window usually becomes the focused window. 在某些操作系统(例如Microsoft Windows)上,前窗口通常成为焦点窗口。 In these cases, the Window.toFront method moves the window to the front, thereby giving it the focus. 在这些情况下,Window.toFront方法将窗口移到最前面,从而使其成为焦点。 However, on other operating systems, such as Solaris™ Operating System, the window manager may choose the focused window based on cursor position, and in these cases the behavior of the Window.toFront method is different. 但是,在其他操作系统(例如Solaris™操作系统)上, 窗口管理器可能会根据光标位置选择聚焦的窗口,在这些情况下,Window.toFront方法的行为是不同的。

from here: http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html 从这里开始: http : //docs.oracle.com/javase/tutorial/uiswing/misc/focus.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM