简体   繁体   English

在Java swing中创建X window(X11)并获取其ID

[英]to create X window(X11) in java swing and to get its id

有人可以帮助我使用Eclipse在Java swing中创建X11窗口吗?还有获取X11 ID的函数。在Java中创建X11窗口的基本要求是什么。

Tom answered the first part of your question. 汤姆回答了您问题的第一部分。 The second part of the answer is: to get the id of an X11 window you are going to have to use native code (code written in C or C++) and access it through the JNI interface. 答案的第二部分是:要获取X11窗口的ID,您将必须使用本机代码(用C或C ++编写的代码)并通过JNI接口访问它。

You may have to run a search by title through all existing windows to get the one you desire. 您可能必须通过标题在所有现有窗口中进行搜索才能获得所需的窗口。

Here is a recursive function that will search (starting from the root window) for a window with the desired name 这是一个递归函数,它将搜索(从根窗口开始)具有所需名称的窗口

Window windowWithName(Display *dpy, Window top, char *name)
{
    Window *children, dummy;
    unsigned int nchildren;
    unsigned int i;
    Window w = 0;
    char *window_name;

    if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
        return (top);

    if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
        return (0);

    for (i = 0; i < nchildren; i++)
    {
        w = windowWithName(dpy, children[i], name);
        if (w)
            break;
    }
    if (children)
        XFree((char *) children);
    return (w);
}

Note: **unfortunately there is a well documented memory leak in the XFetchName function implemented in X11 that was never fixed. 注意:**不幸的是,在X11中实现的XFetchName函数中有记录良好的内存泄漏,但从未修复过。 If you run valgrind and have minor memory leak issues this is whats causing them. 如果您运行valgrind并有较小的内存泄漏问题,这是导致它们的原因。

Just to expand the answers @Zubzub and @ArtemGr gave, the following minimalistic AWT example works for me, at least in Sun 's Java 1.8: 为了扩展@Zubzub@ArtemGr给出的答案,以下简约的AWT示例至少在Sun的Java 1.8中对我有用

import java.awt.Dimension;

import javax.swing.JFrame;

import sun.awt.X11.XWindow;

class C {
    public static void main(final String args[]) {
        final JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(200, 200));
        frame.pack();
        frame.setVisible(true);
        final XWindow xWindow = (XWindow) frame.getPeer();
        frame.setTitle("Window id: 0x" + Long.toHexString(xWindow.getWindow()));
    }
}

Once the window is visible, you can verify its id with xwininfo utility. 窗口可见后,您可以使用xwininfo实用程序验证其ID。

Please note, however, that even provided your AWT application will only run under X11 , the above solution is not portable: 但是请注意,即使您的AWT应用程序只能在X11下运行,上述解决方案也不是可移植的:

  • GCJ will use gnu.java.awt.peer.gtk.GtkFramePeer , and GCJ将使用gnu.java.awt.peer.gtk.GtkFramePeer
  • older Sun JDK versions (1.4 and below) will use sun.awt.motif.MFramePeer . Sun JDK的早期版本(1.4及更低版本)将使用sun.awt.motif.MFramePeer Despite XToolkit is the default on Sun JDK starting with 1.5, old MToolkit can still be re-enabled on 1.5 and 1.6, and 尽管XToolkit从1.5开始是Sun JDK的默认设置, MToolkit仍可以在1.5和1.6上重新启用旧的MToolkit ,并且
  • I'm not sure about IBM J9 , BEA JRockit , Oracle lwAWT and Apache Harmony . 我不确定IBM J9BEA JRockitOracle lwAWTApache Harmony

Creating an X11 window in Swing is as easy as new Frame() followed by setVisible(true) . 在Swing中创建X11窗口就像new Frame()setVisible(true)一样容易。 Getting any of the unabstracted details will be harder. 获得任何基本信息将更加困难。 Of course, you can always open a java.net.Socket to port 6000 or so and speak X11 yourself. 当然,您总是可以打开一个java.net.Socket到端口6000左右,并自己说X11。

If you're using a Sun JVM and don't mind overriding package access using reflection and "setAccessible(true)", then you can create an X11 window using the Sun's toolkit. 如果您使用的是Sun JVM,并且不介意使用反射和“ setAccessible(true)”覆盖包访问,则可以使用Sun的工具包来创建X11窗口。 https://www.docjar.com/docs/api/sun/awt/X11/XWindow.html https://www.docjar.com/docs/api/sun/awt/X11/XWindow.html

Not all methods of the toolkit might be present, though: I suspect methods not used in the toolkit itself were removed. 但是,并不是所有的工具箱方法都存在:我怀疑工具箱本身未使用的方法已删除。 For example, most of the XlibUtil aren't there. 例如,大多数XlibUtil都不存在。 Here's an example how to use reflection to access the toolkit methods, in Scala: http://gist.github.com/567076 这是在Scala中如何使用反射来访问工具包方法的示例: http : //gist.github.com/567076

You can also use JNA to code xlib parts in Java; 您也可以使用JNA在Java中对xlib部件进行编码。 see question: Using Xlib via JNA to move a window 看到问题: 通过JNA使用Xlib移动窗口

For those out there still looking for this: 对于那些仍然在寻找这个的人:

You need to find the most toplevel component of your program and get the component's 'componentpeer'. 您需要找到程序中最顶层的组件,并获取该组件的“ componentpeer”。 Under linux this will be of type XComponent peer which extends XWindow which extends XBaseWindow. 在linux下,它的类型为XComponent peer,它扩展了XWindow,后者又扩展了XBaseWindow。 XBaseWindow has an attribute window of type long. XBaseWindow具有类型为long的属性窗口。 That's what you're looking for. 这就是您要寻找的。 You probably need to use reflection to get to it. 您可能需要使用反射来达到目的。

hf 高频

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

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