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

被刻印的时光 ゝ 提交于 2019-12-13 17:27:36

问题


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):

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");


    }
}

回答1:


I have the same problem using IntelliJ and when I run from the command line, the only way that I found to show the app menu is creating an .app directory (http://www.eclipse.org/swt/macosx/) and executing it with double click or using the open command.




回答2:


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.



来源:https://stackoverflow.com/questions/19803332/menu-doesnt-show-up-with-swt-on-mac-os-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!