简体   繁体   中英

Menu doesn't show up with SWT on Mac OS X

For some reason, the menu doesn't show up at all when I run my simple SWT application on OS X. This problem doesn't happen with other SWT applications that I've written, and I'm not sure what I'm doing differently. When I run my program, the menu I see at the top of the screen belongs to Eclipse, which happens to be the IDE I'm using. Also, while the Eclipse menu is visible, it is not clickable or responsive in any way.

Here's what the menu looks like now (to avoid confusion as to what I meant):

Eclipse 的菜单

Here's the relevant code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class AnalyzerApp {

    protected Shell shell;

    Display display;

    boolean thisIsAMac = SWT.getPlatform().equals("cocoa");

    public static void main(String[] args) {
        try {
            AnalyzerApp window = new AnalyzerApp();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void open() {
        Display.setAppName("Analyzer");
        display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }


    protected void createContents() {
        shell = new Shell();
        shell.setSize(832, 526);
        shell.setText("Analyzer");
        shell.setLayout(new FormLayout());

        // Menu

        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        if(thisIsAMac) menu = display.getMenuBar();
            // ^ tried to fix with this, didn't change anything

        MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
        mntmFile.setText("File");

        Menu fileMenu = new Menu(mntmFile);
        mntmFile.setMenu(fileMenu);

        MenuItem mntmNew = new MenuItem(fileMenu, SWT.NONE);
        mntmNew.setText("New");

        MenuItem mntmOpen = new MenuItem(fileMenu, SWT.NONE);
        mntmOpen.setText("Open");

        MenuItem mntmSave = new MenuItem(fileMenu, SWT.NONE);
        mntmSave.setText("Save");

        MenuItem mntmSaveAs = new MenuItem(fileMenu, SWT.NONE);
        mntmSaveAs.setText("Save As");

        new MenuItem(fileMenu, SWT.SEPARATOR);

        MenuItem mntmImport = new MenuItem(fileMenu, SWT.NONE);
        mntmImport.setText("Import");


    }
}

I have exactly the same problem on Mavericks with Eclipse 4.4 but I realized that I can see the menu bar when I but the swt application in the background and bring it to the front again. Still, seems to be some kind of bug.

我在使用 IntelliJ 时遇到了同样的问题,当我从命令行运行时,我发现显示应用程序菜单的唯一方法是创建一个 .app 目录( http://www.eclipse.org/swt/macosx/ )和双击或使用 open 命令执行它。

Still a problem. But works if you open a window of any other application after starting your SWT app, then switch back. A hack for doing this programmatically from your SWT app is to open a Finder window after getting your Display instance:

Display display = Display.getDefault();
try { 
     Runtime.getRuntime().exec("open .");
} catch (Exception e) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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