我想使用Java应用程序以本机方式在OS X上的系统托盘/任务栏中显示文本。我想通过在setTitle
上调用NSStatusItem
(例如在this example中)来执行此操作。我从未在SWT中访问过底层本机库,而且我遇到了麻烦。
this thread上的某人演示了如何调用Objective-C方法来更改SWT中窗口的属性(全屏按钮)。这是他/她的代码:
Field field = Control.class.getDeclaredField("view");
Object /*NSView*/ view = field.get(rShell);
if (view != null)
{
Class<?> c = Class.forName("org.eclipse.swt.internal.cocoa.NSView");
Object /*NSWindow*/ window = c.getDeclaredMethod("window").invoke(view);
c = Class.forName("org.eclipse.swt.internal.cocoa.NSWindow");
Method setCollectionBehavior = c.getDeclaredMethod(
"setCollectionBehavior", JVM.is64bit() ? long.class : int.class);
setCollectionBehavior.invoke(window, getFullScreenMask());
}
所以我想这会产生这个Objective-C代码:
[window setCollectionBehaviour:1<<7];
现在我想对SWT TrayItem
做同样的事情。目标是获得这个Objective-C代码的等价物:
[statusItem setTitle:@"Status"];
但我得到以下例外:
Exception in thread "main" java.lang.IllegalArgumentException: Can not set org.eclipse.swt.internal.cocoa.NSView field org.eclipse.swt.widgets.Control.view to org.eclipse.swt.widgets.TrayItem
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
at java.lang.reflect.Field.get(Field.java:358)
at com.teez.status.MainStatus.main(MainStatus.java:35)
我不确定这意味着什么。我标记了引发异常的行。尝试这个时我还会遇到哪些其他问题?这是我的代码:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
public class MainStatus {
public static void main(String[] args) throws Exception {
Display display = new Display();
Shell shell = new Shell(display);
Tray tray = display.getSystemTray();
if (tray != null) {
TrayItem item = new TrayItem(tray, SWT.NONE);
Field field = Control.class.getDeclaredField("view");
Object /*NSView*/ view = field.get(item); //Exception thrown here
if (view != null)
{
Class<?> c = Class.forName("org.eclipse.swt.internal.cocoa.NSView");
Object /*NSWindow*/ window = c.getDeclaredMethod("window").invoke(view);
c = Class.forName("org.eclipse.swt.internal.cocoa.NSStatusItem");
Method setCollectionBehavior = c.getDeclaredMethod("setTitle", long.class);
setCollectionBehavior.invoke(window, "Desired title");
}
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
}
编辑:感谢Eugene解决此问题,但文本仍然没有显示在状态栏上,所以我问了另一个问题here。
答案 0 :(得分:1)