`
yianpuodiaotu
  • 浏览: 240241 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

SWT/JFACE——toolbar/toolItem

阅读更多

工具栏通常有两种: toolbar、coolBar。两者的区分是CoolBar可以自由移动。

 

toolBar的实现通常有两种方式:

1、使用ToolBar和ToolItem;

2、使用ToolBarManager 、ActionContributionItem、Action组合;

 

先介绍第一种方式:使用ToolBar和ToolItem

package menu.test;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ToolBarExample {
	Display display = new Display();
	Shell shell = new Shell(display);
	ToolBar toolBar;
	Action forwardAction,homeAction;
	public ToolBarExample() {
		MenuManager menuManager = new MenuManager();
		
		//1.添加工具栏
		toolBar = new ToolBar(shell,SWT.FLAT|SWT.WRAP|SWT.RIGHT |SWT.BORDER);

		//2.添加工具项-push
		ToolItem pushItem = new ToolItem(toolBar,SWT.PUSH);
		pushItem.setText("Push Item");
		Image icon = new Image(shell.getDisplay(),"icons/forward.gif");
		pushItem.setImage(icon);
		
		//3.添加工具项-check,radio,seperator
		ToolItem checkItem = new ToolItem(toolBar,SWT.CHECK);
		checkItem.setText("Check Item");
		ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO);
		radioItem1.setText("RADIO item 1");
		ToolItem radioItem2 = new ToolItem(toolBar, SWT.RADIO);
		radioItem2.setText("RADIO item 2");
		ToolItem separatorItem = new ToolItem(toolBar, SWT.SEPARATOR);
		
		//4.下拉DROP_DOWN、
		final ToolItem dropDownItem = new ToolItem(toolBar, SWT.DROP_DOWN);
		dropDownItem.setText("DROP_DOWN item");
		dropDownItem.setToolTipText("Click here to see a drop down menu ...");
		final Menu menu = new Menu(shell, SWT.POP_UP);
		new MenuItem(menu, SWT.PUSH).setText("Menu item 1");
		new MenuItem(menu, SWT.PUSH).setText("Menu item 2");
		new MenuItem(menu, SWT.SEPARATOR);
		new MenuItem(menu, SWT.PUSH).setText("Menu item 3");
		// 设置工具项的事件监听器
		dropDownItem.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) {
					Rectangle bounds = dropDownItem.getBounds();
					Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
					// 设置菜单的显示位置
					menu.setLocation(point);
					menu.setVisible(true);
				}
			}
		});
		
		// 5.事件处理
		// 设置工具项的事件监听器
		Listener selectionListener = new Listener() {
			public void handleEvent(Event event) {
				ToolItem item = (ToolItem) event.widget;
				System.out.println(item.getText() + " is selected");
				if ((item.getStyle() & SWT.RADIO) != 0
						|| (item.getStyle() & SWT.CHECK) != 0)
					System.out.println("Selection status: "
							+ item.getSelection());
			}
		};
		pushItem.addListener(SWT.Selection, selectionListener);
		checkItem.addListener(SWT.Selection, selectionListener);
		radioItem1.addListener(SWT.Selection, selectionListener);
		radioItem2.addListener(SWT.Selection, selectionListener);
		dropDownItem.addListener(SWT.Selection, selectionListener);
		
		
		// 6. 其他组件text 、checkbox
		ToolItem textItem = new ToolItem(toolBar,SWT.SEPARATOR);
		Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE);
	    text.pack();
	    textItem.setWidth(text.getSize().x);
	    textItem.setControl(text);
	    
	    ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR);
	    Combo combo = new Combo(toolBar, SWT.READ_ONLY);
	    for (int i = 0; i < 4; i++) {
	      combo.add("Item " + i);
	    }
	    combo.pack();
	    sep.setWidth(combo.getSize().x);
	    sep.setControl(combo);
	    
		toolBar.pack();
		shell.addListener(SWT.Resize, new Listener(){

			public void handleEvent(Event arg0) {
				Rectangle clientArea = shell.getClientArea();
				toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT));
				
			}
			
		});
		shell.setSize(400, 300);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	
	public static void main(String[] args) {
		new ToolBarExample();
	}

}

效果图:


对于第二种方法:

package menu;
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.graphics.Rectangle;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;

public class ToolBarMangerExample {
  Display display = new Display();
  Shell shell = new Shell(display);
  Action openAction,exitAction;
  ToolBar toolBar;

  public ToolBarMangerExample() {
    MenuManager menuManager = new MenuManager();
    
    toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT | SWT.BORDER);
    final ToolBarManager toolBarManager = new ToolBarManager(toolBar);
    initActions();// 初始化Action
    toolBarManager.add(openAction);
    ActionContributionItem item = new ActionContributionItem(exitAction);
    item.setMode(ActionContributionItem.MODE_FORCE_TEXT);
    toolBarManager.add(item);
    toolBarManager.update(true);
    toolBar.pack();
    
    MenuManager fileMenuManager = new MenuManager("&File");
    fileMenuManager.add(openAction);
    fileMenuManager.add(exitAction);
    menuManager.add(fileMenuManager);
    menuManager.updateAll(true);
  
    shell.setMenuBar(menuManager.createMenuBar((Decorations)shell));
    shell.addListener(SWT.Resize, new Listener(){

		public void handleEvent(Event arg0) {
			Rectangle clientArea = shell.getClientArea();
			toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT));
		}
	});
    shell.setSize(300,200);
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    display.dispose();
  }
  private void initActions(){
	  openAction =
	      new Action("&openAction",
	        ImageDescriptor.createFromFile(null, "icons/Open.gif")) {
	      public void run() {
	        System.out.println("OPEN");
	      }
	    };
	    openAction.setAccelerator(SWT.CTRL + 'O');


	    exitAction =
	      new Action("&Exit",
	        ImageDescriptor.createFromFile(null, "icons/Exit.gif")) {
	      public void run() {
	        System.out.println("Exit");
	      }
	    };
	    exitAction.setAccelerator(SWT.CTRL + 'E');
  }
  public static void main(String[] args) {
    new ToolBarMangerExample();
  }
}

效果图:



  • 大小: 18.5 KB
  • 大小: 10.3 KB
  • 大小: 13 KB
分享到:
评论

相关推荐

    SWT/JFace专题 --- SWT/JFace概述

    SWT (Standard Widget Toolkit) 和 JFace 是两个在Java中用于构建图形用户界面(GUI)的重要库,尤其在开发Eclipse插件时被广泛应用。它们是开源项目,由Eclipse基金会维护,为开发者提供了丰富的控件和高级UI设计...

    swt/jface.jar

    标题中的"swt/jface.jar"是一个关键组件,它在Java开发环境中用于构建用户界面。SWT(Standard Widget Toolkit)是IBM开发的一个开源GUI库,它提供了与原生操作系统更紧密集成的图形用户界面控件。JFace是建立在SWT...

    swt/jface中文教程

    本教程将涵盖 SWT/JFace 的概览、目的、许可证和平台支持、SWT 和 JFace 的区别、使用 SWT/JFace 构建 GUI 应用程序、使用 SWT/JFace 管理颜色、绘图、字体、图案等内容。 SWT/JFace 的目的: SWT/JFace 的主要...

    SWT/Jface 开发入门指南

    SWT/Jface开发入门指南是一篇专为初学者编写的教程,旨在帮助他们快速搭建开发环境并掌握使用SWT和JFace编写图形化应用程序的基本技巧。以下是该教程所涵盖的关键知识点: 1. **SWT和JFace简介**: - SWT全称...

    SWT/Jface API 3.4

    SWT (Standard Widget Toolkit) 和 JFace 是两个用于构建 Java 应用程序的图形用户界面(GUI)的库,它们都是由 Eclipse 开源项目提供的。SWT 是一个直接与操作系统进行交互的底层库,提供了丰富的控件和组件,而 ...

    SWT/JFACE客户端登录窗口例子

    SWT (Standard Widget Toolkit) 和 JFace 是 Eclipse 开发框架中的两个重要组件,它们主要用于构建图形用户界面(GUI)。SWT 是一个底层的 GUI 工具包,它提供了与操作系统直接交互的能力,使得应用程序能够拥有原生...

    Eclipse SWT/JFace 核心应用 带书签

    Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace

    SWT/JFace开发实例

    SWT/JFace开发实例

    SWT/JFace学习文档

    SWT/JFace是Java开发图形用户界面(GUI)的两个重要库,由Eclipse基金会维护。它们被广泛用于构建桌面应用程序,尤其是与Eclipse IDE相关的项目。本学习文档旨在介绍SWT/JFace的基础知识,帮助开发者更好地理解和...

    swt/jface in action +中文版+英文版+源码 下载

    SWT (Standard Widget Toolkit) 和 JFace 是两个与Java GUI编程密切相关的库,它们由Eclipse项目维护。本文将深入探讨这两个技术,并结合《SWT/JFace in Action》这本书的相关内容,提供一个全面的知识框架。 SWT是...

    Eclipse SWT/JFace 核心应用光盘源码

    Eclipse SWT(Standard Widget Toolkit)和JFace是Java开发中用于构建图形用户界面(GUI)的库,尤其在开发Eclipse插件和RCP(Rich Client Platform)应用程序时非常重要。这两个库提供了丰富的组件和框架,使得...

    eclipse swt/jface核心应用源码

    Eclipse SWT/JFace是Eclipse框架中的两个关键组件,它们为构建用户界面提供了强大的支持。SWT(Standard Widget Toolkit)是Eclipse的本机GUI库,而JFace是基于SWT构建的更高层次的抽象层,它简化了UI开发过程。 ...

    Eclipse swt/jface核心应用源码(带视频)

    Eclipse SWT (Standard Widget Toolkit) 和 JFace 是两个在Java编程环境下用于构建图形用户界面(GUI)的重要库,尤其在开发Eclipse插件和RCP(Rich Client Platform)应用程序时非常常见。SWT是Eclipse项目的一个...

    swt/jface实例开发

    【SWT/JFace实例开发】是一份针对Java GUI编程的学习资源,主要聚焦于SWT(Standard Widget Toolkit)和JFace这两个强大的图形用户界面库。SWT是Eclipse项目的一部分,它提供了一套与操作系统直接交互的原生控件,...

    SWT/JFace从入门到精通

    【SWT/JFace从入门到精通】 SWT(Standard Widget Toolkit)和JFace是Eclipse平台下用于构建图形用户界面(GUI)的两个关键库。它们为Java开发者提供了丰富的控件和高级UI功能,使创建桌面应用程序变得简单而强大。...

    Eclipse全程指南:基础知识、Web开发、SWT/JFace开发、Eclipse插件_带书签_高清完整版

    王占全,苏玲 著; ISBN:7-121-05152-4 学习 SWT/JFace 的好书!

    SWT/JFace的核心应用与实战教程的PDF电子版.rar

    基于 Java 桌面程序开发的图形库...本书主要讲解了如何使用 SWT 和 JFace 进行应用程序的开发,通过本书系统而全面的 SWT/JFace 知识学习,将帮助读者快 速开发出完美、实用的 GUI 程序,轻松完成繁琐的界面、菜单编程.

    SWT/JFace 3.5 API (HTML)

    SWT(Standard Widget Toolkit)和JFace是Eclipse开源项目中的两个重要库,主要用于构建图形用户界面(GUI)。它们提供了一种与平台无关的方式来创建Java应用程序的用户界面,特别是对于开发IDE和其他复杂的桌面应用...

    Eclipse SWT/JFace 开发实战精解+完美书签+光盘源码完整版

    Eclipse SWT(Standard Widget Toolkit)和JFace是Java开发领域中的两个重要库,主要用于构建桌面应用程序。它们都是Eclipse IDE的重要组成部分,提供了丰富的图形用户界面(GUI)组件和设计模式,使得开发者能够...

Global site tag (gtag.js) - Google Analytics