JFace的Window是对Shell的封装, 便利使用. 封装的内容包括,
Window的创建分做3步,
若想window.open()直接进入event loop, window close后方才退出, 则可以在open()调用前调用setBlockOnOpen
windowObj.setBlockOnOpen(true);
int retCode = windowObj.open();
Window继承的methods常用的有,
ApplicationWindow是Window的子类, 增强的功能包括,
一个例子
public class Snippet002ApplicationWindow extends ApplicationWindow {
public static ApplicationWindow app;
Action newAction;
Action openAction;
Action saveAction;
Action saveAsAction;
Action exitAction;
Action copyAction;
Action cutAction;
Action pasteAction;
Action helpAction;
Text content;
class MyAction extends Action{
public MyAction(String text, String tooltip, int keycode, String img)
{
super(text);
setToolTipText(tooltip);
if(keycode != 0)
setAccelerator(keycode);
if(img != null && !img.isEmpty())
{
ImageDescriptor desc = ImageDescriptor.createFromFile(Snippet002ApplicationWindow.class, img);
setImageDescriptor(desc);
}
}
public void run()
{
MessageBox dialog = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_WARNING | SWT.OK);
dialog.setText("Action");
dialog.setMessage("do "+getText());
dialog.open();
}
}
public Snippet002ApplicationWindow() {
super(null);
app = this;
newAction = new MyAction("&New", "new file", SWT.CTRL + 'N', "file_new.png");
openAction = new MyAction("&Open", "open file", SWT.CTRL + 'O', "file_open.png");
saveAction = new MyAction("&Save", "save file", SWT.CTRL + 'S', "file_save.png");
saveAsAction = new MyAction("Save &as", "save as file", SWT.CTRL + 'A', "file_saveas.png");
exitAction = new MyAction("E&xit", "exit app", 0, null);
copyAction = new MyAction("&Copy", "copy to clipboard", SWT.CTRL + 'C', "edit_copy.png");
cutAction = new MyAction("Cut(&X)", "cut", SWT.CTRL + 'X', "edit_cut.png");
pasteAction = new MyAction("&Paste", "paste from clipboard", SWT.CTRL + 'P', "edit_paste.png");
helpAction = new MyAction("&Help", "help", 0, "help.png");
addMenuBar();
addToolBar(SWT.FLAT);
addStatusLine();
//创建menu
MenuManager menuBar = getMenuBarManager();
MenuManager fileMenu = new MenuManager("文件(&F)");
MenuManager editMenu = new MenuManager("编辑(&E)");
MenuManager helpMenu = new MenuManager("帮助(&H)");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
fileMenu.add(newAction);
fileMenu.add(openAction);
fileMenu.add(new Separator());
fileMenu.add(saveAction);
fileMenu.add(saveAsAction);
fileMenu.add(new Separator());
fileMenu.add(exitAction);
editMenu.add(copyAction);
editMenu.add(cutAction);
editMenu.add(pasteAction);
helpMenu.add(helpAction);
//创建toolbar
ToolBarManager toolbarMgr = getToolBarManager();
toolbarMgr.add(new GroupMarker("file"));
toolbarMgr.add(new GroupMarker("edit"));
toolbarMgr.add(new GroupMarker("help"));
toolbarMgr.appendToGroup("file", newAction);
toolbarMgr.appendToGroup("file", openAction);
toolbarMgr.appendToGroup("file", saveAction);
toolbarMgr.appendToGroup("file", saveAsAction);
toolbarMgr.appendToGroup("edit", new Separator());
toolbarMgr.appendToGroup("edit", copyAction);
toolbarMgr.appendToGroup("edit", cutAction);
toolbarMgr.appendToGroup("edit", pasteAction);
toolbarMgr.appendToGroup("help", new Separator());
toolbarMgr.appendToGroup("help", helpAction);
//statusLine initialization
setStatus("Ready");
}
@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Snippet002ApplicationWindow");
shell.setMaximized(true);
}
@Override
protected Point getInitialSize() {
//return super.getInitialSize();
return new Point(300,200);
}
@Override
protected Control createContents(Composite parent) {
Composite comp = new Composite(parent, SWT.BORDER);
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
comp.setBackground(new Color(200, 200, 0));
comp.setLayout(new GridLayout());
Label label = new Label(comp, SWT.PUSH);
label.setText("new window");
label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
return comp;
}
public static void main(String[] args) {
Display display = new Display(); //the default display, Display.getCurrent()
Snippet002ApplicationWindow appWin = new Snippet002ApplicationWindow();
appWin.setBlockOnOpen(true);
app.open();
display.dispose();
}
}
class Window implements IShellProvider;
class ApplicationWindow;
class Dialog;
class PopupDialog;
驽马一架 一花一世界 2021/12/10