如何使用jface/swt操作?

时间:2022-03-25 00:45:36

I'm working on an application using jface/swt and would like to use org.eclipse.jface.action.Action to implement menuItems, buttons etc. I've search high and low for some documentation or tutorial explaining how to use actions, but have been unable to find any. Someone care to point me to tutorials, or enlighten me themselves?

我正在使用jface/swt进行应用程序,并希望使用org.eclipse.jface.action。执行菜单项、按钮等的操作。我搜索了高和低的一些文档或教程,说明如何使用操作,但一直找不到。有人愿意指教我,或者让我自己开导?

Thanks in advance!

提前谢谢!

Note: This is a java application, not an eclipse plugin.

注意:这是一个java应用程序,而不是一个eclipse插件。

2 个解决方案

#1


2  

Use IContributionManager.

使用IContributionManager。

#2


1  

I am also having some problem understanding how the menu bar, events, and actions in JFace work and there is very little useful posts online from what I can see. I sort of know how to use MenuManager. My question is whether I need to create 10 different classes for 10 menu items, if the actions are different.

我也有一些问题,了解如何在JFace工作中的菜单栏、事件和动作,以及在网上几乎没有什么有用的帖子。我知道如何使用MenuManager。我的问题是,如果操作不同,我是否需要为10个菜单项创建10个不同的类。

You can study the source code here. Check chapter 4 about Action and IContributionManager. Also see chapter 9.

你可以在这里学习源代码。检查第4章关于行动和IContributionManager。也看到第9章。

Here is a sample JFace menu program that works.

下面是一个示例JFace菜单程序。

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class TestApp extends ApplicationWindow {

    public TestApp() {
        super(null);
        addMenuBar();
    }

    public Control createContents(Composite parent) {
        getShell().setText("JFace menu demo");
        getShell().setSize(800, 600);
        return parent;
    }

    protected MenuManager createMenuManager() {
        MenuManager mainMenu = new MenuManager();
        MenuManager fileMenu = new MenuManager("File");
        MenuManager helpMenu = new MenuManager("Help");

        // File popup menu
        fileMenu.add(new OpenFile());
        fileMenu.add(new Exit(this));

        // Help popup menu
        helpMenu.add(new About());

        mainMenu.add(fileMenu);
        mainMenu.add(helpMenu);

        return mainMenu;
    }

    public static void main(String[] args) {
        TestApp win = new TestApp();
        win.setBlockOnOpen(true);
        win.open();             
        Display.getCurrent().dispose();
    }

    class OpenFile extends Action {
        public OpenFile() {
            super("&Open Filer@Ctrl+O", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }

    class Exit extends Action {
        ApplicationWindow win;
        public Exit(ApplicationWindow aWin) {
            super("E&xit@Alt+X", AS_PUSH_BUTTON);
            this.win = aWin;
        }

        public void run() {
            this.win.close();
        }
    }
    class About extends Action {

        public About() {
            super("About", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }
}

#1


2  

Use IContributionManager.

使用IContributionManager。

#2


1  

I am also having some problem understanding how the menu bar, events, and actions in JFace work and there is very little useful posts online from what I can see. I sort of know how to use MenuManager. My question is whether I need to create 10 different classes for 10 menu items, if the actions are different.

我也有一些问题,了解如何在JFace工作中的菜单栏、事件和动作,以及在网上几乎没有什么有用的帖子。我知道如何使用MenuManager。我的问题是,如果操作不同,我是否需要为10个菜单项创建10个不同的类。

You can study the source code here. Check chapter 4 about Action and IContributionManager. Also see chapter 9.

你可以在这里学习源代码。检查第4章关于行动和IContributionManager。也看到第9章。

Here is a sample JFace menu program that works.

下面是一个示例JFace菜单程序。

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class TestApp extends ApplicationWindow {

    public TestApp() {
        super(null);
        addMenuBar();
    }

    public Control createContents(Composite parent) {
        getShell().setText("JFace menu demo");
        getShell().setSize(800, 600);
        return parent;
    }

    protected MenuManager createMenuManager() {
        MenuManager mainMenu = new MenuManager();
        MenuManager fileMenu = new MenuManager("File");
        MenuManager helpMenu = new MenuManager("Help");

        // File popup menu
        fileMenu.add(new OpenFile());
        fileMenu.add(new Exit(this));

        // Help popup menu
        helpMenu.add(new About());

        mainMenu.add(fileMenu);
        mainMenu.add(helpMenu);

        return mainMenu;
    }

    public static void main(String[] args) {
        TestApp win = new TestApp();
        win.setBlockOnOpen(true);
        win.open();             
        Display.getCurrent().dispose();
    }

    class OpenFile extends Action {
        public OpenFile() {
            super("&Open Filer@Ctrl+O", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }

    class Exit extends Action {
        ApplicationWindow win;
        public Exit(ApplicationWindow aWin) {
            super("E&xit@Alt+X", AS_PUSH_BUTTON);
            this.win = aWin;
        }

        public void run() {
            this.win.close();
        }
    }
    class About extends Action {

        public About() {
            super("About", AS_PUSH_BUTTON);
        }
        public void run() {

        }
    }
}