SWT(JFace)体验之TabFolder(分页),ToolBar(工具条)时间:2022-11-11 16:35:02代码如下: TabFolder(分页) package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; import org.eclipse.swt.widgets.Text; public class TabFolderExample { Display display = new Display(); Shell shell = new Shell(display); Image icon = new Image(shell.getDisplay(), "C:/icons/icon.gif"); public TabFolderExample() { shell.setLayout(new FillLayout()); final TabFolder tabFolder = new TabFolder(shell, SWT.BOTTOM); Button button = new Button(tabFolder, SWT.NULL); button.setText("This is a button."); TabItem tabItem1 = new TabItem(tabFolder, SWT.NULL); tabItem1.setText("item #1"); tabItem1.setImage(icon); tabItem1.setControl(button); Text text = new Text(tabFolder, SWT.MULTI); text.setText("This is a text control."); TabItem tabItem2 = new TabItem(tabFolder, SWT.NULL); tabItem2.setText("item #2"); tabItem2.setImage(icon); tabItem2.setControl(text); Label label = new Label(tabFolder, SWT.NULL); label.setText("This is a text lable."); TabItem tabItem3 = new TabItem(tabFolder, SWT.NULL); tabItem3.setText("item #3"); tabItem3.setControl(label); tabFolder.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { System.out.println("Selected item index = " + tabFolder.getSelectionIndex()); System.out.println("Selected item = " + (tabFolder.getSelection() == null ? "null" : tabFolder.getSelection()[0].toString())); } public void widgetDefaultSelected(SelectionEvent e) { widgetSelected(e); } }); //tabFolder.setSelection(new TabItem[]{tabItem2, tabItem3}); tabFolder.setSelection(2); shell.setSize(400, 120); shell.open(); System.out.println(tabFolder.getSelectionIndex()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } public static void main(String[] args) { new TabFolderExample(); } } ToolBar(工具条) package swt_jface.demo9; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.ActionContributionItem; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Decorations; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; public class TestToolBar { Display display = new Display(); Shell shell = new Shell(display); public TestToolBar() { MenuManager menuManager = new MenuManager(); ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT); final ToolBarManager manager = new ToolBarManager(toolBar); final Action actionForward = new Action( "&Forward", ImageDescriptor.createFromFile( null, "C:/icons/web/forward.gif")) { public void run() { System.out.println("FORWARD"); } }; actionForward.setAccelerator(SWT.CTRL + 'F'); Action actionHome = new Action( "&Home", ImageDescriptor.createFromFile(null, "C:/icons/web/home.gif")) { public void run() { System.out.println("HOME"); } }; actionHome.setAccelerator(SWT.CTRL + 'H'); manager.add(actionForward); ActionContributionItem item = new ActionContributionItem(actionHome); item.setMode(ActionContributionItem.MODE_FORCE_TEXT); manager.add(item); manager.update(true); toolBar.pack(); MenuManager fileMenuManager = new MenuManager("&File"); fileMenuManager.add(actionForward); fileMenuManager.add(actionHome); menuManager.add(fileMenuManager); menuManager.updateAll(true); shell.setMenuBar(menuManager.createMenuBar((Decorations)shell)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } public static void main(String[] args) { new TestToolBar(); } }